This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch backport/25594-to-camel-4-18-x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 91f24c5c96ffc37afb87d794560abc8ce2335c1b Author: Claus Ibsen <[email protected]> AuthorDate: Mon Nov 16 10:12:54 2026 +0100 CAMEL-24416: camel-core - align the XmlConverter SAX parser factory with the DOM one XmlConverter.createDocumentBuilderFactory() disables DOCTYPE declarations and external general entities, but createSAXParserFactory() in the same class set only FEATURE_SECURE_PROCESSING and external-general-entities=false, so the two parser factories in one class did not agree on their hardening. This aligns the SAX factory with the DOM one: it also disables external parameter entities and external DTD loading. Backport of #25594 (3defc689649d). The upgrade guide note is on main. Co-authored-by: Claude Opus 5 (1M context) <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../jaxp/SaxParserFactoryHardeningTest.java | 60 ++++++++++++++++++++++ .../apache/camel/converter/jaxp/XmlConverter.java | 12 +++++ 2 files changed, 72 insertions(+) diff --git a/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/SaxParserFactoryHardeningTest.java b/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/SaxParserFactoryHardeningTest.java new file mode 100644 index 000000000000..2bae384035bc --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/converter/jaxp/SaxParserFactoryHardeningTest.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.converter.jaxp; + +import javax.xml.XMLConstants; +import javax.xml.parsers.SAXParserFactory; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The SAX factory and the DOM factory in {@link XmlConverter} are both reachable from a converted message body, so they + * must not disagree about external-resource resolution. The DOM factory has always blocked it; the SAX factory only + * blocked general entities. + * <p/> + * These assertions are on the factory configuration rather than on parse behaviour deliberately: whether a given JDK + * would have fetched the resource anyway varies by version, and the point is that Camel states the intent itself. + */ +class SaxParserFactoryHardeningTest { + + private static final String EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities"; + private static final String EXTERNAL_PARAMETER_ENTITIES = "http://xml.org/sax/features/external-parameter-entities"; + private static final String LOAD_EXTERNAL_DTD = "http://apache.org/xml/features/nonvalidating/load-external-dtd"; + + @Test + void saxFactoryBlocksExternalResourceResolution() throws Exception { + SAXParserFactory factory = new XmlConverter().createSAXParserFactory(); + + assertThat(factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)).isTrue(); + assertThat(factory.getFeature(EXTERNAL_GENERAL_ENTITIES)).isFalse(); + assertThat(factory.getFeature(EXTERNAL_PARAMETER_ENTITIES)).isFalse(); + assertThat(factory.getFeature(LOAD_EXTERNAL_DTD)).isFalse(); + } + + @Test + void saxFactoryAgreesWithTheDomFactoryOnSharedFeatures() throws Exception { + SAXParserFactory sax = new XmlConverter().createSAXParserFactory(); + var dom = new XmlConverter().createDocumentBuilderFactory(); + + assertThat(sax.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) + .isEqualTo(dom.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); + assertThat(sax.getFeature(EXTERNAL_GENERAL_ENTITIES)) + .isEqualTo(dom.getFeature(EXTERNAL_GENERAL_ENTITIES)); + } +} diff --git a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java index 63ba11da1b72..5135d093ef2a 100644 --- a/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java +++ b/core/camel-xml-jaxp/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java @@ -1205,6 +1205,18 @@ public class XmlConverter { LOG.warn("SAXParser doesn't support the feature {} with value {}, due to {}.", "http://xml.org/sax/features/external-general-entities", false, e.getMessage()); } + try { + sfactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); + } catch (Exception e) { + LOG.warn("SAXParser doesn't support the feature {} with value {}, due to {}.", + "http://xml.org/sax/features/external-parameter-entities", false, e.getMessage()); + } + try { + sfactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); + } catch (Exception e) { + LOG.warn("SAXParser doesn't support the feature {} with value {}, due to {}.", + "http://apache.org/xml/features/nonvalidating/load-external-dtd", false, e.getMessage()); + } sfactory.setNamespaceAware(true); return sfactory; }
