Conversation
Replace the hand-written XML parser configuration at each call site with factories from Apache Commons Secure XML, which ignore external resources independently of the JAXP implementation: - DOM, TrAX, XPath and schema validation use the Secure* factories. - StAX readers use SecureXMLInputFactory. - JAXB unmarshalling goes through new XmlUtils.unmarshal helpers, which read from an XMLEventReader created by SecureXMLInputFactory. - Commons Digester receives an XMLReader from SecureSAXParserFactory and a resolver that leaves external references to the library. - TopologyMarshaller selects the XML or JSON reader by exact media type. A new forbidden-apis execution rejects the plain JAXP factory methods, the raw-input Unmarshaller.unmarshal overloads and the Digester constructors that create their own parser in main code. Assisted-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Test Results 4 files 4 suites 41s ⏱️ Results for commit 3ef1b8a. ♻️ This comment has been updated with latest results. |
The factory is configured once and never modified, so a single instance can be shared between threads. It is created lazily in a holder class, so a failure to create it does not affect the other XmlUtils helpers. Assisted-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Assisted-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
KNOX-3484 - Use Apache Commons Secure XML for all JAXP factories
What changes were proposed in this pull request?
Knox configured its XML parsers by hand: each call site set its own subset of features and properties, and some relied on the defaults of whichever implementation (JDK, Woodstox, MOXy, Glassfish JAXB, Commons Digester) served them. This PR replaces those ad hoc settings with one dependency, Apache Commons Secure XML 1.0.0, whose factories are hardened as delivered: external resources (DTDs, external entities, XInclude, schemas, stylesheets) are ignored, independently of the JAXP implementation.
This is a hardening and maintenance change: the PMC no longer has to configure XML parsers.
DOM, TrAX, XPath, Validation:
XmlUtils,UrlRewriteRulesDescriptorAdapter,XmlFilterReader,ServiceURLPropertyConfig,TopologyValidatorandTopologyToDescriptorobtain their factories fromSecureDocumentBuilderFactory,SecureTransformerFactory,SecureXPathFactoryandSecureSchemaFactory. The hand-written feature settings are removed.StAX:
XmlFilterReaderusesSecureXMLInputFactory.JAXB:
ServiceDefinitionUnmarshaller,TopologyMarshaller(XML bodies),ServiceDefinitionsLoader,ApplicationDeploymentContributor,TopologyToDescriptor,RemoteConfigurationRegistriesParserandProviderConfigurationParserunmarshal through two new helpers,XmlUtils.unmarshal(Unmarshaller, Class<T>, File)andXmlUtils.unmarshal(Unmarshaller, Class<T>, String, InputStream), which unmarshal from anXMLEventReadercreated bySecureXMLInputFactory. This follows the StAX approach documented for JAXB in Document JAXB commons-secure-xml#103, and makes the result independent of the JAXB provider serving each context.TopologyMarshaller: selects the XML or JSON reader by exact media type (application/xmlorapplication/json, parameters ignored) and passes MOXy a fixed media type instead of the request's. Other types, including wildcards, are rejected with HTTP 415.Commons Digester:
TopologyUtils,XmlGatewayDescriptorImporterandXmlUrlRewriteRulesImporterpass Digester anXMLReadercreated bySecureSAXParserFactory. Since Digester 3.2 resolves any absolute system id by itself, each site also installs a resolver that returnsnull, leaving external references to Commons Secure XML.Enforcement: a new forbidden-apis execution (
build-tools/forbiddenapis/xml-signatures.txt, main code only) rejects the plain JAXP factory methods, theUnmarshaller.unmarshaloverloads that let the JAXB provider create its own parser (File,InputStream,Reader,URL,InputSource,Source) and the Digester constructors that create their own parser.TopologyMarshalleris excluded from this check ingateway-service-admin/pom.xml, because MOXy parses its JSON request bodies with its own JSON reader. On the currentmasterit reports 23 violations in 9 modules, for example:Behavior change: documents containing a DOCTYPE are now accepted everywhere. External references are ignored and internal entities are expanded within the limits of secure processing. Previously
XmlUtilsrejected any DOCTYPE and the StAX readers disabled DTD support.Minor fixes in touched code:
XmlUtils.readXml(File)andProviderConfigurationParser.parseXML(File)no longer leak their input stream, andServiceDefinitionsLoaderno longer opens a stream it does not read.How was this patch tested?
mvn installon the affected modules (gateway-util-common,gateway-spi,gateway-service-admin,gateway-provider-rewrite,gateway-provider-rewrite-common,gateway-server,gateway-discovery-ambari,gateway-service-remoteconfig,gateway-topology-simple) and their dependencies: all unit tests, checkstyle, forbidden-apis and dependency analysis pass.GatewayAdminTopologyFuncTest: 32/32 pass.GatewayAdminTopologyFuncTest#testPutTopologyWithEntityExpansion,ServiceDefinitionUnmarshallerTest#testEntityExpansionIsNotResolved) asserted that internal entities are not expanded; they now assert that the content of an external entity does not leak. RevertingServiceDefinitionUnmarshallerto a plainXMLInputFactorymakes both unit tests fail.Integration Tests
No integration tests are added or changed: the change does not alter any Knox feature, and the existing suites cover the affected parsing paths.
🤖 Generated with Claude Code