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

davsclaus pushed a commit to branch camel-4.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.22.x by this push:
     new da838c1ca4f4 [backport camel-4.22.x] CAMEL-24416: camel-core - align 
the XmlConverter SAX parser factory with the DOM one (#30181)
da838c1ca4f4 is described below

commit da838c1ca4f48f26f61bbcd9c59e073becc500a9
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 16 14:34:40 2026 +0100

    [backport camel-4.22.x] CAMEL-24416: camel-core - align the XmlConverter 
SAX parser factory with the DOM one (#30181)
    
    Backport of #25594 (CAMEL-24416: camel-core - align the XmlConverter SAX 
factory with the DOM factory on external resources) onto `camel-4.22.x`.
    
    **Original PR:** #25594, merged on main as 3defc689649d
    **Original author:** Andrea Cosentino
    **Target branch:** `camel-4.22.x`
    
    `XmlConverter.createSAXParserFactory()` now also sets 
`external-parameter-entities=false` and `load-external-dtd=false`, the same as 
on main. Before, it set only `FEATURE_SECURE_PROCESSING` and 
`external-general-entities=false`. This SAX factory also backs the secure 
`XMLReader` that the CAMEL-26498 backports (#30157, #30158) use when stream 
caching a `SAXSource`.
    
    _Claude Code on behalf of davsclaus_
    
    Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
    Co-Authored-By: Claude Opus 5.5 <[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 c0ea03898a1a..b0fa3add7226 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
@@ -1213,6 +1213,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;
     }

Reply via email to