# Buggy code
def mobsf_ca(request):
    """Install and Remove MobSF Proxy RootCA"""
    try:
        if request.method == "POST":
            data = {}
            act = request.POST["action"]
            rootca = get_ca_dir()
            adb = getADB()
            if act == "install":
                logger.info("Installing MobSF RootCA")
                adb_command(["push", rootca, "/data/local/tmp/" + settings.ROOT_CA])
                if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                    # For some reason, avd emulator does not have cp binary
                    adb_command(
                        [
                            "/data/local/tmp/busybox",
                            "cp",
                            "/data/local/tmp/" + settings.ROOT_CA,
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                    adb_command(
                        [
                            "chmod",
                            "644",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                else:
                    adb_command(
                        [
                            "su",
                            "-c",
                            "cp",
                            "/data/local/tmp/" + settings.ROOT_CA,
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                    adb_command(
                        [
                            "su",
                            "-c",
                            "chmod",
                            "644",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                adb_command(["rm", "/data/local/tmp/" + settings.ROOT_CA], True)
                data = {"ca": "installed"}
            elif act == "remove":
                logger.info("Removing MobSF RootCA")
                if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                    adb_command(
                        ["rm", "/system/etc/security/cacerts/" + settings.ROOT_CA], True
                    )
                else:
                    adb_command(
                        [
                            "su",
                            "-c",
                            "rm",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                data = {"ca": "removed"}
            return HttpResponse(json.dumps(data), content_type="application/json")
        else:
            return HttpResponseRedirect("/error/")
    except:
        PrintException("[ERROR] MobSF RootCA Handler")
        return HttpResponseRedirect("/error/")


# Traceback (KeyError)
[INFO] 08/Feb/2019 16:17:54 - VM Starting
[INFO] 08/Feb/2019 16:17:55 - "POST /DynamicAnalyzer/ HTTP/1.1" 200 32400
[INFO] 08/Feb/2019 16:17:55 - "GET /static/css/devices.min.css HTTP/1.1" 200 44241
[INFO] 08/Feb/2019 16:17:55 - "GET /download/screen/screen.png HTTP/1.1" 302 0
[DEBUG] 08/Feb/2019 16:17:55 - Exception while resolving variable 'exp' in template 'general/error.html'.
Traceback (most recent call last):
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/base.py", line 829, in _resolve_lookup
current = current[bit]
File "/home/userhome/Mobile-Security-Framework-MobSF/venv/lib/python3.6/site-packages/django/template/context.py", line 83, in __getitem__
raise KeyError(key)
KeyError: 'exp'

During handling of the above exception, another exception occurred:

# Fixed code
def mobsf_ca(request):
    """Install and Remove MobSF Proxy RootCA"""
    try:
        if request.method == "POST":
            data = {}
            act = request.POST["action"]
            rootca = get_ca_dir()
            adb = getADB()
            if act == "install":
                logger.info("Installing MobSF RootCA")
                adb_command(["push", rootca, "/data/local/tmp/" + settings.ROOT_CA])
                if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                    # For some reason, avd emulator does not have cp binary
                    adb_command(
                        [
                            "/data/local/tmp/busybox",
                            "cp",
                            "/data/local/tmp/" + settings.ROOT_CA,
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                    adb_command(
                        [
                            "chmod",
                            "644",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                else:
                    adb_command(
                        [
                            "su",
                            "-c",
                            "cp",
                            "/data/local/tmp/" + settings.ROOT_CA,
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                    adb_command(
                        [
                            "su",
                            "-c",
                            "chmod",
                            "644",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                adb_command(["rm", "/data/local/tmp/" + settings.ROOT_CA], True)
                data = {"ca": "installed"}
            elif act == "remove":
                logger.info("Removing MobSF RootCA")
                if settings.ANDROID_DYNAMIC_ANALYZER == "MobSF_AVD":
                    adb_command(
                        ["rm", "/system/etc/security/cacerts/" + settings.ROOT_CA], True
                    )
                else:
                    adb_command(
                        [
                            "su",
                            "-c",
                            "rm",
                            "/system/etc/security/cacerts/" + settings.ROOT_CA,
                        ],
                        True,
                    )
                data = {"ca": "removed"}
            return HttpResponse(json.dumps(data), content_type="application/json")
        else:
            return print_n_send_error_response(request, "Only POST allowed", True)
    except:
        PrintException("MobSF RootCA Handler")
        return print_n_send_error_response(request, "Error in RootCA Handler", True)