Disable document type definitions in every XML parser that handles untrusted input. OWASP's main advice is to turn DTDs off whenever possible. If you need them, disable external entity resolution and external DTD loading, and limit entity expansion. If a parser cannot apply a safety setting, treat that as a configuration failure.
What XXE does
XXE happens when an XML processor resolves an external entity from untrusted input. OWASP lists the impact as exposing local files, enabling server side request forgery, or exhausting resources. It also notes that stopping XXE does not stop every XML denial of service attack, so entity expansion needs its own limits.
Language checklist
These notes come from the OWASP XXE cheat sheet. Check the current page for exact code before you copy settings.
Java.
- For
DocumentBuilderFactoryandSAXParserFactory, set thedisallow-doctype-declfeature and enable secure processing. - For StAX
XMLInputFactory, setSUPPORT_DTDandIS_SUPPORTING_EXTERNAL_ENTITIESto false and install a resolver that rejects external references. - For
TransformerFactoryandSchemaFactory, restrict theACCESS_EXTERNAL_*properties before use. - Do not use
java.beans.XMLDecoderon untrusted input. - Note that
setExpandEntityReferences(false)is not a substitute.
.NET.
XmlDocumenthas a null resolver by default from .NET Framework 4.5.2.XmlTextReaderis unsafe by default before 4.5.2. SetDtdProcessingtoProhibit.- In ASP.NET, the
httpRuntime targetFrameworkin Web.config must be 4.5.2 or later.
Python. Use the defusedxml interfaces for untrusted XML and forbid DTDs where unneeded. OWASP also says Expat versions below 2.7.2 may have denial of service issues, and your Python version alone does not tell you the Expat version.
PHP. Avoid LIBXML_NOENT, LIBXML_DTDLOAD and LIBXML_DTDVALID on untrusted XML unless external resources are blocked.
Places teams forget
- Schema validation and style sheet processing. OWASP says to apply external access restrictions there as well, not only to parsing.
- XInclude. Keep it disabled unless needed.
- Input size. Limit input and decompressed size. OWASP notes the standard library
xmlrpcmodule is exposed to decompression bombs. - Parameters as file paths or URLs. Pass input as streams, not untrusted filenames or URLs.
- Formats built on XML. Many document, image and message formats are XML under the hood, so any parser that opens them is in scope. The OWASP page does not detail these, so confirm the parser settings for each pipeline.
Finding gaps
- Use code search for each parser class named above and check the surrounding configuration.
- Add a static analysis rule for factories without the DTD setting. OWASP mentions Semgrep rules for Java factories.
- Review your dependencies, because a library may parse XML on your behalf with default settings. An SBOM helps you see which XML libraries and versions you ship.
Test it
Send a small XML document that declares a DOCTYPE with an external entity pointing to a harmless local test file, in a non production environment. A hardened parser should reject the DOCTYPE or refuse to resolve the entity. If the file contents appear in the response or an error, fix the parser.
Quick check
Is it enough to switch to JSON for new APIs?
It removes XXE from those APIs. Legacy endpoints, file imports and SOAP style services still need the hardening above.
Sources
Last reviewed: 19 October 2026.