From 65efce9efe8286309e8b60657ca54722018405de Mon Sep 17 00:00:00 2001 From: Ajin Abraham Date: Mon, 28 Oct 2024 19:06:56 -0700 Subject: [PATCH] Code qa: sonarqube --- mobsf/MobSF/utils.py | 10 +++- mobsf/MobSF/views/home.py | 53 +++++++++---------- .../views/ios/binary_analysis.py | 3 +- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/mobsf/MobSF/utils.py b/mobsf/MobSF/utils.py index e63a9bb5c..841fb5eb7 100755 --- a/mobsf/MobSF/utils.py +++ b/mobsf/MobSF/utils.py @@ -949,6 +949,10 @@ def get_scan_logs(checksum): return [] +class TaskTimeoutError(Exception): + pass + + def run_with_timeout(func, limit, *args, **kwargs): def run_func(result, *args, **kwargs): result.append(func(*args, **kwargs)) @@ -964,5 +968,7 @@ def run_func(result, *args, **kwargs): if thread.is_alive(): msg = (f'function <{func.__name__}> ' f'timed out after {limit} seconds') - raise Exception(msg) - return result[0] if result else None + raise TaskTimeoutError(msg) + if result and len(result) > 0: + return result[0] + return None diff --git a/mobsf/MobSF/views/home.py b/mobsf/MobSF/views/home.py index 035267643..26cbe5e6d 100755 --- a/mobsf/MobSF/views/home.py +++ b/mobsf/MobSF/views/home.py @@ -334,39 +334,38 @@ def search(request, api=False): query = request.POST['query'] else: query = request.GET['query'] - checksum = None - if not re.match(MD5_REGEX, query): - file_names = RecentScansDB.objects.filter( - FILE_NAME__icontains=query, - ) - if file_names.exists(): - checksum = file_names[0].MD5 - - package_names = RecentScansDB.objects.filter( - PACKAGE_NAME__icontains=query, - ) - if package_names.exists(): - checksum = package_names[0].MD5 - - app_names = RecentScansDB.objects.filter( - APP_NAME__icontains=query, - ) - if app_names.exists(): - checksum = app_names[0].MD5 - else: - checksum = query + + if not query: + msg = 'No search query provided.' + return print_n_send_error_response(request, msg, api) + + checksum = query if re.match(MD5_REGEX, query) else find_checksum(query) if checksum and re.match(MD5_REGEX, checksum): - db_obj = RecentScansDB.objects.filter(MD5=checksum) - if db_obj.exists(): - e = db_obj[0] - url = f'/{e.ANALYZER}/{e.MD5}/' + db_obj = RecentScansDB.objects.filter(MD5=checksum).first() + if db_obj: + url = f'/{db_obj.ANALYZER}/{db_obj.MD5}/' if api: - return {'checksum': e.MD5} - return HttpResponseRedirect(url) + return {'checksum': db_obj.MD5} + else: + return HttpResponseRedirect(url) + msg = 'You can search by MD5, app name, package name, or file name.' return print_n_send_error_response(request, msg, api, 'Scan not found') + +def find_checksum(query): + """Get the first matching checksum from the database.""" + search_fields = ['FILE_NAME', 'PACKAGE_NAME', 'APP_NAME'] + + for field in search_fields: + result = RecentScansDB.objects.filter( + **{f'{field}__icontains': query}).first() + if result: + return result.MD5 + + return None + # AJAX diff --git a/mobsf/StaticAnalyzer/views/ios/binary_analysis.py b/mobsf/StaticAnalyzer/views/ios/binary_analysis.py index f64d627a4..6b3675dff 100755 --- a/mobsf/StaticAnalyzer/views/ios/binary_analysis.py +++ b/mobsf/StaticAnalyzer/views/ios/binary_analysis.py @@ -70,10 +70,9 @@ def ipa_macho_analysis(binary): data['checksec'] = chksec data['symbols'] = symbols data['libraries'] = libs - return data except Exception: logger.exception('Running MachO Analysis') - return data + return data def binary_analysis(checksum, src, tools_dir, app_dir, executable_name):