Skip to content

Make XML parsing and validation resilient to outdated XML parsers on the classpath#894

Open
patbaumgartner wants to merge 1 commit into
CycloneDX:masterfrom
patbaumgartner:xml-schema-factory-xerces-resilience
Open

Make XML parsing and validation resilient to outdated XML parsers on the classpath#894
patbaumgartner wants to merge 1 commit into
CycloneDX:masterfrom
patbaumgartner:xml-schema-factory-xerces-resilience

Conversation

@patbaumgartner

Copy link
Copy Markdown
Contributor

Related to CycloneDX/cyclonedx-gradle-plugin#349

Problem

When an outdated XML parser such as Xerces 2.x leaks onto the classpath (pulled in transitively by another library or Gradle plugin), the JAXP lookup mechanism (SchemaFactory.newInstance / DocumentBuilderFactory.newInstance) picks it up, and BOM parsing/validation fails hard:

SAXNotRecognizedException: Property 'http://javax.xml.XMLConstants/property/accessExternalDTD' is not recognized.

These parsers pre-date the JAXP 1.5 secure-processing properties. This is what breaks cyclonedxBom in CycloneDX/cyclonedx-gradle-plugin#349 whenever any other plugin ships Xerces on the buildscript classpath - reproducible with a plain Gradle 8.4 project, the current gradle plugin 3.3.0, and xerces:xercesImpl:2.12.2 added to the buildscript classpath.

Fix

  • new XmlFactoryUtils prefers the JDK's built-in SchemaFactory / DocumentBuilderFactory via newDefaultInstance(), bypassing the classpath-based JAXP lookup entirely. newDefaultInstance() only exists since Java 9 while this library targets Java 8, so it is invoked reflectively with a fallback to the standard lookup
  • used in the three affected sites: CycloneDxSchema.getXmlSchema, XmlParser.createSecureDocument, BomXmlGenerator.buildSecureDocumentBuilder
  • the ACCESS_EXTERNAL_DTD/ACCESS_EXTERNAL_SCHEMA hardening properties are additionally treated as best-effort for exotic factory implementations reached through the fallback path (secure processing remains enforced, and only local schema copies are resolved)

Regression coverage

xerces:xercesImpl:2.12.2 is added to the test classpath. Before this change, this makes all 51 XmlParserTest cases fail with the exact error above; with the change, the full suite (1614 tests) passes. This guards the library against classpath poisoning by construction.

Verification

  • mvn clean verify green (1614 tests, 0 failures)
  • reproducer for the gradle plugin scenario: Gradle 8.4 + plugin 3.3.0 + xercesImpl on the buildscript classpath fails with Error whilst validating XML BOM / SAXNotRecognizedException today; with this fix in core-java, validation uses the JDK parser and is unaffected

…the classpath

When an outdated XML parser such as Xerces 2.x leaks onto the classpath
(pulled in transitively by another library), the JAXP lookup mechanism
picks it up and BOM parsing/validation fails with errors like:

  SAXNotRecognizedException: Property
  'http://javax.xml.XMLConstants/property/accessExternalDTD' is not
  recognized.

because such parsers pre-date the JAXP 1.5 secure-processing
properties. Reported against the Gradle plugin in
CycloneDX/cyclonedx-gradle-plugin#349, where any plugin leaking Xerces
onto the buildscript classpath breaks BOM generation.

- introduce XmlFactoryUtils that prefers the JDK built-in
  SchemaFactory/DocumentBuilderFactory (newDefaultInstance, invoked
  reflectively for Java 8 compatibility) over the classpath-based
  JAXP lookup
- use it in CycloneDxSchema, XmlParser and BomXmlGenerator
- treat the ACCESS_EXTERNAL_DTD/SCHEMA hardening properties as
  best-effort for exotic factory implementations
- add xercesImpl to the test classpath as regression coverage: before
  this change, all XmlParserTest cases fail with the error above

Signed-off-by: Patrick Baumgartner <contact@patbaumgartner.com>
@patbaumgartner
patbaumgartner requested a review from a team as a code owner July 24, 2026 20:31
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 5 complexity

Metric Results
Complexity 5

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant