Skip to content

Commit 8f18e3e

Browse files
committed
Refactor XML ParseError handling and use defusedxml (fixes #14752)
Switched to defusedxml for better security alignment and updated the error format to use a field-specific dictionary as recommended in DRF. Also moved the catch block to avoid misleading comment alignment.
1 parent ca8a6d1 commit 8f18e3e

1 file changed

Lines changed: 7 additions & 7 deletions

File tree

dojo/api_v2/serializers.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
import logging
55
import re
66
import time
7-
import xml.etree.ElementTree as ET
87
from datetime import datetime
98

109
import six
1110
import tagulous
11+
from defusedxml import ElementTree as ET
1212
from django.conf import settings
1313
from django.contrib.auth.models import Group, Permission
1414
from django.contrib.auth.password_validation import validate_password
@@ -2394,14 +2394,14 @@ def process_scan(
23942394
duration = time.perf_counter() - start_time
23952395
LargeScanSizeProductAnnouncement(response_data=data, duration=duration)
23962396
ScanTypeProductAnnouncement(response_data=data, scan_type=context.get("scan_type"))
2397+
except ET.ParseError as e:
2398+
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
23972399
# convert to exception otherwise django rest framework will swallow them as 400 error
23982400
# exceptions are already logged in the importer
23992401
except SyntaxError as se:
24002402
raise Exception(se)
24012403
except ValueError as ve:
24022404
raise Exception(ve)
2403-
except ET.ParseError as e:
2404-
raise serializers.ValidationError(f"Malformed XML: {e}")
24052405

24062406
def validate(self, data: dict) -> dict:
24072407
scan_type = data.get("scan_type")
@@ -2704,14 +2704,14 @@ def process_scan(
27042704
duration = time.perf_counter() - start_time
27052705
LargeScanSizeProductAnnouncement(response_data=data, duration=duration)
27062706
ScanTypeProductAnnouncement(response_data=data, scan_type=context.get("scan_type"))
2707+
except ET.ParseError as e:
2708+
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
27072709
# convert to exception otherwise django rest framework will swallow them as 400 error
27082710
# exceptions are already logged in the importer
27092711
except SyntaxError as se:
27102712
raise Exception(se)
27112713
except ValueError as ve:
27122714
raise Exception(ve)
2713-
except ET.ParseError as e:
2714-
raise serializers.ValidationError(f"Malformed XML: {e}")
27152715

27162716
def save(self, *, push_to_jira=False):
27172717
# Go through the validate method
@@ -2788,12 +2788,12 @@ def save(self):
27882788
create_dojo_meta,
27892789
origin="API",
27902790
)
2791+
except ET.ParseError as e:
2792+
raise serializers.ValidationError({"file": f"Malformed XML: {e}"})
27912793
except SyntaxError as se:
27922794
raise Exception(se)
27932795
except ValueError as ve:
27942796
raise Exception(ve)
2795-
except ET.ParseError as e:
2796-
raise serializers.ValidationError(f"Malformed XML: {e}")
27972797

27982798

27992799
class LanguageTypeSerializer(serializers.ModelSerializer):

0 commit comments

Comments
 (0)