From 551dff9e719f0256e198b25df2ea43f1cc5cf1e5 Mon Sep 17 00:00:00 2001 From: Alexander Todorov Date: Tue, 18 Apr 2023 18:52:44 +0300 Subject: [PATCH] Enable additional upload validators for django-attachments to block files which may be executed by the browser and cause an issue with users who aren't careful. Test with .exe file from React OS! --- docs/source/modules/tcms.kiwi_attachments.rst | 15 +++++++++ .../tcms.kiwi_attachments.validators.rst | 7 +++++ docs/source/modules/tcms.rst | 1 + tcms/kiwi_attachments/__init__.py | 0 tcms/kiwi_attachments/apps.py | 14 +++++++++ tcms/kiwi_attachments/tests/__init__.py | 0 .../kiwi_attachments/tests/test_validators.py | 29 ++++++++++++++++++ tcms/kiwi_attachments/validators.py | 23 ++++++++++++++ tcms/settings/common.py | 2 +- tests/ui/data/reactos_csrss.exe | Bin 0 -> 9728 bytes 10 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 docs/source/modules/tcms.kiwi_attachments.rst create mode 100644 docs/source/modules/tcms.kiwi_attachments.validators.rst create mode 100644 tcms/kiwi_attachments/__init__.py create mode 100644 tcms/kiwi_attachments/apps.py create mode 100644 tcms/kiwi_attachments/tests/__init__.py create mode 100644 tcms/kiwi_attachments/tests/test_validators.py create mode 100644 tcms/kiwi_attachments/validators.py create mode 100755 tests/ui/data/reactos_csrss.exe diff --git a/docs/source/modules/tcms.kiwi_attachments.rst b/docs/source/modules/tcms.kiwi_attachments.rst new file mode 100644 index 0000000000..cf54db0f28 --- /dev/null +++ b/docs/source/modules/tcms.kiwi_attachments.rst @@ -0,0 +1,15 @@ +tcms.kiwi\_attachments package +============================== + +.. automodule:: tcms.kiwi_attachments + :members: + :undoc-members: + :show-inheritance: + +Submodules +---------- + +.. toctree:: + :maxdepth: 4 + + tcms.kiwi_attachments.validators diff --git a/docs/source/modules/tcms.kiwi_attachments.validators.rst b/docs/source/modules/tcms.kiwi_attachments.validators.rst new file mode 100644 index 0000000000..a457456bdc --- /dev/null +++ b/docs/source/modules/tcms.kiwi_attachments.validators.rst @@ -0,0 +1,7 @@ +tcms.kiwi\_attachments.validators module +======================================== + +.. automodule:: tcms.kiwi_attachments.validators + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/source/modules/tcms.rst b/docs/source/modules/tcms.rst index 64e66c6707..ea3b3f4760 100644 --- a/docs/source/modules/tcms.rst +++ b/docs/source/modules/tcms.rst @@ -15,6 +15,7 @@ Subpackages tcms.bugs tcms.core tcms.issuetracker + tcms.kiwi_attachments tcms.kiwi_auth tcms.management tcms.rpc diff --git a/tcms/kiwi_attachments/__init__.py b/tcms/kiwi_attachments/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tcms/kiwi_attachments/apps.py b/tcms/kiwi_attachments/apps.py new file mode 100644 index 0000000000..c3b3d23d6e --- /dev/null +++ b/tcms/kiwi_attachments/apps.py @@ -0,0 +1,14 @@ +from attachments.apps import AttachmentsConfig + +from . import validators + + +class AppConfig(AttachmentsConfig): + """ + Defines custom form validators! + """ + + attachment_validators = ( + validators.deny_uploads_ending_in_dot_exe, + validators.deny_uploads_containing_script_tag, + ) diff --git a/tcms/kiwi_attachments/tests/__init__.py b/tcms/kiwi_attachments/tests/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tcms/kiwi_attachments/tests/test_validators.py b/tcms/kiwi_attachments/tests/test_validators.py new file mode 100644 index 0000000000..1f17135483 --- /dev/null +++ b/tcms/kiwi_attachments/tests/test_validators.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +# pylint: disable=attribute-defined-outside-init, invalid-name, objects-update-used + +import base64 +from xmlrpc.client import Fault + +from tcms.rpc.tests.utils import APITestCase + + +class TestValidators(APITestCase): + def test_uploading_svg_with_inline_script_should_fail(self): + with open("tests/ui/data/inline_javascript.svg", "rb") as svg_file: + b64 = base64.b64encode(svg_file.read()).decode() + + with self.assertRaisesRegex(Fault, "File contains forbidden