Difficulty: Advanced
What is an XXE (XML External Entity) injection, what can an attacker do with it, and how do you prevent it?
XXE is a vulnerability in applications that parse XML with external entity resolution enabled. XML lets a document define entities, including external ones that point at a URI, and a parser that resolves them will fetch that URI during parsing. An attacker who can submit XML can therefore make the server read local files, reach internal services or hang on a malicious resource.
XML has a Document Type Definition where you can declare entities. A normal entity is just a text substitution. An external entity uses a SYSTEM identifier with a URI, and a vulnerable parser resolves it by reading that URI, whether it is file:///etc/passwd or http://internal-service/. The parser then inserts whatever it fetched into the document, which the application may echo back or act upon.
/etc/passwd and have them returned in the response.169.254.169.254, to steal instance credentials.XXE is not only in obvious XML APIs. It appears in SOAP services, SVG image uploads, DOCX and XLSX files (which are zipped XML), RSS and SAML assertions. Any feature that accepts a file which is really XML is a candidate.
| Control | Detail |
|---|---|
| Disable DTDs entirely | The strongest fix. If the parser never processes a DOCTYPE, external entities cannot be declared. |
| Disable external entities and external DTD loading | Use the parser's secure-processing flags if you must allow DTDs. |
| Prefer safer formats | JSON has no entity concept; use it where you control the contract. |
| Use hardened libraries | In Python use defusedxml; modern .NET and current Java defaults are safer but verify them. |
| Validate and sandbox uploads | Treat SVG, DOCX and XML uploads as untrusted and parse them with entities off. |
The fix is almost always a parser configuration change, not input filtering. Most modern parsers can be told to reject DOCTYPE declarations in one line.
<!ENTITY> strings instead of disabling the feature in the parser.// Java: disable DTDs completely on the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
dbf.setXIncludeAware(false);
dbf.setExpandEntityReferences(false);
DocumentBuilder builder = dbf.newDocumentBuilder();
disallow-doctype-decl is the single most effective setting: with no DOCTYPE allowed, no external entity can be declared in the first place.
# The stdlib parser can be unsafe for untrusted input.
# Use defusedxml, which disables entity expansion and external access.
from defusedxml.ElementTree import fromstring
def parse_untrusted(xml_bytes: bytes):
# Raises EntitiesForbidden / DTDForbidden on malicious input
return fromstring(xml_bytes)
defusedxml wraps the standard parsers and blocks DTDs, external entities and billion-laughs expansion, so untrusted XML is safe to parse.
XML Parsing, External Entities, SSRF, File Disclosure, Parser Hardening