XXE in Plain English: When Your XML Parser Fetches Files for the Attacker
By Chris Clark · AppSec practitioner & AWS Solutions Architect
XML has a feature most developers never use on purpose. A document can define shortcuts called entities. Think of an entity as a named placeholder: write &name; and the parser swaps in whatever name stands for. Those definitions live in a DTD (Document Type Definition), the block at the top of a file. An entity can point at a URL, and then the parser goes and fetches that URL for you.
That's XXE, or XML External Entity injection. Someone sends your app XML with a DOCTYPE that says "this entity is file:///etc/passwd," and your parser opens the file and drops its contents into the document. If any of that document ends up in a response or an error message, they just read a file off your server. Point the entity at an http:// URL instead and your server makes requests to places the attacker can't reach directly.
MITRE files it as CWE-611, Improper Restriction of XML External Entity Reference. OWASP's Top 10:2025 lists it under A02:2025 Security Misconfiguration, not Injection. That placement tells you something. The bug isn't in your code's logic. It's a parser left on settings you never chose.
What it looks like in a Java 8 app
Here's the attacker's side, in the same entity syntax Oracle's JAXP Security Guide uses:
]>
&xxe;
And here's the app side. It's the one-liner you'll find in a lot of older Java code:
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(untrustedStream);
Nothing looks wrong, and that's the problem. Oracle's guide says the JDK XML processors, by default, "make a connection and fetch the external resources as specified." The default limit on which protocols they're allowed to use for external DTDs and entities is all.
Older Java apps take in XML from everywhere: SOAP endpoints, file uploads, partner feeds, config files, SAML assertions. Every one of those is a DocumentBuilderFactory, SAXParserFactory, XMLInputFactory, or TransformerFactory that somebody set up years ago.
The fix: refuse the DOCTYPE
The OWASP XXE Prevention Cheat Sheet's first rule is simple: if you don't need DTDs, turn them off and reject any document that has a DOCTYPE. Most business XML never needs one.
On Java 8, with the factory your legacy code already uses:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder builder = dbf.newDocumentBuilder();
SAX gets the same disallow-doctype-decl feature, set on the factory before you create the reader. StAX (the streaming XMLInputFactory) doesn't support that feature, so the cheat sheet has you turn off both DTD support and external entities:
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
XSLT and schema validation can load outside files on their own, with no entities involved. For TransformerFactory and SchemaFactory, the cheat sheet sets ACCESS_EXTERNAL_DTD to "", plus ACCESS_EXTERNAL_STYLESHEET or ACCESS_EXTERNAL_SCHEMA to "", before compiling. An empty string means "no protocols allowed."
One more rule from the cheat sheet: if a security setting throws because the parser doesn't support it, stop. Don't catch the exception and parse anyway.
The trap: flags that look like the fix
setExpandEntityReferences(false) sounds like exactly the right switch. It isn't. The cheat sheet says it only changes how entity references show up in the parsed tree, and you shouldn't use it in place of blocking external resources.
The SAX version is subtler. Suppose you allow DTDs and install a custom EntityResolver that rejects outside references. If you then call SAXParser.parse(source, handler), the handler replaces your resolver. The cheat sheet says to call reader.parse(...) directly when you depend on a resolver.
Same lesson as the SQL injection piece: the class name isn't the fix. The settings on that specific factory instance are.
Where it hides during a strangler replacement
When you carve a section out of the Java 8 monolith and rebuild it in Spring Boot, the XML handling often gets ported as-is. Nobody wants to re-test the partner's SOAP format, so the old XmlUtils.parse() helper comes along unchanged.
Two things make that worse in the new service.
First, Spring gives you a false sense of safety. Jaxb2Marshaller (Spring's JAXB wrapper) defaults to DTDs off and external entities off. But Spring's own docs say that setting only applies when you hand it a SAXSource or StreamSource. It has no effect on a DOMSource. So if the ported helper parses the XML into a DOM with the old unhardened factory and then passes that DOM to the marshaller, the safe default never runs. The XXE already happened.
Second, newInstance() uses the JAXP lookup, which can pick a third-party parser off the classpath, like that old Xerces jar the monolith dragged along. Its settings can differ from the JDK's. On the new service's modern JDK, the cheat sheet uses newDefaultInstance() (Java 9+), which always picks the built-in implementation.
Review rule: one hardened factory per service, built once, and every XML entry point goes through it.
What GitLab SAST flags
GitLab's Semgrep-based Java analyzer ships a family of XXE rules, all tagged CWE-611 and A02:2025. A few you'll see in Jira:
java_xxe_rule-DocumentBuilderFactoryDisallowDoctypeDeclMissingandjava_xxe_rule-SAXParserFactoryDisallowDoctypeDeclMissingjava_xxe_rule-XMLStreamRdr(StAX reader created without DTD/external-entity settings)java_xxe_rule-TransformerfactoryDTDNotDisabledjava_xxe_rule-DisallowDoctypeDeclFalse,java_xxe_rule-ExternalGeneralEntitiesTrue,java_xxe_rule-XMLInputFactoryExternalEntitiesEnabled(someone switched protection off on purpose)
Here's how they differ from the SQL injection rule. The DocumentBuilderFactory rule is still a taint rule ("taint" means following something from where it starts to where it's used), but what it follows is the factory, not user input. It starts at DocumentBuilderFactory.newInstance() and flags newDocumentBuilder() if neither disallow-doctype-decl nor both external-entity features got set on the way. The finding means "this parser is unhardened." It doesn't tell you who can send it XML.
When the finding lands in Jira:
Go to the factory, not the parse call. Check whether the feature gets set before newDocumentBuilder(). If it doesn't, the fix is mechanical: add disallow-doctype-decl, or route the call through the service's hardened factory. Don't argue reachability first. Fixing it is usually one line, while proving the XML is trusted means following it through every caller. If the code truly needs a DTD, that's a design conversation: external entities off, a rejecting resolver, and entity limits. It's not a one-line fix.
The short version
XXE happens when an XML parser follows a link the document hands it, to a file or to an internal URL. In Java, the default factories will do it. The fix is boring: reject DOCTYPEs with disallow-doctype-decl, turn off DTDs and external entities on StAX, and lock down external access on transformers and schema validators. Watch the strangler port, where an old parsing helper rides into a clean Spring Boot service and sidesteps the framework's safe defaults.
Chris's take: I don't accept "that XML comes from a partner, not a user" as a reason to close an XXE ticket. A partner feed is input you didn't write, coming from a server you don't patch. The fix is one line. The trust argument takes a meeting, and it's only true until their side gets popped.