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


##########
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:
   **[medium]** `SAXParserFactory.newInstance()` performs service-loader 
classpath scanning on every call. On routes processing many reader-less 
`SAXSource` bodies with `entityResolver` configured, this cost is paid per 
exchange. The factory is thread-safe after configuration — only the `XMLReader` 
instance needs to be new per call. Cache it as a `private static final` field:
   
   ```suggestion
               try {
                   reader = ENTITY_RESOLVER_SPF.newSAXParser().getXMLReader();
   ```
   
   Where `ENTITY_RESOLVER_SPF` is defined as:
   
   ```java
   private static final SAXParserFactory ENTITY_RESOLVER_SPF;
   static {
       ENTITY_RESOLVER_SPF = SAXParserFactory.newInstance();
       ENTITY_RESOLVER_SPF.setNamespaceAware(true);
       try {
           
ENTITY_RESOLVER_SPF.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
       } catch (ParserConfigurationException | SAXException e) {
           throw new ExceptionInInitializerError(e);
       }
   }
   ```
   
   This is the same pattern `SecureReaderPoolHolder` uses on the Saxon side. 
The three `factory.set*` calls move from per-exchange to class-load time, and 
`factory.newSAXParser()` (which creates the parser) remains per-call.



-- 
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