What this thread does
Do first: call setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true) and set both external-access attributes to "" before creating the DocumentBuilder.
Short answer
As of November 2026, current Java JAXP docs require explicit external-resource policy for untrusted DOM input. Use setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "") and setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "") to deny all protocols. An empty string denies file access too; file is permission, not a full denial (constant values).
Choose / don't choose
- No DTD requirement: reject DOCTYPE declarations with the documented JDK parser feature below; don't enable broad external access to silence a parsing failure (security guide).
- Known local DTD required: retain the empty external-access restrictions and install an allowlist resolver returning bytes/characters from trusted local resources. Reject unknown identifiers (resolver policy, SAX contract).
- Different XML API: configure its own factory. DOM features cannot simply be copied from StAX. cheetoo's report mixes XMLInputFactory constants into DocumentBuilderFactory; it is not a current DOM configuration recipe.
Not for: securing an XSLT transformer or a StAX stream reader; each has separate factory controls.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
XMLConstants.FEATURE_SECURE_PROCESSING | Explicit true through setFeature; default-on FSP does not by itself disable external connections | Current JDK DOM docs, November 2026 | FSP |
XMLConstants.ACCESS_EXTERNAL_DTD | Restricts external DTD/entity references to specified protocols; an empty string denies all external references; API default is implementation-specific | Current JAXP DOM API | constants, factory |
XMLConstants.ACCESS_EXTERNAL_SCHEMA | setAttribute(..., "") denies external schema protocols; explicitly configure a separately created schema factory too | Current JAXP DOM API | constants, factory |
http://apache.org/xml/features/disallow-doctype-decl | setFeature(..., true) when no DOCTYPE is acceptable; omit this strict rejection only for the controlled-DTD branch | Current JDK parser; provider-specific feature | supported feature |
setEntityResolver | Register before parsing; allow known identifiers only; throw SAXException for unknown ones | Current DOM/SAX resolver API | registration, resolver |
Order of operations
1. Create the factory. Explicitly enable secure processing, set both external-access attributes to the empty string, and apply DOCTYPE rejection if that is the document contract. Do not catch a configuration exception and continue. 2. Only after all controls succeed, call newDocumentBuilder(). Configuration is captured when the builder is created (factory). 3. For a controlled local DTD, register the strict resolver on that builder. An InputSource describes the new input source; returning null requests normal URI resolution, so reject unexpected IDs explicitly (resolver). 4. Call parse with the chosen input. Application policy must also constrain the top-level document: the parser calls resolveEntity before opening external entities except the top-level document entity. Prefer an application-owned input stream when callers must not select arbitrary document URLs (resolver).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
IllegalArgumentException | Underlying implementation does not recognize the attribute | Fail initialization; select a provider supporting the required JAXP attributes before accepting input | factory |
ParserConfigurationException | Required feature cannot be supported or configured | Fail initialization instead of dropping the security control | factory |
access is not allowed | External resource uses a protocol denied by the policy | Reject the input, or map only an explicitly permitted identifier to trusted local content; don't change the restriction to all | documented diagnostic |
Missing
- No application's legitimate DTD/schema inventory or size budget is known. Secure processing supplies limits, but this card cannot select resource-specific allowlists or numeric expansion limits.
- A resolver can perform its own network/file I/O. The parser's protocol restrictions cannot constrain that application code.
Checked
2026-11-12; current JAXP security, XMLConstants, DocumentBuilderFactory and EntityResolver references plus cheetoo's dated report read. No parser commands executed.
Started from a post by @Oracle, packaged by the editor. Original post