garydgregory commented on code in PR #42:
URL: https://github.com/apache/commons-xml/pull/42#discussion_r3857207339
##########
src/test/java/org/apache/commons/xml/EntityResolverFloorTest.java:
##########
@@ -352,7 +361,41 @@ void transformerDeniesUnlisted() {
.transform(AttackTestSupport.streamSource("<root/>"), new
StreamResult(sink));
assertFalse(sink.toString().contains(AttackTestSupport.LEAKED_MARKER),
"unlisted stylesheet import leaked");
} catch (final TransformerException blocked) {
- // Acceptable: rejected at compile rather than resolved to empty.
+ // Throwing is an acceptable outcome, since it doesn't leak the
marker.
+ }
+ }
+
+ @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) {
+ // Throwing is an acceptable outcome, since it doesn't leak the
marker.
+ }
+ }
+
+ @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) {
+ // Throwing is an acceptable outcome, since it doesn't leak the
marker.
Review Comment:
Same as before: if this is a non-deterministic failure, add a clearer
comment explaining when it can happen; if it’s an invariant, use
`assertThrows()` instead of using try with an empty catch.
##########
src/test/java/org/apache/commons/xml/EntityResolverFloorTest.java:
##########
@@ -352,7 +361,41 @@ void transformerDeniesUnlisted() {
.transform(AttackTestSupport.streamSource("<root/>"), new
StreamResult(sink));
assertFalse(sink.toString().contains(AttackTestSupport.LEAKED_MARKER),
"unlisted stylesheet import leaked");
} catch (final TransformerException blocked) {
- // Acceptable: rejected at compile rather than resolved to empty.
+ // Throwing is an acceptable outcome, since it doesn't leak the
marker.
+ }
+ }
+
+ @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) {
Review Comment:
Same as before: if this is a non-deterministic failure, add a clearer
comment explaining when it can happen; if it’s an invariant, use
`assertThrows()` instead of using try with an empty catch.
--
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]