Can one DOCTYPE line in XML read your server's files? Closing XXE
XXE happens when your XML parser resolves an external entity from a file the user sent, so it reads a file from the server and puts it in the reply. OWASP's advice: disable DTDs in the parser whenever possible.

A small XML file, like an order or an invoice a customer sends to your system, has one extra line at the top. That line asks your server to open a file from the disk and put its contents into the reply. This article shows you how that happens, and how to close it with one setting in the parser.
What is XXE?
XXE is a flaw that happens when an XML parser resolves an "external entity" that came from user input. An external entity is like a shortcut that tells the parser "replace this word with the contents of this file".
The OWASP XXE prevention cheat sheet defines it this way: it happens when an XML processor resolves an external entity from untrusted input.
What does the attack look like?
The attack is a normal XML file that starts with a DOCTYPE definition holding an entity that points at a file on the server, and the body uses that entity's name. The parser sees the name and replaces it with the file's contents.
<!DOCTYPE order [
<!ENTITY secret SYSTEM "file:///etc/passwd">
]>
<order><note>&secret;</note></order>If your app returns the note field in the reply, or saves it and shows it later, the attacker reads the file. It is the same result a file path traversal flaw gives, through another door.
Why does the parser do this at all?
The parser does this because the entity is written in the file itself, and it reads the file as it was sent. The OWASP cheat sheet says controls and defaults vary by parser, so you can't assume yours is closed on its own. So the code that reads the file has no visible mistake: the dangerous line comes from the file, and the parser does what it was asked.
That is what makes the fix simple: if the parser never accepts a DTD, there is nowhere to define the entity, and the whole attack falls apart.
How do you close it in your code?
You close it by turning off DTDs in the XML parser wherever you read a file that comes from outside. The OWASP cheat sheet says to disable DTDs whenever possible.
Search your code for every place that reads XML: API endpoints, file uploads, import libraries.
In Java with DocumentBuilderFactory: turn on the
disallow-doctype-declfeature andFEATURE_SECURE_PROCESSING.In Python: for untrusted XML, use the
defusedxmlparsing interfaces in place of the standard-library functions.In PHP: from libxml 2.9.0 on, entity substitution is off by default, so check your version and don't turn it back on.
Test it: send the file above to your endpoint. If you get a "DOCTYPE not allowed" error or the request is rejected, you're fine.
And if your app accepts files from users in general, also see the article on validating file uploads.
Where do you start now?
The lesson for us as developers: a file the user sends can carry orders for the parser, not just data, so the parser should only listen to what you allow. Open the first place in your code that reads XML today, and turn off DTDs there.
If you want someone to look for flaws like this in your app before anyone else does, there is Muhassan. Where does XML enter your system, and do you know which parser reads it?
FAQ
My app doesn't use XML. Should I care?
Look before you decide. Any library that reads XML-based file formats, or any old API endpoint that accepts XML, has an XML parser in it even if you didn't write one.
If I strip the word ENTITY from the input, is that enough?
No. Word filters break easily. The fix is that the parser itself refuses DTDs, so there is nowhere for the entity to be defined.
Do I need to do anything in PHP?
According to OWASP, from libxml 2.9.0 on entity substitution is off by default. Make sure your version is that or newer, and that your code doesn't turn it back on.


