highUser input reaching your database or shell

XML From the Request Parsed With Entity Expansion

XML supplied by the caller is parsed with a parser that honours external entities.

Why it matters

XML has a feature for including other documents by reference. A parser that follows those references will happily read /etc/passwd or fetch an internal URL on the caller's behalf, and an entity that expands recursively turns a tiny document into gigabytes. defusedxml is the standard library's own parsers with those features switched off.

What it looks like

This is the shape of code that triggers the rule. AI tools produce it because it works, and nothing tells them it is unsafe.

ElementTree on the request body
import xml.etree.ElementTree as ET
root = ET.fromstring(request.data)

The smallest fix

minimal patch
# Before
import xml.etree.ElementTree as ET
root = ET.fromstring(request.data)
# After
import defusedxml.ElementTree as ET
root = ET.fromstring(request.data)

The better fix

If you have a few more minutes, this is the approach that holds up as the app grows.

safe alternative
# With lxml, build the parser explicitly:
parser = lxml.etree.XMLParser(resolve_entities=False, no_network=True, huge_tree=False)
root = lxml.etree.fromstring(request.data, parser)
# Better still, accept JSON for new endpoints.

Let your AI tool fix it

When the scanner finds this in your project, it fills in the file and line for you. This is the prompt it gives you to paste into Claude Code, Cursor or whatever you use.

In [the file] at line [the line number] XML from the request is parsed unsafely: [the flagged code]. Switch to the matching defusedxml module (defusedxml replaces xml.etree, minidom, sax, pulldom and xmlrpc drop-in), or for lxml pass an XMLParser with resolve_entities=False and no_network=True. Add a test that submits a document with an external entity and asserts it is rejected.

How to check the fix worked

1. Submit XML containing <!ENTITY x SYSTEM "file:///etc/hostname"> and
   confirm the request fails or the entity is not expanded.
2. Confirm a normal document still parses.

Further reading