SSOGEN / Learn / XXE attack: the payload, and the parser setting that stops it

Learn

XXE attack: the payload, and the parser setting that stops it

An XXE attack (XML eXternal Entity injection, CWE-611) makes an XML parser read a file or open a URL that the attacker names. Rejecting DOCTYPE in the parser stops it.

Cutaway of a mail machine: a red arm lifts a sheet from an attached filing drawer into the outgoing envelope
The application asked for a note and received file contents.

A stock JDK parser, given the five-line document below, printed demo-secret-line: the one line of a secret.txt file the application never meant to open. That is an XXE attack (XML eXternal Entity injection, CWE-611): the document declares an entity, and many parsers resolve it by default.

order.xml
<?xml version="1.0"?>
<!DOCTYPE order [
<!ENTITY xxe SYSTEM "secret.txt">
]>
<order><note>&xxe;</note></order>
Output, OpenJDK 21.0.12.1, default factory
$ java Demo.java default
note=demo-secret-line

The application asked for a note and received file contents. The Java program that produced this output is further down.

The DOCTYPE declares an entity and the parser fills it in

Each line of the document has one job. Only the middle three carry the attack.

Where In order.xml What it does
Line 1
<?xml version="1.0"?>
The XML declaration. Ordinary and harmless. Every XML document may start this way.
Lines 2 to 4
<!DOCTYPE order [ ... ]>
A document type declaration. Its square brackets hold declarations that the document brings with it.
Line 3
<!ENTITY xxe SYSTEM "secret.txt">
Declares an entity named xxe. Its value is whatever the system identifier points to. Here that is a relative file. An attacker supplies an absolute path with the file: scheme or an http URL, as OWASP describes.
Line 5
<order><note>&xxe;</note></order>
The reference, &xxe;. The parser swaps it for the file contents before the application sees the text. If the application shows the note, the attacker reads the file.

Some attacks need no echo: in a blind XXE attack the data leaves through a request the parser makes (PortSwigger Web Security Academy).

One crafted document can read a file, send a request or exhaust memory

The parser trusts declarations that arrive with the data. The impact depends on what the declaration points to.

Impact What the document does Evidence
File disclosure An entity names a local path, and the value lands in the output. The run above; CWE-611
Server-side request forgery An entity names a URL on a back-end system, and the parser requests it with the application’s network access. OWASP, PortSwigger
Data sent out blind Entity values travel in a request or a DNS name to a host the attacker controls. OWASP, PortSwigger
Memory exhaustion Entities built from entities expand geometrically. Our run below; CWE-776

The exhaustion row also ran, at a harmless size. Three lines of DTD defined a as ten digits, b as ten copies of a and c as ten copies of b. The same JDK default factory returned a note of 1,000 characters from a document of 196 bytes.

CWE-611 and CWE-776 (recursive entity references) both sit under A02:2025 Security Misconfiguration in the OWASP Top 10 2025.

Reject DOCTYPE in the parser, and block external access where DTDs stay

OWASP’s first rule is to disable DTDs wherever possible by rejecting DOCTYPE. The setting differs in every parser. Each run below used the order.xml above.

Java: one feature on the factory

This is the whole program. The argument picks which setting the factory gets, so the same file is parsed four ways.

Demo.java
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class Demo {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
switch (args[0]) {
case "doctype" -> dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
case "both" -> {
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
}
case "secure" -> dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
default -> { }
}
var doc = dbf.newDocumentBuilder().parse(new File("order.xml"));
System.out.println("note=" + doc.getElementsByTagName("note").item(0).getTextContent());
}
}

Before: no setting. The parser expands the entity and the program prints note=demo-secret-line.

After: disallow-doctype-decl set to true. The parser stops at line 2, column 10 with a SAXParseException that reads: DOCTYPE is disallowed when the feature "http://apache.org/xml/features/disallow-doctype-decl" set to true. Nothing is read.

Two more runs on the same JDK show what the other settings do.

Argument Setting Result
secure FEATURE_SECURE_PROCESSING only SAXParseException at line 5, column 19: 'file' access is not allowed due to restriction set by the accessExternalDTD property.
both disallow-doctype-decl and FEATURE_SECURE_PROCESSING The DOCTYPE message from the first run, at line 2

On this JDK, secure processing alone blocked the file read. OWASP still recommends rejecting DOCTYPE first, because secure processing alone is not a portable replacement for restricting external access. newInstance() can also pick a third-party provider with different settings, so test the provider you ship.

SAX, StAX, XSLT, schema validation, XPath and JAXB each have their own constructor and setting. The OWASP XXE Prevention Cheat Sheet lists them with code.

Python: lxml needs a parser with entities off

demo.py (lxml 4.6.5, Python 3.9.25)
import sys
from lxml import etree
if sys.argv[1] == "safe":
parser = etree.XMLParser(resolve_entities=False, no_network=True)
else:
parser = etree.XMLParser()
root = etree.parse("order.xml", parser).getroot()
print("note =", repr(root.find("note").text))

Before: python3 demo.py default with lxml 4.6.5. The entity is expanded and the program prints note = 'demo-secret-line'.

After: python3 demo.py safe with resolve_entities=False, no_network=True. The program prints note = None. The file is not read.

The standard library behaved differently. xml.etree.ElementTree raised ParseError: undefined entity &xxe;: line 5, column 13 and never opened the file. These results belong to these versions; run the same commands on yours. For untrusted XML, OWASP advises the defusedxml interfaces and forbid_dtd=True where they offer it, and checking pyexpat.EXPAT_VERSION on the deployed interpreter.

.NET: safe by default from 4.5.2, according to OWASP

This part was not run for this page. It restates the OWASP cheat sheet, read on 2026-11-17.

  • XmlDocument, XmlTextReader, XPathNavigator. Vulnerable to external entities and entity expansion below .NET Framework 4.5.2. Protected from 4.5.2 onward, where XmlResolver is null by default.
  • XmlReader, XmlDictionaryReader, XmlNodeReader. Not vulnerable before or after 4.5.2. From 4.5.2 they do not process an inline DTD by default.
  • To be explicit. Set DtdProcessing = DtdProcessing.Prohibit, which throws when a DOCTYPE is present, and do not assign a non-null XmlResolver.
  • ASP.NET. Set httpRuntime targetFramework to 4.5.2 or higher in Web.config. The effective version is the lower of the build version and that setting.

A SAML response is XML, so the parser reads it before any signature check

A SAML service provider receives a base64 form field from the browser. Whoever controls the browser controls that field.

Step What happens
1. Receive
The service provider decodes the field into XML text. Nothing in it has been verified yet.
2. Parse
The text becomes a document tree. A DTD in the text would be processed here.
3. Verify
The service provider checks the schema and the signatures on the tree it built.

The order is the point. A signature can only be checked on a parsed document, so the parser runs on input nobody has authenticated.

One widely used library, python3-saml, parses the response with forbid_dtd=True, forbid_entities=True when the Response object is created. Its is_valid method runs the checks afterward (xml_utils.py and response.py, master branch, read 2026-11-17). This is one library, so check the one you use.

For the structure of what arrives in step 1, see the annotated SAML assertion. The full sign-in flow is in what SAML is.

Find every parser in your code and test it with the payload

Start with the places that create a parser. The setting must be applied before the first parse call.

  1. List the constructors. In a Java project:
Find Java XML parsers
grep -rnE "DocumentBuilderFactory|SAXParserFactory|XMLInputFactory|TransformerFactory|SchemaFactory" src/
  1. For each hit, confirm that the factory sets the feature before it creates a parser. In Python, search for etree.XMLParser, etree.parse and etree.fromstring. In .NET, search for XmlReaderSettings, XmlTextReader and XmlDocument.
  2. Add a test that parses order.xml from this page and expects an exception, or an empty value, instead of demo-secret-line.
  3. Fail closed. OWASP treats a security setting the parser does not support as a configuration failure, not a reason to continue.
  4. Keep parser libraries current. OWASP Dependency-Check reports known CVEs in the Java libraries your build pulls in.

Questions people ask

How to prevent xxe attack?

Reject DOCTYPE declarations in the XML parser. Where the application needs DTDs, disable external entity resolution and external DTD loading, and limit entity expansion. Keep XInclude off, and apply the same settings to schema validation and XSLT. The exact setting differs per language, as the Java, Python and .NET sections above show.

Can an XML file be malicious?

Yes, when the parser trusts what the file declares. A DOCTYPE can define entities that read local files or request URLs, deeply nested entities can exhaust memory, and XInclude can pull in other resources. The file needs no executable code. The parser does the work on the attacker’s behalf.

What is XXE protection?

XXE protection is parser configuration that rejects DTDs and external resources. In practice it means DOCTYPE rejected or external entity resolution turned off for every parser that handles untrusted input, plus parser libraries kept up to date. OWASP lists the setting for each language in its XXE Prevention Cheat Sheet.

How to fix XML external entity injection in Java?

Set disallow-doctype-decl and FEATURE_SECURE_PROCESSING on the factory before you create the parser. For a DocumentBuilderFactory that is two setFeature calls, shown in the both run above on OpenJDK 21.0.12.1. Other Java parsers need their own constructor settings, listed in the OWASP cheat sheet.

Runs made 2026-11-17 with OpenJDK 21.0.12.1, Python 3.9.25 and lxml 4.6.5. More in the Learn hub.

Sources, all read 2026-11-17:

Updated