fix: handle XML namespaces in DMARCParser for web.de/gmx.net compatibility

Agent-Logs-Url: https://github.com/christianlouis/dmarq/sessions/56611674-6132-435f-ab36-728464bb9157

Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-03-29 17:59:13 +00:00
parent f7200d973c
commit 0844981bc0
3 changed files with 89 additions and 1 deletions
+9
View File
@@ -128,6 +128,14 @@ class DMARCParser:
return None
@staticmethod
def _strip_namespace(el) -> None:
"""Recursively remove XML namespace prefixes from element tags in-place."""
if "}" in el.tag:
el.tag = el.tag.split("}", 1)[1]
for child in el:
DMARCParser._strip_namespace(child)
@staticmethod
def _parse_metadata(root) -> dict:
"""Parse the report_metadata section of a DMARC XML report."""
@@ -216,6 +224,7 @@ class DMARCParser:
"""
try:
root = ET.fromstring(xml_content)
DMARCParser._strip_namespace(root)
report = DMARCParser._parse_metadata(root)
+48
View File
@@ -47,3 +47,51 @@ SAMPLE_XML = """\
</record>
</feedback>
"""
SAMPLE_XML_WITH_NAMESPACE = """\
<?xml version="1.0" encoding="UTF-8" ?>
<feedback xmlns="urn:ietf:params:xml:ns:dmarc-2.0">
<report_metadata>
<org_name>web.de</org_name>
<email>dmarc@web.de</email>
<report_id>987654321</report_id>
<date_range>
<begin>1597449600</begin>
<end>1597535999</end>
</date_range>
</report_metadata>
<policy_published>
<domain>example.com</domain>
<adkim>r</adkim>
<aspf>r</aspf>
<p>reject</p>
<sp>reject</sp>
<pct>100</pct>
</policy_published>
<record>
<row>
<source_ip>198.51.100.5</source_ip>
<count>3</count>
<policy_evaluated>
<disposition>reject</disposition>
<dkim>pass</dkim>
<spf>pass</spf>
</policy_evaluated>
</row>
<identifiers>
<header_from>example.com</header_from>
</identifiers>
<auth_results>
<dkim>
<domain>example.com</domain>
<result>pass</result>
<selector>s1</selector>
</dkim>
<spf>
<domain>example.com</domain>
<result>pass</result>
</spf>
</auth_results>
</record>
</feedback>
"""
+32 -1
View File
@@ -4,7 +4,7 @@ import zipfile
import pytest
from app.services.dmarc_parser import DMARCParser
from app.tests.test_data import SAMPLE_XML
from app.tests.test_data import SAMPLE_XML, SAMPLE_XML_WITH_NAMESPACE
class TestDMARCParser:
@@ -67,6 +67,37 @@ class TestDMARCParser:
with pytest.raises(ValueError):
DMARCParser.parse_file(b"not xml at all", "report.xml")
def test_parse_xml_report_with_namespace(self):
"""Test parsing a DMARC XML report that uses an XML namespace (e.g. web.de/gmx.net)."""
xml_bytes = SAMPLE_XML_WITH_NAMESPACE.encode("utf-8")
result = DMARCParser.parse_file(xml_bytes, "report.xml")
# Metadata
assert result["report_id"] == "987654321"
assert result["org_name"] == "web.de"
assert result["email"] == "dmarc@web.de"
assert result["begin_timestamp"] == 1597449600
assert result["end_timestamp"] == 1597535999
# Policy published
assert result["domain"] == "example.com"
assert result["policy"]["p"] == "reject"
# Records
assert len(result["records"]) == 1
record = result["records"][0]
assert record["source_ip"] == "198.51.100.5"
assert record["count"] == 3
assert record["disposition"] == "reject"
assert record["dkim_result"] == "pass"
assert record["spf_result"] == "pass"
assert record["header_from"] == "example.com"
# Summary
assert result["summary"]["total_count"] == 3
assert result["summary"]["passed_count"] == 3
assert result["summary"]["failed_count"] == 0
def test_unsupported_extension_returns_none(self):
"""Test that an unsupported file extension raises ValueError."""
with pytest.raises(ValueError, match="Could not extract XML"):