Skip to content

Lesson 16 of 20 · 16m

XXE: the parser reads your filesystem

External entities turn any XML-ish input — SAML, docx, SVG — into file read plus SSRF, and the disabling flags differ per stack.

Hand an XML parser <!DOCTYPE r [<!ENTITY x SYSTEM "file:///etc/passwd">]> and it substitutes the file into &x; — external entities are a feature of XML, not a bug in your code. The same mechanism accepts http://169.254.169.254/latest/meta-data/iam/security-credentials/, which makes XXE a file read and an SSRF delivered in one DOCTYPE.

XXE earned its own slot at A4 in the OWASP Top 10 2017 (folded into Security Misconfiguration in 2021) because it keeps landing in everything that parses XML-ish bytes: SAML Responses, SOAP, RSS, SVG uploads, and .docx/.xlsx — zips of XML, which is how the well-documented 2014 Facebook careers XXE ran through a Word upload. Apache Solr's /update handler took an XXE via <dataConfig> in October 2017 (CVE-2017-12629) and read files off the server.

The defaults that betray you

Every major stack resolves entities differently, and "differently" historically means "on":

  • Java: DocumentBuilderFactory and SAXParserFactory resolved external entities for decades. XMLConstants.FEATURE_SECURE_PROCESSING caps entity expansion but does not stop external DTD fetches — the flag that does is http://apache.org/xml/features/disallow-doctype-decl, or setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "").
  • .NET: XmlResolver fetched externals by default until .NET 4.5.2. The hardened shape is new XmlReaderSettings { DtdProcessing = DtdProcessing.Prohibit, XmlResolver = null } with XmlReader.Create(stream, settings) — the settings, not the reader class, carry the policy.
  • PHP: libxml_disable_entity_loader(true) was the old switch; PHP 8 removed it because libxml ≥2.9 stops loading external entities — except LIBXML_NOENT, which re-enables entity expansion. That flag in simplexml_load_string calls is the residual bug.
  • Python: stdlib xml.etree doesn't fetch external entities but does expand internal DTDs (the billion-laughs DoS — nested <!ENTITY> declarations that expand lol9 into billions of repetitions), and lxml's XMLParser resolves entities unless built with XMLParser(resolve_entities=False, no_network=True). defusedxml exists solely to wrap the stdlib and kill DTDs outright — use it.

The Java form, written out:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
dbf.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");

Blind XXE is the common case

Most apps don't echo the parsed document back, so nothing shows on screen — that stops nothing. Out-of-band XXE exfiltrates through the fetch itself: point the entity at http://attacker.example/ with the file contents baked into the URL or a parameter entity chain, and the data rides out on the parser's outbound request. no_network kills it dead; an egress filter on the app is the belt to that suspenders.

Why "we don't use XML" is half-wrong

You do: every .docx, .svg, SAML assertion, RSS import, and SOAP endpoint on the way in is XML. The parser usually lives inside a library you didn't pick, running with your process's filesystem and network reach — which is why XXE keeps surfacing in file-upload and SSO plumbing rather than in code anyone remembers writing. The cost asymmetry is why this matters: disallow-doctype-decl or resolve_entities=False costs nothing unless you genuinely parse DTD-bearing documents, which almost nothing does. Keep XInclude off too — <xi:include href="file:///etc/passwd" parse="text"/> is the sibling exfil path when XIncludeAware is enabled.

Practice

Where JSON works, use JSON and delete the XML path entirely. Where XML is mandatory — SAML, docx imports, legacy APIs — reject DOCTYPE at the boundary and set the flags above explicitly; FEATURE_SECURE_PROCESSING alone is not the fix. no_network/ACCESS_EXTERNAL_* matter as much as entity resolution: a parser that can't fetch can't leak. Audit the transitive parsers too — jackson-dataformat-xml, xstream, python-docx, anything rendering SVG — because the entity resolves inside the library, not inside your repo.

Keep reading

Related articles from the SudoSecurity blog.