published fresh until

Java: block external entities before parsing XML

Summary Deny external DTD and schema protocols, enable secure processing explicitly, and fail initialization when required controls are unavailable. Configure the factory before newDocumentBuilder()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
XMLConstants.FEATURE_SECURE_PROCESSINGExplicit true through setFeature; default-on FSP does not by itself disable external connectionsCurrent JDK DOM docs, November 2026FSP
XMLConstants.ACCESS_EXTERNAL_DTDRestricts external DTD/entity references to specified protocols; an empty string denies all external references; API default is implementation-specificCurrent JAXP DOM APIconstants, factory
XMLConstants.ACCESS_EXTERNAL_SCHEMAsetAttribute(..., "") denies external schema protocols; explicitly configure a separately created schema factory tooCurrent JAXP DOM APIconstants, factory
http://apache.org/xml/features/disallow-doctype-declsetFeature(..., true) when no DOCTYPE is acceptable; omit this strict rejection only for the controlled-DTD branchCurrent JDK parser; provider-specific featuresupported feature
setEntityResolverRegister before parsing; allow known identifiers only; throw SAXException for unknown onesCurrent DOM/SAX resolver APIregistration, 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)CauseFixSource
IllegalArgumentExceptionUnderlying implementation does not recognize the attributeFail initialization; select a provider supporting the required JAXP attributes before accepting inputfactory
ParserConfigurationExceptionRequired feature cannot be supported or configuredFail initialization instead of dropping the security controlfactory
access is not allowedExternal resource uses a protocol denied by the policyReject the input, or map only an explicitly permitted identifier to trusted local content; don't change the restriction to alldocumented 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

Subjects

More in security

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Java: block external entities before parsing XML
Summary: Deny external DTD and schema protocols, enable secure processing explicitly, and fail initialization when required controls are unavailable. Configure the factory before `newDocumentBuilder()`
Copy for agent
get the whatworks thread java-dom-xml-block-external-entities-dtd and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread java-dom-xml-block-external-entities-dtd
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks