Handle invalid XML

This commit is contained in:
michaeldavie
2019-11-05 22:13:06 -05:00
parent 13a2624850
commit d4ec6dee65
2 changed files with 14 additions and 3 deletions
+13 -2
View File
@@ -134,7 +134,10 @@ def _parse_report_record(record, offline=False, nameservers=None,
new_record["policy_evaluated"] = new_policy_evaluated
new_record["identifiers"] = record["identifiers"].copy()
new_record["auth_results"] = OrderedDict([("dkim", []), ("spf", [])])
lowered_from = new_record["identifiers"]["header_from"].lower()
if type(new_record["identifiers"]["header_from"]) == str:
lowered_from = new_record["identifiers"]["header_from"].lower()
else:
lowered_from = ''
new_record["identifiers"]["header_from"] = lowered_from
if record["auth_results"] is not None:
auth_results = record["auth_results"].copy()
@@ -216,10 +219,13 @@ def parse_aggregate_report_xml(xml, offline=False, nameservers=None,
"""
errors = []
# Parse XML and recover from errors
try:
xmltodict.parse(xml)["feedback"]
except Exception as e:
errors.append(e.__str__())
tree = etree.parse(BytesIO(xml.encode('utf-8')),
etree.XMLParser(recover=True))
xml = etree.tostring(tree).decode('utf-8')
try:
# Replace XML header (sometimes they are invalid)
@@ -497,6 +503,11 @@ def parsed_aggregate_reports_to_csv_rows(reports):
row["spf_results"] = ",".join(map(to_str, dkim_results))
rows.append(row)
for r in rows:
for k, v in r.items():
if type(v) is not str:
r[k] = ''
return rows
+1 -1
View File
@@ -28,7 +28,7 @@
</policy_evaluated>
</row>
<identifiers>
<header_from>example.com</header_from>
<header_from>bad<xml.net</header_from>
</identifiers>
<auth_results>
<spf>