XXE Parser Defaults and Hardening

Per-language XML parser behavior: which libraries resolve external entities by default, which don’t, and the exact settings that close the gap — because in XXE the dangerous property is a default, not a call site.

Why defaults decide everything

XXE is “entirely a function of which option bits are set”; higher-level bindings differ only in which bits they set for you (XXE as a DAST Finding). Measured against current versions, most parsers at their defaults parse a DOCTYPE and expand internal entities but refuse external resolution — which is why a finding built only on “DOCTYPE processed” is a common false positive, and why the exact library version must be cited. Two anti-patterns recur: a flag flipped on by a wrapper, and a parser whose “harden” call covers only part of the surface.

Java

The highest-risk ecosystem — rich parser zoo, verbose error strings, and many wrappers. Core JAXP controls (OWASP XXE Prevention Cheat Sheet, Parsers Should Not Fetch Files):

  • DocumentBuilderFactory / SAXParserFactory: set http://apache.org/xml/features/disallow-doctype-decl = true and enable FEATURE_SECURE_PROCESSING; keep XInclude off. If a DTD is required, disable external general entities, external parameter entities, and load-external-dtd.
  • XMLInputFactory (StAX): SUPPORT_DTD = false and IS_SUPPORTING_EXTERNAL_ENTITIES = false.
  • TransformerFactory / SchemaFactory / Validator: set ACCESS_EXTERNAL_DTD, ACCESS_EXTERNAL_SCHEMA, ACCESS_EXTERNAL_STYLESHEET to empty string — schema validation and XSLT resolve entities too. JVM-wide, the matching javax.xml.accessExternalDTD/...Schema/...Stylesheet system properties were the recommended Struts CVE-2025-68493 mitigation (Apache Struts XWork XXE).
  • Wrappers replace your resolver: dom4j (SAXReader), JDOM (SAXBuilder), commons-digester (Digester) all swap the underlying reader’s entity resolver — harden through the wrapper’s own API, not the raw reader.
  • setExpandEntityReferences(false) is not a fix — on JDOM2 2.0.6.1 it only disables external general entities, leaving parameter entities and external DTD subsets live (Apache Roller CVE-2026-105153) (Apache Roller AtomPub XXE).
  • Never feed untrusted XML to java.beans.XMLDecoder — blocking entities doesn’t remove its deserialization risk (OWASP XXE Prevention Cheat Sheet).
  • Per-path parser construction. Apache Camel CVE-2026-85250 (CWE-611, fixed 4.18.5 / 4.22.x; 4.14.x EOL unfixed): with documentType=InputSource, XPathBuilder handed the payload to javax.xml.xpath, which built its own DocumentBuilder with JDK defaults, so a DOCTYPE refused on Camel’s hardened default document type was accepted and external entities resolved. Choosing InputSource for streaming silently chose a weaker parser (Apache Camel CVE-2026-85250).
  • Validation is not hardening. PMD’s TestSchemaParser.newDocumentBuilder enabled schema validation but never disabled DTDs or external entities; a crafted rule-tests XML read a local file into the test description, which became a JUnit dynamic test name and leaked through CI build logs (GHSA-9hm5-r2r7-hjcq) (PMD rule-test XXE).
  • JAXB unmarshalling resolves external entities unless fed a hardened StAX reader — IBM ApplinX (CVE-2026-92877), Eclipse AAS4J (CVE-2026-127031) (Eclipse AAS4J XmlSchemaValidator XXE).

.NET

Safety is version-gated at 4.5.2:

  • XmlDocument, XmlTextReader, XPathNavigator are unsafe before 4.5.2 (their XmlResolver must be set to null); safe by default at ≥4.5.2.
  • XmlReader/XmlDictionaryReader/XmlNodeReader and XDocument/LINQ-to-XML are safe by default, and at ≥4.5.2 won’t even process an inline DTD unless you explicitly re-enable it.
  • ASP.NET must also set <httpRuntime targetFramework="4.5.2+"/> — omitting the tag reverts to unsafe-by-default regardless of the actual framework (OWASP XXE Prevention Cheat Sheet). Legacy .NET defaults are exactly why OOXML upload parsers are XXE sinks (XXE Hidden in Cell A2).

PHP / libxml2

Since libxml 2.9.0, entity substitution is off by default (OWASP XXE Prevention Cheat Sheet).

  • Do not pass LIBXML_NOENT, LIBXML_DTDLOAD, or LIBXML_DTDVALID on untrusted XML — any of these re-enables the class (Symfony XmlUtils::parse() validateOnParse=true, CVE-2026-45071, enabled DTD validation and let file:// entities resolve before doctype rejection) (Symfony Config XmlUtils::parse XXE).
  • In libxml2 directly, ensure XML_PARSE_NOENT and XML_PARSE_DTDLOAD are unset. The substitute-entities bit alone enables general-entity file reads; DTD-load adds external DTDs.
  • To reject external entities, install a libxml_set_external_entity_loader() callback that returns null — passing null directly is not equivalent and does not block loading (per PHP’s own advisory) (OWASP XXE Prevention Cheat Sheet).
  • Internal entity expansion (quadratic/billion-laughs DoS) is not blocked by 2.9+ defaults — bound expansion separately (PhpSpreadsheet ISO-2022 Bypass).

Python

Built-in parsers use Expat, which doesn’t access files/network by default (OWASP XXE Prevention Cheat Sheet). Measured defaults (XXE as a DAST Finding):

  • xml.etree.ElementTree, minidom, xml.sax expand internal entities, resolve no external entity by default. A SAX finding hinges on whether feature_external_ges was turned on (that one switch enables file read); feature_external_pes can’t even be enabled on Expat.
  • lxml: from version 5.0 the default is resolve_entities='internal' (was True before 5.0 — pre-5.0 code reads external entities by default). That 5.0 change covered only the normal XML and HTML parsers: iterparse() and ETCompatXMLParser() kept resolve_entities=True until lxml 6.1, so default use of either read local files (GHSA-vfmq-68hx-4jfw); on older versions pass resolve_entities='internal' or False explicitly (lxml iterparse XXE). The default still loads local external parameter entities (a DTD fragment on disk), and network access depends on whether the linked libxml2 was built with HTTP support — cite the measured LIBXML_FEATURES. Set resolve_entities=False, no_network=True, DTD loading off; etree.iterparse() insecurely configured was CVE-2025-6984 in langchain-community (langchain-community XXE). lxml’s default parser read Tandoor Recipes’ .env via an XML importer (Tandoor Recipes lxml XXE).
  • Use defusedxml for untrusted XML; set forbid_dtd=True where available. Keep input size and decompressed size bounded independently (xmlrpc is decompression-bomb-prone). XInclude is a separate file-read path — don’t run the XInclude step on untrusted input.

Node.js, Ruby, C/C++

  • Node: sax 1.4.1 (strict) and fast-xml-parser 5.2.5 do not resolve external entities by default (“External entities are not supported”) — but this is “not by default here,” not a guarantee for every Node XML library (libxmljs wraps libxml2 and can be configured unsafely) (XXE as a DAST Finding).
  • Ruby/Nokogiri: default parse options are safe, but enabling DTDLOAD/DTDVALID/NOENT on untrusted XML is dangerous; on JRuby the NONET option was bypassed for short-form IPv4 hosts (http://127.1/) and absolute paths — SSRF to external DTDs/entities (GHSA-v995-44v6-5fqm, fixed Nokogiri 1.19.5) (JRuby NONET Bypass).
  • C/C++: libxml2 — don’t set XML_PARSE_NOENT/XML_PARSE_DTDLOAD; Xerces-C++ — setDisableDefaultEntityResolution(true) or XMLUni::fgXercesDisableDefaultEntityResolution (OWASP XXE Prevention Cheat Sheet).

Cross-cutting principles

  • Fail closed — if a parser doesn’t support a security setting, stop rather than parse partly hardened (Parsers Should Not Fetch Files).
  • Same hardened factory everywhere — one forgotten DocumentBuilder in a utility class undoes the rest, and so does a library that builds its own parser on an alternate code path (Camel XPath with InputSource).
  • Dev tooling parses untrusted XML too — test-fixture XML from a pull request runs in CI with access to runner secrets (PMD).
  • Parse before auth is the worst case — SAML/SOAP/provisioning parsers that run before permission checks turn any default-on parser into a pre-auth bug (Apache Roller AtomPub XXE).
  • SAST caveat — model XXE as parser construction/use with DTD or external-entity options enabled on untrusted data; crediting a denylist “sanitizer” it doesn’t actually protect is the classic false result (XXE as a DAST Finding).

Sources