# Buggy 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)


# Traceback (FileNotFoundError)
[ERROR] 18/Sep/2020 16:56:43 - Reading from Info.plist
Traceback (most recent call last):
File "/root/Mobile-Security-Framework-MobSF/StaticAnalyzer/views/ios/plist_analysis.py", line 67, in plist_analysis
dirs = os.listdir(src)
FileNotFoundError: [Errno 2] No such file or directory: '/root/.MobSF/uploads/ab66d07fa05376c8bb931e981e9e470e/Payload/'
[ERROR] 18/Sep/2020 16:56:43 - Error Perfroming Static Analysis
Traceback (most recent call last):
File "/root/Mobile-Security-Framework-MobSF/StaticAnalyzer/views/ios/static_analyzer.py", line 102, in static_analyzer_ios
app_dict['appstore'] = app_search(infoplist_dict.get('id'))
AttributeError: 'NoneType' object has no attribute 'get'
[ERROR] 18/Sep/2020 16:56:43 - 'NoneType' object has no attribute 'get'
[ERROR] 18/Sep/2020 16:56:43 - Internal Server Error: /StaticAnalyzer_iOS/

# 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)
        base = Path(settings.UPLD_DIR) / md5_hash
        if mode == "ipa":
            src1 = base / "payload"
            src2 = base / "Payload"
            if src1.exists():
                src = src1
            elif src2.exists():
                src = src2
            else:
                raise Exception("MobSF cannot find Payload directory")
        elif mode == "ios":
            src = base
        sfile = src / fil
        sfile = sfile.as_posix()
        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)