ppkarwasz commented on code in PR #42:
URL: https://github.com/apache/commons-xml/pull/42#discussion_r3856978482


##########
src/test/java/org/apache/commons/xml/EntityResolverFloorTest.java:
##########
@@ -356,6 +380,40 @@ void transformerDeniesUnlisted() {
         }
     }
 
+    @Test
+    @Tag("trax")
+    void transformerParsesOptedInImportHardened() {
+        // The opted-in module carries an external DTD reference; parsed on 
the floor the DTD is empty, so its entity cannot expand into the output.
+        final TransformerFactory factory = hardenedTransformerFactory();
+        factory.setURIResolver((href, base) ->
+                href != null && href.endsWith("included.xsl") ? 
AttackTestSupport.resourceSource("included-with-entity.xsl") : null);
+        try {
+            final StringWriter sink = new StringWriter();
+            
factory.newTemplates(AttackTestSupport.resourceSource("with-import.xsl")).newTransformer()
+                    .transform(AttackTestSupport.streamSource("<root/>"), new 
StreamResult(sink));
+            
assertFalse(sink.toString().contains(AttackTestSupport.LEAKED_MARKER), 
"opted-in stylesheet import leaked its external entity");
+        } catch (final TransformerException blocked) {
+            // Acceptable: the hardened parse reports the entity as undeclared 
instead of expanding it.
+        }
+    }
+
+    @Test
+    @Tag("trax")
+    void transformerParsesOptedInDocumentHardened() {
+        // Same contract on the runtime document() channel, which reaches a 
different internal reader than the compile-time import.
+        final TransformerFactory factory = hardenedTransformerFactory();
+        factory.setURIResolver((href, base) ->
+                href != null && href.endsWith("referenced.xml") ? 
AttackTestSupport.resourceSource("referenced-with-entity.xml") : null);
+        try {
+            final StringWriter sink = new StringWriter();
+            
factory.newTemplates(AttackTestSupport.resourceSource("with-document.xsl")).newTransformer()
+                    .transform(AttackTestSupport.streamSource("<root/>"), new 
StreamResult(sink));
+            
assertFalse(sink.toString().contains(AttackTestSupport.LEAKED_MARKER), 
"opted-in document() resource leaked its external entity");
+        } catch (final TransformerException blocked) {
+            // Acceptable: the hardened parse reports the entity as undeclared 
instead of expanding it.

Review Comment:
   I improved the comment in 
https://github.com/apache/commons-xml/pull/42/commits/cc3c0ad8e2d455c1cd918282c0614fed4fcde9fa



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