Skip to content

Commit

Permalink
Use bleach_allowlist.generally_xss_unsafe
Browse files Browse the repository at this point in the history
when searching for potentially dangerous files.
  • Loading branch information
atodorov committed Jun 5, 2023
1 parent d789f4b commit 700e9f9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 31 deletions.
6 changes: 4 additions & 2 deletions tcms/kiwi_attachments/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def test_uploading_svg_with_inline_script_should_fail(self, file_name):
with open(f"tests/ui/data/{file_name}", "rb") as svg_file:
b64 = base64.b64encode(svg_file.read()).decode()

message = str(_("File contains forbidden <script> tag"))
tag_name = "script"
message = str(_(f"File contains forbidden tag: <{tag_name}>"))
with self.assertRaisesRegex(Fault, message):
self.rpc_client.User.add_attachment("inline_javascript.svg", b64)

Expand All @@ -34,7 +35,8 @@ def test_uploading_svg_with_forbidden_attributes_should_fail(self, file_name):
with open(f"tests/ui/data/{file_name}", "rb") as svg_file:
b64 = base64.b64encode(svg_file.read()).decode()

message = str(_("File contains forbidden attribute:"))
attr_name = "onload"
message = str(_(f"File contains forbidden attribute: `{attr_name}`"))
with self.assertRaisesRegex(Fault, message):
self.rpc_client.User.add_attachment("image.svg", b64)

Expand Down
13 changes: 9 additions & 4 deletions tcms/kiwi_attachments/validators.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from bleach_allowlist import generally_xss_unsafe
from django.forms import ValidationError
from django.utils.translation import gettext_lazy as _


def deny_uploads_containing_script_tag(uploaded_file):
for chunk in uploaded_file.chunks(2048):
if chunk.lower().find(b"<script") > -1:
raise ValidationError(_("File contains forbidden <script> tag"))
for tag_name in generally_xss_unsafe:
if chunk.lower().find(b"<" + tag_name.encode()) > -1:
raise ValidationError(_(f"File contains forbidden tag: <{tag_name}>"))

if chunk.lower().find(b"onload=") > -1:
raise ValidationError(_("File contains forbidden attribute:") + "onload")
for attr_name in ("onload",):
if chunk.lower().find(attr_name.encode() + b"=") > -1:
raise ValidationError(
_(f"File contains forbidden attribute: `{attr_name}`")
)


def deny_uploads_ending_in_dot_exe(uploaded_file):
Expand Down
12 changes: 9 additions & 3 deletions tcms/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,17 @@ msgid ""
"href=\"%(doc_url)s\">documentation</a> and enable SSL."
msgstr ""

#: tcms/kiwi_attachments/validators.py:8
msgid "File contains forbidden <script> tag"
#: tcms/kiwi_attachments/validators.py:11
#, python-brace-format
msgid "File contains forbidden tag: <{tag_name}>"
msgstr ""

#: tcms/kiwi_attachments/validators.py:15
#, python-brace-format
msgid "File contains forbidden attribute: `{attr_name}`"
msgstr ""

#: tcms/kiwi_attachments/validators.py:12
#: tcms/kiwi_attachments/validators.py:19
msgid "Uploading executable files is forbidden"
msgstr ""

Expand Down
11 changes: 0 additions & 11 deletions tests/ui/data/inline_javascript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 0 additions & 11 deletions tests/ui/data/inline_javascript_mixed_case.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 700e9f9

Please sign in to comment.