Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions dojo/api_v2/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import six
import tagulous
from defusedxml import ElementTree as ET

Check failure on line 11 in dojo/api_v2/serializers.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (N817)

dojo/api_v2/serializers.py:11:24: N817 CamelCase `ElementTree` imported as acronym `ET`
from django.conf import settings
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.password_validation import validate_password
Expand Down Expand Up @@ -2393,6 +2394,8 @@
duration = time.perf_counter() - start_time
LargeScanSizeProductAnnouncement(response_data=data, duration=duration)
ScanTypeProductAnnouncement(response_data=data, scan_type=context.get("scan_type"))
except ET.ParseError as e:
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
# convert to exception otherwise django rest framework will swallow them as 400 error
# exceptions are already logged in the importer
except SyntaxError as se:
Expand Down Expand Up @@ -2701,6 +2704,8 @@
duration = time.perf_counter() - start_time
LargeScanSizeProductAnnouncement(response_data=data, duration=duration)
ScanTypeProductAnnouncement(response_data=data, scan_type=context.get("scan_type"))
except ET.ParseError as e:
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
# convert to exception otherwise django rest framework will swallow them as 400 error
# exceptions are already logged in the importer
except SyntaxError as se:
Expand Down Expand Up @@ -2783,6 +2788,8 @@
create_dojo_meta,
origin="API",
)
except ET.ParseError as e:
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
except SyntaxError as se:
raise Exception(se)
except ValueError as ve:
Expand Down
1 change: 1 addition & 0 deletions unittests/scans/zap/malformed.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root
27 changes: 27 additions & 0 deletions unittests/test_issue_14752.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from unittests.dojo_test_case import DojoAPITestCase
import os

Check failure on line 2 in unittests/test_issue_14752.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (I001)

unittests/test_issue_14752.py:1:1: I001 Import block is un-sorted or un-formatted help: Organize imports

class XMLParseErrorTest(DojoAPITestCase):

Check failure on line 4 in unittests/test_issue_14752.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (E302)

unittests/test_issue_14752.py:4:1: E302 Expected 2 blank lines, found 1 help: Add missing blank line(s)
fixtures = ["dojo_testdata.json"]

def setUp(self):
super().setUp()
self.login_as_admin()

def test_import_scan_malformed_xml_zap(self):
"""
Test that importing a malformed XML file via the API returns a 400 error
instead of crashing the worker (propagating ParseError).
"""
# engagement 1 should exist from fixtures
relative_path = os.path.join("scans", "zap", "malformed.xml")

Check failure on line 17 in unittests/test_issue_14752.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (PTH118)

unittests/test_issue_14752.py:17:25: PTH118 `os.path.join()` should be replaced by `Path` with `/` operator

# We expect a 400 Bad Request if handled
response = self.import_scan_with_params(
filename=relative_path,
scan_type="ZAP Scan",
engagement=1,
expected_http_status_code=400

Check failure on line 24 in unittests/test_issue_14752.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (COM812)

unittests/test_issue_14752.py:24:42: COM812 Trailing comma missing help: Add trailing comma
)

Check failure on line 26 in unittests/test_issue_14752.py

View workflow job for this annotation

GitHub Actions / ruff-linting

ruff (W293)

unittests/test_issue_14752.py:26:1: W293 Blank line contains whitespace help: Remove whitespace from blank line
self.assertIn("Malformed XML", response["file"][0])
Loading