# Buggy code
def run(request, api=False):
    """View iOS Files."""
    try:
        logger.info("View iOS Source File")
        exp = "Error Description"
        file_format = "cpp"
        if api:
            fil = request.POST["file"]
            md5_hash = request.POST["hash"]
            mode = request.POST["type"]
            viewsource_form = ViewSourceIOSApiForm(request.POST)
        else:
            fil = request.GET["file"]
            md5_hash = request.GET["md5"]
            mode = request.GET["type"]
            viewsource_form = ViewSourceIOSForm(request.GET)
        typ = set_ext_api(fil)
        if not viewsource_form.is_valid():
            err = FormUtil.errors_message(viewsource_form)
            if api:
                return err
            return print_n_send_error_response(request, err, False, exp)
        if mode == "ipa":
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/Payload/")
        elif mode == "ios":
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/")
        sfile = os.path.join(src, fil)
        if not is_safe_path(src, sfile):
            msg = "Path Traversal Detected!"
            if api:
                return {"error": "Path Traversal Detected!"}
            return print_n_send_error_response(request, msg, False, exp)
        dat = ""
        sql_dump = {}
        if typ == "m":
            file_format = "cpp"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        elif typ == "xml":
            file_format = "xml"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        elif typ == "plist":
            file_format = "json"
            dat = biplist.readPlist(sfile)
            try:
                dat = json.dumps(dat, indent=4, sort_keys=True)
            except Exception:
                pass
        elif typ == "db":
            file_format = "asciidoc"
            sql_dump = read_sqlite(sfile)
        elif typ == "txt" and fil == "classdump.txt":
            file_format = "cpp"
            app_dir = os.path.join(settings.UPLD_DIR, md5_hash + "/")
            cls_dump_file = os.path.join(app_dir, "classdump.txt")
            if is_file_exists(cls_dump_file):
                with io.open(
                    cls_dump_file, mode="r", encoding="utf8", errors="ignore"
                ) as flip:
                    dat = flip.read()
            else:
                dat = "Class Dump result not Found"
        elif typ == "txt":
            file_format = "text"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        else:
            if api:
                return {"error": "Invalid Parameters"}
            return HttpResponseRedirect("/error/")
        context = {
            "title": escape(ntpath.basename(fil)),
            "file": escape(ntpath.basename(fil)),
            "type": file_format,
            "dat": dat,
            "sql": sql_dump,
            "version": settings.MOBSF_VER,
        }
        template = "general/view.html"
        if api:
            return context
        return render(request, template, context)
    except Exception as exp:
        logger.exception("Error Viewing Source")
        msg = str(exp)
        exp = exp.__doc__
        if api:
            return print_n_send_error_response(request, msg, True, exp)
        return print_n_send_error_response(request, msg, False, exp)


# Traceback (FileNotFoundError)
MobSFy Android Instance Traceback (most recent call last): File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 408, in mobsfy_init self.mobsf_agents_setup('frida') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 418, in mobsf_agents_setup self.install_mobsf_ca('install') File "C:\Users\Lijoy KFH\Mobile-Security-Framework-MobSF-master\DynamicAnalyzer\views\android\environment.py", line 140, in install_mobsf_ca pem = open(get_ca_dir(), 'rb').read() FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\Lijoy KFH\\.mitmproxy\\mitmproxy-ca-cert.pem' [ERROR] 12/Apr/2020 18:53:11 – Failed to MobSFy the instance [ERROR] 12/Apr/2020 18:53:11 – Internal Server Error: /android_dynamic/"

# Fixed code
def run(request, api=False):
    """View iOS Files."""
    try:
        logger.info("View iOS Source File")
        exp = "Error Description"
        file_format = None
        if api:
            fil = request.POST["file"]
            md5_hash = request.POST["hash"]
            mode = request.POST["type"]
            viewsource_form = ViewSourceIOSApiForm(request.POST)
        else:
            fil = request.GET["file"]
            md5_hash = request.GET["md5"]
            mode = request.GET["type"]
            viewsource_form = ViewSourceIOSForm(request.GET)
        typ = set_ext_api(fil)
        if not viewsource_form.is_valid():
            err = FormUtil.errors_message(viewsource_form)
            if api:
                return err
            return print_n_send_error_response(request, err, False, exp)
        if mode == "ipa":
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/Payload/")
        elif mode == "ios":
            src = os.path.join(settings.UPLD_DIR, md5_hash + "/")
        sfile = os.path.join(src, fil)
        if not is_safe_path(src, sfile):
            msg = "Path Traversal Detected!"
            if api:
                return {"error": "Path Traversal Detected!"}
            return print_n_send_error_response(request, msg, False, exp)
        dat = ""
        sql_dump = {}
        if typ == "m":
            file_format = "cpp"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        elif typ == "xml":
            file_format = "xml"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        elif typ == "plist":
            file_format = "json"
            dat = biplist.readPlist(sfile)
            try:
                dat = json.dumps(dat, indent=4, sort_keys=True)
            except Exception:
                pass
        elif typ == "db":
            file_format = "asciidoc"
            sql_dump = read_sqlite(sfile)
        elif typ == "txt" and fil == "classdump.txt":
            file_format = "cpp"
            app_dir = os.path.join(settings.UPLD_DIR, md5_hash + "/")
            cls_dump_file = os.path.join(app_dir, "classdump.txt")
            if is_file_exists(cls_dump_file):
                with io.open(
                    cls_dump_file, mode="r", encoding="utf8", errors="ignore"
                ) as flip:
                    dat = flip.read()
            else:
                dat = "Class Dump result not Found"
        elif typ == "txt":
            file_format = "text"
            with io.open(sfile, mode="r", encoding="utf8", errors="ignore") as flip:
                dat = flip.read()
        else:
            if api:
                return {"error": "Invalid Parameters"}
            return HttpResponseRedirect("/error/")
        context = {
            "title": escape(ntpath.basename(fil)),
            "file": escape(ntpath.basename(fil)),
            "type": file_format,
            "dat": dat,
            "sql": sql_dump,
            "version": settings.MOBSF_VER,
        }
        template = "general/view.html"
        if api:
            return context
        return render(request, template, context)
    except Exception as exp:
        logger.exception("Error Viewing Source")
        msg = str(exp)
        exp = exp.__doc__
        if api:
            return print_n_send_error_response(request, msg, True, exp)
        return print_n_send_error_response(request, msg, False, exp)