# Buggy code
def view(request):
    """View File"""
    logger.info("Viewing File")
    try:
        typ = ""
        fil = ""
        rtyp = ""
        dat = ""
        if re.match("^[0-9a-f]{32}$", request.GET["md5"]):
            fil = request.GET["file"]
            md5_hash = request.GET["md5"]
            typ = request.GET["type"]
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/DYNAMIC_DeviceData/")
            sfile = os.path.join(src, fil)
            # Prevent Directory Traversal Attacks
            if ("../" in fil) or ("%2e%2e" in fil) or (".." in fil) or ("%252e" in fil):
                return HttpResponseRedirect("/error/")
            else:
                with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                    dat = flip.read()
                if (fil.endswith(".xml")) and (typ == "xml"):
                    rtyp = "xml"
                elif typ == "db":
                    dat = handle_sqlite(sfile)
                    rtyp = "asciidoc"
                elif typ == "others":
                    rtyp = "asciidoc"
                else:
                    return HttpResponseRedirect("/error/")
                context = {
                    "title": escape(ntpath.basename(fil)),
                    "file": escape(ntpath.basename(fil)),
                    "dat": dat,
                    "type": rtyp,
                }
                template = "general/view.html"
                return render(request, template, context)

        else:
            return HttpResponseRedirect("/error/")
    except:
        PrintException("[ERROR] Viewing File")
        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 view(request):
    """View File"""
    logger.info("Viewing File")
    try:
        typ = ""
        fil = ""
        rtyp = ""
        dat = ""
        if re.match("^[0-9a-f]{32}$", request.GET["md5"]):
            fil = request.GET["file"]
            md5_hash = request.GET["md5"]
            typ = request.GET["type"]
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/DYNAMIC_DeviceData/")
            sfile = os.path.join(src, fil)
            # Prevent Directory Traversal Attacks
            if ("../" in fil) or ("%2e%2e" in fil) or (".." in fil) or ("%252e" in fil):
                return print_n_send_error_response(
                    request, "Path Traversal Attack Detected"
                )
            else:
                with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                    dat = flip.read()
                if (fil.endswith(".xml")) and (typ == "xml"):
                    rtyp = "xml"
                elif typ == "db":
                    dat = handle_sqlite(sfile)
                    rtyp = "asciidoc"
                elif typ == "others":
                    rtyp = "asciidoc"
                else:
                    return print_n_send_error_response(
                        request, "File Type not supported"
                    )
                context = {
                    "title": escape(ntpath.basename(fil)),
                    "file": escape(ntpath.basename(fil)),
                    "dat": dat,
                    "type": rtyp,
                }
                template = "general/view.html"
                return render(request, template, context)
        else:
            return print_n_send_error_response(request, "Invalid Scan Hash")
    except:
        PrintException("Viewing File")
        return print_n_send_error_response(request, "ERROR Viewing File")