Practical technical guides / Core IT: security (XML external entities in Java)

Java XML Parsers and External Entities: What JDK 23 Resolves by Default and the Settings That Stop It

On JDK 23.0.2 DOM, SAX, StAX and Transformer parsers all read a local file through an external entity by default. Measured settings that stop it, and a test to keep them.

By Sandeep Belgavi · Published 2026-11-10

On JDK 23.0.2 DOM, SAX, StAX and Transformer parsers all read a local file through an external entity by default. Measured settings that stop it, and a test to keep them.

A supplier feed that can read your files

Belgavi's Book Store receives catalogue updates from publishers as XML: titles, ISBNs, prices, stock dates. Hansini maintains the Java service that imports them. One feed arrives with a header nobody asked for:

<?xml version="1.0"?>
<!DOCTYPE note [ <!ENTITY ext SYSTEM "file:///etc/catalogue/import.properties"> ]>
<note>&ext;</note>

The DOCTYPE declares an external entity named ext whose content is a local file, and the document body references it. A parser that resolves external entities replaces &ext; with the file's content. If the importer stores the parsed text, logs it, or echoes it back in an error message, the file leaves the server. This is XML external entity injection (XXE), CWE-611, and in Java it is a configuration problem: the standard factories resolve external entities unless you tell them not to.

This guide measures what the JDK's four XML APIs do with that document, on one JDK, with each common setting, and ends with a factory helper and a test that fails when someone creates a parser without it.

What was measured

The companion probe writes a synthetic file containing the marker SYNTHETIC-SECRET-CASE-1042 to a temporary directory, builds the document above pointing at that file, and parses it with DocumentBuilderFactory (DOM), SAXParserFactory (SAX), XMLInputFactory (StAX) and TransformerFactory (an identity transform). An outcome of LEAKED means the marker appeared in the result. The recording was made on JDK 23.0.2 on Windows, with no system properties set; other vendors and versions may behave differently, so the probe is meant to be run on yours.

API and settingJDK 23.0.2 result
DocumentBuilderFactory, defaultsLEAKED
DocumentBuilderFactory, FEATURE_SECURE_PROCESSING set explicitlyRejected: file access not allowed by accessExternalDTD
DocumentBuilderFactory, ACCESS_EXTERNAL_DTD set to the empty stringRejected: same message
DocumentBuilderFactory, disallow-doctype-declRejected: DOCTYPE is disallowed
DocumentBuilderFactory, external general and parameter entities offParsed, entity not expanded
SAXParserFactory, defaultsLEAKED
SAXParserFactory, disallow-doctype-declRejected: DOCTYPE is disallowed
XMLInputFactory, defaultsLEAKED
XMLInputFactory, SUPPORT_DTD falseRejected: entity referenced but not declared
XMLInputFactory, IS_SUPPORTING_EXTERNAL_ENTITIES falseParsed, entity not expanded
TransformerFactory, defaultsLEAKED
TransformerFactory, ACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_STYLESHEET emptyRejected: file access not allowed

Every API leaks with its defaults on this build: resolving external entities is the default behaviour, and restricting it is left to the application or to process-wide settings. Every API also has at least one setting that stops this attack; they differ in whether the document is rejected or parsed without the entity.

Two system-wide switches in recent JDKs

JDK 22 added the jdk.xml.dtd.support property, with values allow, ignore and deny, that applies to all XML processors. JDK 23 added a template configuration file, $JAVA_HOME/conf/jaxp-strict.properties.template, and its release note says future releases will keep moving XML processing toward more restrictive defaults. The probe was run twice more on JDK 23.0.2 to see what each does.

  • With -Djdk.xml.dtd.support=deny, every API, with every setting, rejected the document: "DOCTYPE is disallowed when the DTD property is set to deny".
  • With -Djava.xml.config.file pointing at the strict template, every API that would have resolved the entity rejected it, here through the JDK's built-in catalog ("No match found" for the external reference). The two settings that already switched entity expansion off still parsed the document without the entity.

The release notes for JDK 24 through JDK 27 that were checked for this article list no change to these defaults. JDK 26 extended the external access properties to XPath. Check the release notes of the JDK you deploy; this is an area the JDK team has said it will keep tightening.

Which setting to choose

For a service that accepts XML from outside, there is rarely a reason to process a DOCTYPE at all. Publisher feeds, partner APIs and uploaded files need elements and attributes, not entity declarations. Rejecting any DOCTYPE is the strongest and simplest rule.

GoalDOM and SAXStAXTransformer
Reject any DOCTYPEfeature disallow-doctype-declSUPPORT_DTD falseparse to DOM first with a hardened builder
Allow DTDs, never fetch externalsACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_SCHEMA emptyIS_SUPPORTING_EXTERNAL_ENTITIES falseACCESS_EXTERNAL_DTD and ACCESS_EXTERNAL_STYLESHEET empty
Process-wide backstopjdk.xml.dtd.support=denysamesame

Two details from the measurement matter in practice. Setting FEATURE_SECURE_PROCESSING explicitly did block the file read on JDK 23, because the java.xml module documentation says that properties not set explicitly take more restrictive values when FEATURE_SECURE_PROCESSING is enabled; relying on that alone couples your safety to a documented but indirect rule. And the "parsed, entity not expanded" outcomes mean the document was accepted with an unresolved reference. If your importer then validates the result, it should treat a missing value as an error.

A factory helper and a test

Centralise parser creation so that a new import job cannot forget the settings.

public final class SafeXml {
    private SafeXml() {}

    public static DocumentBuilderFactory documentBuilderFactory() throws ParserConfigurationException {
        DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
        f.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
        f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        f.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        f.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        f.setXIncludeAware(false);
        f.setExpandEntityReferences(false);
        return f;
    }

    public static XMLInputFactory xmlInputFactory() {
        XMLInputFactory f = XMLInputFactory.newInstance();
        f.setProperty(XMLInputFactory.SUPPORT_DTD, false);
        f.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
        return f;
    }
}

Then make it hard to bypass. A unit test that parses the probe document through every factory your code uses, and asserts that each call throws or returns no marker, catches a regression in your own code. A build rule that forbids direct calls to DocumentBuilderFactory.newInstance() outside the helper (static analysis tools such as Semgrep, SpotBugs with Find Security Bugs, or a simple ArchUnit rule can do this) catches new code that never reaches the test.

Run the JVM with -Djdk.xml.dtd.support=deny as well, if nothing in the process needs DTDs. It protects code you do not own, such as libraries that create their own parsers, and the probe shows it applies to all four APIs on JDK 23.

Where XML enters a service you did not think parsed XML

The catalogue importer is the obvious XML endpoint. Most services have others that are easy to miss when someone audits parser settings:

  • Office documents. DOCX and XLSX files are ZIP archives of XML parts. A help-desk tool that reads an uploaded spreadsheet of returns is parsing XML from a customer.
  • SVG images. An SVG uploaded as a book cover is an XML document; server-side resizing or validation may parse it.
  • Feeds and sitemaps. RSS and Atom feeds from publishers, and sitemaps fetched by a crawler, arrive as XML from outside.
  • Federation and legacy integrations. SAML assertions and SOAP services are XML at the protocol level, usually parsed by a library rather than your own code.

Each of these paths needs the same treatment: find the parser that actually runs, and apply the settings above or confirm the library already does.

Failure modes

  • Third-party libraries create parsers. SOAP stacks, office-document readers and configuration loaders often call the factories themselves. The system property is the backstop here, after testing that the library does not need DTDs.
  • Factory lookup returns a different implementation. If an older Xerces or Woodstox jar is on the class path, newInstance() may return it, with its own defaults and feature names. Log the factory class at startup.
  • Errors echo the input. Even a rejected document can leak if your error handler logs the whole payload to a place an attacker can read. Log the rule that failed, not the body.
  • Billion laughs and other entity expansion. Internal entities can also be abused to exhaust memory. Disallowing DOCTYPEs removes that class too; if you must allow DTDs, keep the JDK's expansion limits (jdk.xml.entityExpansionLimit is 64,000 by default) and do not raise them.

Verification

  • Run the companion probe (sh run.sh) on the exact JDK build in your image and compare with the recording. A line that changes from LEAKED to rejected after an upgrade is good news; the opposite is a regression to investigate.
  • Add the probe document to your importer's integration tests and assert the stored record contains no file content.
  • Search the code base for every newInstance() call on the four factories and route them through one helper.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/java/xml_external_entity_probe. Views are my own and do not represent any employer, past or present.