This is an automated email from the ASF dual-hosted git repository.

oscerd pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 3defc689649d CAMEL-24416: camel-core - align the XmlConverter SAX 
parser factory with the DOM one
3defc689649d is described below

commit 3defc689649d0c324e2ec2af31fb1a37f3830550
Author: Andrea Cosentino <[email protected]>
AuthorDate: Wed Aug 26 09:00:51 2026 +0200

    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, so the two parser factories in one class did not 
agree on
    their hardening.
    
    This aligns the SAX factory with the DOM one, so both refuse a DOCTYPE 
declaration and
    neither resolves external entities.
    
    Closes #25594
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
---
 .../jaxp/SaxParserFactoryHardeningTest.java        | 60 ++++++++++++++++++++++
 .../apache/camel/converter/jaxp/XmlConverter.java  | 12 +++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_23.adoc    | 19 +++++++
 3 files changed, 91 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 fdb98e05e002..d316a5eaf82a 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;
     }
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
index 3c88f2fbad71..4bf8e9fd9541 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_23.adoc
@@ -206,6 +206,25 @@ Routes that relied on steps after these processors running 
for unauthenticated r
 restructured. The authenticated paths are unchanged: a successfully 
authenticated request continues
 through the rest of the route exactly as before, and `OAuthLogoutProcessor` is 
unchanged.
 
+=== camel-core - XmlConverter SAX parser factory
+
+`XmlConverter.createSAXParserFactory()` now also disables external parameter 
entities and external
+DTD loading:
+
+* `http://xml.org/sax/features/external-parameter-entities` = `false`
+* `http://apache.org/xml/features/nonvalidating/load-external-dtd` = `false`
+
+It previously set only `FEATURE_SECURE_PROCESSING` and 
`external-general-entities=false`, while
+`createDocumentBuilderFactory()` in the same class already blocked external 
resource resolution more
+thoroughly. Both factories are reachable from a converted message body — 
`toSAXSource` is a
+registered converter, and the SAXSource route is tried first for bodies 
reaching camel-xslt — so the
+two should not disagree.
+
+Documents carrying an internal DTD subset still parse: `disallow-doctype-decl` 
is deliberately not
+set here, because that would reject input that parses today. Routes that 
genuinely need to resolve
+an external DTD or parameter entity through this converter must supply their 
own
+`SAXParserFactory`.
+
 === camel-netty-http
 
 The security-constraint lookup now strips the endpoint context-path from the 
request target

Reply via email to