Quick revision
The short answer
An XXE (XML External Entity) attack uses a DTD in the document to declare an entity whose value comes from a file or URL, such as <!ENTITY e SYSTEM "file:///etc/passwd">, and then references it as &e;, so a parser that resolves external entities inlines the file's content into the data our code reads. It can lead to file disclosure, server-side request forgery against internal services, and denial of service. The safest fix is setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) on DocumentBuilderFactory, which rejects any DOCTYPE; when DTDs are needed, we disable external entities and external DTD and schema access instead, and we configure every XML API separately.
What to remember
In this answer, we saw that an XXE attack declares an external entity in a DTD, such as one pointing at file:///etc/passwd, and references it so that the parser inlines the file into the data, leading to file disclosure, server-side request forgery and denial of service. The safest fix is the disallow-doctype-decl feature, which rejects any DOCTYPE; when DTDs are required we disable external general and parameter entities and external DTD loading, set XInclude off and entity reference expansion off, and set ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA to empty. SAX, StAX, Transformer, SchemaFactory and Validator each need their own settings.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
An XXE (XML External Entity) attack uses a DTD in the document to declare an entity whose value comes from a file or URL, such as <!ENTITY e SYSTEM "file:///etc/passwd">, and then references it as &e;, so a parser that resolves external entities inlines the file's content into the data our code reads. It can lead to file disclosure, server-side request forgery against internal services, and denial of service. The safest fix is setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) on DocumentBuilderFactory, which rejects any DOCTYPE; when DTDs are needed, we disable external entities and external DTD and schema access instead, and we configure every XML API separately.
Why this question is asked
XXE has been in the OWASP lists of serious web vulnerabilities for years, and Java is particularly exposed because the JDK's parser factories historically resolve entities by default for compatibility. A service that accepts XML uploads, SOAP requests or SAML assertions can leak its own files with a single crafted request. Interviewers ask this to check that we know how the attack works and how to configure parsers, not only the acronym. In this answer, we will recap DTDs and entities, walk through the attack and its impact, apply the safest fix to DocumentBuilderFactory, see the settings for when DTDs are required, cover the other XML APIs, and answer the follow-up about the billion laughs attack.
1A Quick Recap of DTDs and Entities
A DTD, Document Type Definition, is an old grammar language that can appear at the top of an XML document in a <!DOCTYPE> declaration. Besides describing structure, a DTD can declare entities, which are named pieces of content. An internal entity has its value written inline, <!ENTITY company "Acme Ltd">, and the document refers to it as &company;. The parser replaces each reference with the value.
An external entity gets its value from a URI instead: <!ENTITY e SYSTEM "file:///etc/passwd"> or SYSTEM "http://intranet/admin". A parser that resolves external entities opens that file or URL and inserts what it reads. This feature was designed for splitting large documents into files, and it is exactly what an attacker uses.
2How the Attack Works
Imagine an endpoint that accepts an order as XML and echoes the customer name in its response or in an error message. The attacker sends this document.
6 messages in order. Play them, or step through them one at a time.
Replace the file URI with http://internal-host/ and the same request becomes server-side request forgery.
Our code did nothing unusual. The parser, following the DTD, read /etc/passwd from our server and placed its content inside <customer>, and our response returned it. Even when the value is never echoed, attackers can extract data out of band, for example with parameter entities that send file content to their own server in a URL, or through error messages that include the entity value.
3The Impact
There are three main kinds of damage. File disclosure: configuration files, keys or credentials readable by the service's user are returned to the attacker. Server-side request forgery, SSRF: with an http:// URI, the parser makes requests from inside our network, reaching admin consoles, internal APIs or cloud metadata endpoints that are not exposed to the internet. Denial of service: an entity pointing at a device that never ends, or entities that expand exponentially, which is the billion laughs attack, can exhaust the CPU or memory of the service.
Because the parser runs with the service's own permissions and network position, XXE turns a harmless-looking upload into access to whatever the service can reach.
4The Safest Fix: Reject DOCTYPE Entirely
Most applications never need a DTD in the documents they receive. The safest configuration is to reject any document that contains a DOCTYPE declaration. With the JDK's built-in Xerces-based parser, this feature does exactly that.
The attack document now fails before any entity is declared, and normal documents without a DOCTYPE parse as before. It protects against file disclosure, SSRF and entity expansion together, which is why it is the first recommendation. setXIncludeAware(false) also stops XInclude, another way to pull in external files, which is off by default but worth stating.
5When DTDs Are Required
Some formats really do carry a DOCTYPE, for example older industry formats or documents with internal entities for special characters. Then we allow the DOCTYPE but block everything external.
Each line closes one door: external general entities (the &e; in our attack), external parameter entities (used for out-of-band extraction), loading an external DTD file, and fetching DTDs or schemas over any protocol. Internal entities still work, and secure processing applies limits that defend against entity expansion.
6Every API Needs Its Own Settings
The settings above apply only to DocumentBuilderFactory. Each XML API in the JDK has its own factory and its own defaults, so we must secure each one we use.
| API | Key settings |
|---|---|
| SAXParserFactory | disallow-doctype-decl true, or external entities off |
| XMLInputFactory (StAX) | SUPPORT_DTD false, IS_SUPPORTING_EXTERNAL_ENTITIES false |
TransformerFactory | ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_STYLESHEET set to empty |
SchemaFactory | ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA set to empty |
| Validator | ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA set to empty |
| Libraries (JDOM2, dom4j, Jackson XML) | current version, plus their own secure options |
The practical way to get this right everywhere is a small shared class that creates configured factories, and a test that sends the attack document to every endpoint and parser entry point and expects a rejection.
7Follow-up questions
What is the billion laughs attack?
It is an entity expansion attack that needs no external access. The DTD declares an entity lol with the text lol, then lol1 as ten references to lol, lol2 as ten references to lol1, and so on up to lol9. A single &lol9; in the document expands to ten to the ninth copies of the text, about 3 GB, from a document of under 1 KB. The parser runs out of memory or CPU. Rejecting DOCTYPE stops it completely; otherwise FEATURE_SECURE_PROCESSING enforces the JDK's entity expansion limits, whose exact values depend on the JDK version and can be tuned with jdk.xml system properties.
Is XXE still a problem on modern JDKs?
Yes, by default. For compatibility, the JDK factories still allow DTDs and many entity features unless we change them, although recent releases tighten some limits. Check your version's XML configuration documentation, but configure parsers explicitly either way.
Does validating against an XSD protect us?
No. Entity resolution happens while parsing, before or during validation, so the file has already been read. The validator itself also needs external access restricted, or a schema reference can become an SSRF vector.
8Conclusion
In this answer, we saw that an XXE attack declares an external entity in a DTD, such as one pointing at file:///etc/passwd, and references it so that the parser inlines the file into the data, leading to file disclosure, server-side request forgery and denial of service. The safest fix is the disallow-doctype-decl feature, which rejects any DOCTYPE; when DTDs are required we disable external general and parameter entities and external DTD loading, set XInclude off and entity reference expansion off, and set ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA to empty. SAX, StAX, Transformer, SchemaFactory and Validator each need their own settings.
Open with
XXE stands for XML External Entity. XML documents can contain a DOCTYPE with a DTD, and a DTD can declare entities. An external entity takes its value from a URI, so an attacker sends something like DOCTYPE x with ENTITY e SYSTEM file:///etc/passwd, and then uses ampersand e semicolon in an element.
Then, if they want more
If the parser resolves external entities, which the JDK factories historically do by default, it reads the file and puts its content into the element, and if our service echoes that value or puts it in an error, the attacker gets the file.
With an http URI, the same trick makes our server call internal services, which is server-side request forgery, and entity tricks can also cause denial of service. The safest fix on DocumentBuilderFactory is setFeature with http://apache.org/xml/features/disallow-doctype-decl set to true, which rejects any document with a DOCTYPE, and most applications never need one.
If DTDs are needed, I disable external general and parameter entities and loading external DTDs, turn off XInclude and entity reference expansion, and set ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA to the empty string. The important point is that every API has its own settings, SAX, StAX, Transformer, SchemaFactory and Validator, so I put them in one shared factory class.
The billion laughs attack is nested entities that expand exponentially from a tiny document; rejecting DOCTYPE stops it too.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What is an XXE attack? Show the kind of input that exploits it and how to configure...": https://scrivio-interview-library.netlify.app/java/xml-processing/703c2ca667/