diff --git a/src/test/unit/web_interface/test_filter.py b/src/test/unit/web_interface/test_filter.py index e1e7cca4f..47e75bebe 100644 --- a/src/test/unit/web_interface/test_filter.py +++ b/src/test/unit/web_interface/test_filter.py @@ -6,6 +6,7 @@ import pytest import web_interface.filter as flt +from test.common_helper import create_test_file_object UNSORTABLE_LIST = [[], ()] @@ -496,3 +497,18 @@ def test_str_to_hex(input_, expected_result): ) def test_octal_to_readable(input_, include_type, expected_result): assert flt.octal_to_readable(input_, include_type=include_type) == expected_result + + +@pytest.mark.parametrize( + ('type_analysis', 'expected_result'), + [ + ({'file_type': {}}, False), + ({'file_type': {'result': {'mime': 'image/png'}}}, True), + ({'file_type': {'result': {'mime': 'text/plain'}}}, True), + ({'file_type': {'result': {'mime': 'application/octet-stream'}}}, False), + ({'file_type': {'result': {'mime': 'application/javascript'}}}, True), + ], +) +def test_is_text_file_or_image(type_analysis, expected_result): + fo = create_test_file_object(analyses=type_analysis) + assert flt.is_text_file_or_image(fo) == expected_result diff --git a/src/web_interface/components/ajax_routes.py b/src/web_interface/components/ajax_routes.py index e9a7ca607..c69832e1c 100644 --- a/src/web_interface/components/ajax_routes.py +++ b/src/web_interface/components/ajax_routes.py @@ -12,7 +12,12 @@ from web_interface.file_tree.file_tree import remove_virtual_path_from_root from web_interface.file_tree.file_tree_node import FileTreeNode from web_interface.file_tree.jstree_conversion import convert_to_jstree_node -from web_interface.filter import bytes_to_str_filter, encode_base64_filter +from web_interface.filter import ( + bytes_to_str_filter, + encode_base64_filter, + is_image, + is_text_file, +) from web_interface.security.decorator import roles_accepted from web_interface.security.privileges import PRIVILEGES @@ -89,12 +94,12 @@ def _get_nice_uid_list_html(self, input_data, root_uid): def ajax_get_binary(self, mime_type, uid): mime_type = mime_type.replace('_', '/') binary = self.intercom.get_binary_and_filename(uid)[0] - if 'text/' in mime_type: + if is_text_file(mime_type): return ( '
'
                 f'{html.escape(bytes_to_str_filter(binary))}
' ) - if 'image/' in mime_type: + if is_image(mime_type): return ( '
str: def str_to_hex(string: str) -> str: return string.encode(errors='replace').hex() + + +KNOWN_TEXT_MIME_TYPES = { # that don't start with text/ + 'application/javascript', + 'application/json', + 'application/pgp-keys', + 'firmware/intel-hex', + 'message/news', +} + + +def is_text_file(mime: str) -> bool: + return mime.startswith('text/') or mime in KNOWN_TEXT_MIME_TYPES + + +def is_image(mime: str) -> bool: + return mime.startswith('image/') + + +def is_text_file_or_image(fo: FileObject) -> bool: + mime = fo.processed_analysis.get('file_type', {}).get('result', {}).get('mime', '') + return is_image(mime) or is_text_file(mime) diff --git a/src/web_interface/templates/show_analysis.html b/src/web_interface/templates/show_analysis.html index 6ce2bb8c2..8e295d20b 100644 --- a/src/web_interface/templates/show_analysis.html +++ b/src/web_interface/templates/show_analysis.html @@ -360,10 +360,8 @@
- {%- set is_text_preview = firmware.processed_analysis["file_type"]["result"]['mime'][0:5] == "text/" or firmware.processed_analysis["file_type"]["result"]['mime'][0:6] == "image/" %} -