What Is an XXE Attack? XML External Entities Explained
An XXE attack abuses XML external entities to read files, reach internal services, or exhaust memory. How XXE works and how to configure parsers safely.
An XXE attack (XML External Entity attack) exploits an XML parser that processes entity declarations from untrusted input. By defining an entity that points to a local file or a URL, an attacker can make the server read files from its own disk, send requests to internal systems, or consume enormous amounts of memory. XXE isn’t a bug in any single application’s logic — it’s a consequence of insecure parser defaults, which is why it keeps appearing anywhere XML is accepted.
A quick primer on XML entities
XML supports a Document Type Definition (DTD), an optional section at the top of a document that can declare entities — named placeholders the parser substitutes when it sees &name;. An internal entity is just a string:
<!DOCTYPE note [
<!ENTITY company "Example Corp">
]>
<note>Hello from &company;</note>
An external entity uses the SYSTEM keyword to tell the parser to fetch its value from a URI:
<!ENTITY data SYSTEM "file:///path/to/file">
This was designed for assembling documents from multiple files. The danger is that many parsers honor these declarations by default, including in documents submitted by strangers.
How a classic XXE attack works
Imagine an endpoint that accepts an XML order and echoes back the product name:
<order><product>Widget</product></order>
An attacker submits this instead:
<?xml version="1.0"?>
<!DOCTYPE order [
<!ENTITY xxe SYSTEM "file:///etc/hostname">
]>
<order><product>&xxe;</product></order>
If the parser resolves external entities, it reads the file and substitutes its contents into the product element. The application then echoes the “product name” back, handing the attacker the file’s contents. The same technique can target configuration files, source code, or credentials stored on disk — anything the server process can read, much like a path traversal attack.
Variants of XXE
Server-side request forgery. Replace file:// with http:// and the parser makes an HTTP request from the server’s network position. That lets an attacker probe internal services, hit cloud metadata endpoints, or reach admin panels never exposed to the internet. This is effectively SSRF delivered through XML.
Blind XXE. When the application doesn’t echo parsed data back, attackers can use parameter entities and an external DTD they control to make the server send file contents out to the attacker’s server in a URL, or trigger error messages that leak data.
Entity expansion attacks. Entities can reference other entities. A small document with nested entities that each expand into many copies of the previous one grows exponentially when parsed, exhausting memory and CPU. This class is commonly known as the “billion laughs” attack. It doesn’t need external entities at all, only DTD processing.
| Variant | What the attacker gains | Requires external entities |
|---|---|---|
| File disclosure | Contents of local files | Yes |
| SSRF via XXE | Requests from inside the network | Yes |
| Blind / out-of-band XXE | Data exfiltrated without direct output | Yes |
| Entity expansion (billion laughs) | Denial of service | No, only DTD processing |
Where XML hides
Many teams assume they’re safe because their API speaks JSON. XML turns up in more places than you might expect:
- SOAP and legacy web services, which are XML by definition.
- SAML single sign-on assertions, which are XML documents parsed by the service provider.
- Office documents. DOCX, XLSX, and PPTX files are ZIP archives full of XML. An upload feature that parses them may parse attacker-controlled XML.
- SVG images. SVG is XML, so an image upload or thumbnail pipeline that parses SVG with a permissive parser is exposed.
- RSS and Atom feeds, sitemaps, and configuration imports.
- Content-type switching. Some frameworks accept XML on an endpoint designed for JSON if the client sends
Content-Type: application/xml.
How to prevent XXE
The fix is almost always a parser configuration change, not input filtering. Trying to detect malicious DTDs with regular expressions is fragile; disabling the feature is reliable.
Disable DTDs entirely if you can. Most applications never need them. Rejecting any document containing a DOCTYPE eliminates both external entity and entity expansion attacks in one step.
If DTDs are needed, disable external entities and external DTD loading. Turn off resolution of external general entities, external parameter entities, and loading of external DTDs.
Configuration differs by language and library:
- Java: on
DocumentBuilderFactoryand similar factories, set thehttp://apache.org/xml/features/disallow-doctype-declfeature totrue, or disable external general and parameter entities if DTDs must be allowed. - .NET: set
DtdProcessingtoProhibitonXmlReaderSettings, and don’t supply anXmlResolver. - Python: use the
defusedxmlpackage, which wraps the standard library parsers with safe defaults. - PHP: libxml-based parsers should not be given the
LIBXML_NOENTflag, which enables entity substitution.
Check the defaults of the specific library version you use; some modern parsers are safe by default, others are not, and wrappers can re-enable features silently.
Prefer simpler formats where possible. If you control both ends of an integration, JSON has no entity mechanism at all. Our JSON vs YAML comparison covers trade-offs between common data formats.
Apply defense in depth. Run parsing services with least-privilege file access, restrict outbound network traffic from servers that don’t need it, and validate structure with a schema after safe parsing. These don’t replace parser hardening, but they limit damage if one parser is missed. The broader principles are in our guide to input validation vs sanitization vs escaping.
How XXE relates to other injection flaws
XXE shares a pattern with insecure deserialization: a powerful parsing feature designed for trusted input is applied to untrusted data. In both cases the vulnerable code often looks perfectly ordinary — a single call to parse a document — and the risk lives in configuration the developer never looked at.
The takeaway
An XXE attack abuses XML’s entity system to make a parser read local files, make internal network requests, or exhaust resources. It’s caused by parser defaults, so the fix is configuration: disable DTDs entirely, or at minimum disable external entities and external DTD loading, in every XML parser your application uses — including the ones hidden in SAML, SVG, and office-document handling. Back that up with least-privilege file access and outbound network restrictions.
Keep reading
Chisato · · 4 min read What Is Insecure Deserialization?
Insecure deserialization lets attackers turn a trusted object format into remote code execution. How the attack works and how to prevent it.
Chisato · · 4 min read Referrer-Policy Header Explained: Values and Best Practice
The Referrer-Policy header controls how much of a page's URL the browser sends in the Referer header. What each value does and which one to choose.
Chisato · · 5 min read Refresh Token Rotation Explained: Safer Long Sessions
Refresh token rotation issues a new refresh token on every use and revokes the old one, so a stolen token gets caught on reuse. How it works and pitfalls.