gnodet-bot commented on code in PR #31193:
URL: https://github.com/apache/camel/pull/31193#discussion_r4771029475


##########
components/camel-xslt/src/main/java/org/apache/camel/component/xslt/XsltBuilder.java:
##########
@@ -472,10 +476,25 @@ protected Transformer createTransformer() throws 
Exception {
     }
 
     private void tryAddEntityResolver(SAXSource source) {
-        //expecting source to have not null XMLReader
-        if (this.entityResolver != null && source != null) {
-            source.getXMLReader().setEntityResolver(this.entityResolver);
+        if (this.entityResolver == null || source == null) {
+            return;
         }
+        XMLReader reader = source.getXMLReader();
+        if (reader == null) {
+            // SAXSource(InputSource) leaves the XMLReader unset; create a 
securely configured one
+            // so the entity resolver is honoured instead of being silently 
ignored.
+            try {
+                SAXParserFactory factory = SAXParserFactory.newInstance();

Review Comment:
   ⚡ **`SAXParserFactory.newInstance()` per exchange.** `tryAddEntityResolver` 
runs inside `process()`, so a SAXSource-per-message workload allocates a 
factory + parser per message. `SAXParserFactory` is thread-safe after 
configuration — consider caching it as a field (initialized once in the 
constructor or lazily) to avoid repeated instantiation.



##########
components/camel-xslt/src/main/java/org/apache/camel/component/xslt/XsltBuilder.java:
##########
@@ -472,10 +476,25 @@ protected Transformer createTransformer() throws 
Exception {
     }
 
     private void tryAddEntityResolver(SAXSource source) {
-        //expecting source to have not null XMLReader
-        if (this.entityResolver != null && source != null) {
-            source.getXMLReader().setEntityResolver(this.entityResolver);
+        if (this.entityResolver == null || source == null) {
+            return;
         }
+        XMLReader reader = source.getXMLReader();
+        if (reader == null) {
+            // SAXSource(InputSource) leaves the XMLReader unset; create a 
securely configured one
+            // so the entity resolver is honoured instead of being silently 
ignored.
+            try {
+                SAXParserFactory factory = SAXParserFactory.newInstance();
+                factory.setNamespaceAware(true);
+                factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, 
true);
+                reader = factory.newSAXParser().getXMLReader();

Review Comment:
   🔒 **Missing external-entity restrictions.** The new reader gets 
`FEATURE_SECURE_PROCESSING` but not the external-access disablement features. 
The JDK transformer's own reader inherits the `TransformerFactory`'s 
`ACCESS_EXTERNAL_DTD=""` restriction — a Camel-created reader without the same 
features widens the access surface when the user-supplied `entityResolver` 
returns `null` for an entity (the reader falls back to network fetch; the 
transformer's own reader would have blocked it).
   
   ```suggestion
                   SAXParserFactory factory = SAXParserFactory.newInstance();
                   factory.setNamespaceAware(true);
                   factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, 
true);
                   try {
                       
factory.setFeature("http://xml.org/sax/features/external-general-entities";, 
false);
                       
factory.setFeature("http://xml.org/sax/features/external-parameter-entities";, 
false);
                   } catch (SAXException ignored) {
                       // best-effort: not all parsers support these features
                   }
                   reader = factory.newSAXParser().getXMLReader();
                   source.setXMLReader(reader);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to