garydgregory commented on code in PR #44:
URL: https://github.com/apache/commons-xml/pull/44#discussion_r3857244402


##########
src/test/java/org/apache/commons/xml/XPathInputSourceTest.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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
+ *
+ *      https://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.commons.xml;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Checks that the document parse behind {@code XPath.evaluate(String, 
InputSource)} (and its compiled {@code XPathExpression} counterpart) cannot 
pull in an
+ * external general entity.
+ *
+ * <p>The stock JDK and Apache Xalan implement the {@link 
org.xml.sax.InputSource}-taking {@code evaluate} entry points by provisioning 
an internal document
+ * parser that {@code FEATURE_SECURE_PROCESSING} on the {@link XPathFactory} 
does not reach. The {@link HardeningXPathFactory} wrapper parses the input
+ * through a hardened {@code DocumentBuilder} instead, so the external 
reference resolves to empty on the floor (or the parse is rejected outright), 
while the
+ * evaluation itself still works. Tagged {@code xpath}, so it runs under 
test-stockjdk, test-xalan and test-xalan-xerces; the Saxon engine takes the 
separate
+ * {@code SaxonProvider} path covered by {@code 
SaxonXPathExternalCallsTest}.</p>
+ */
+@Tag("xpath")
+class XPathInputSourceTest {
+
+    private static final String EXPRESSION = "string(/root/child)";
+
+    /** {@link AttackTestSupport#xmlBody} content whose single entity 
reference resolves to {@link AttackTestSupport#LEAKED_MARKER} if the DTD is 
fetched. */
+    private static String entityPayload() {
+        return "<?xml version=\"1.0\"?>\n"
+                + "<!DOCTYPE root [\n  <!ENTITY xxe SYSTEM \"" + 
AttackTestSupport.resourceUrl("referenced.txt") + "\">\n]>\n"
+                + AttackTestSupport.xmlBody("&xxe;");
+    }
+
+    @Test
+    void hardenedXPathEvaluateDoesNotLeak() throws Exception {
+        try {
+            final String result = 
XmlFactories.newXPathFactory().newXPath().evaluate(EXPRESSION, 
AttackTestSupport.inputSource(entityPayload()));
+            assertFalse(result.contains(AttackTestSupport.LEAKED_MARKER), 
"external entity leaked into the XPath result: " + result);
+        } catch (final XPathExpressionException ignored) {
+            // Throwing is an acceptable result, since it does not leak the 
marker.

Review Comment:
   Same as before and in other PRs: 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/XPathInputSourceTest.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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
+ *
+ *      https://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.commons.xml;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFactory;
+
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Checks that the document parse behind {@code XPath.evaluate(String, 
InputSource)} (and its compiled {@code XPathExpression} counterpart) cannot 
pull in an
+ * external general entity.
+ *
+ * <p>The stock JDK and Apache Xalan implement the {@link 
org.xml.sax.InputSource}-taking {@code evaluate} entry points by provisioning 
an internal document
+ * parser that {@code FEATURE_SECURE_PROCESSING} on the {@link XPathFactory} 
does not reach. The {@link HardeningXPathFactory} wrapper parses the input
+ * through a hardened {@code DocumentBuilder} instead, so the external 
reference resolves to empty on the floor (or the parse is rejected outright), 
while the
+ * evaluation itself still works. Tagged {@code xpath}, so it runs under 
test-stockjdk, test-xalan and test-xalan-xerces; the Saxon engine takes the 
separate
+ * {@code SaxonProvider} path covered by {@code 
SaxonXPathExternalCallsTest}.</p>
+ */
+@Tag("xpath")
+class XPathInputSourceTest {
+
+    private static final String EXPRESSION = "string(/root/child)";
+
+    /** {@link AttackTestSupport#xmlBody} content whose single entity 
reference resolves to {@link AttackTestSupport#LEAKED_MARKER} if the DTD is 
fetched. */
+    private static String entityPayload() {
+        return "<?xml version=\"1.0\"?>\n"
+                + "<!DOCTYPE root [\n  <!ENTITY xxe SYSTEM \"" + 
AttackTestSupport.resourceUrl("referenced.txt") + "\">\n]>\n"
+                + AttackTestSupport.xmlBody("&xxe;");
+    }
+
+    @Test
+    void hardenedXPathEvaluateDoesNotLeak() throws Exception {
+        try {
+            final String result = 
XmlFactories.newXPathFactory().newXPath().evaluate(EXPRESSION, 
AttackTestSupport.inputSource(entityPayload()));
+            assertFalse(result.contains(AttackTestSupport.LEAKED_MARKER), 
"external entity leaked into the XPath result: " + result);
+        } catch (final XPathExpressionException ignored) {
+            // Throwing is an acceptable result, since it does not leak the 
marker.
+        }
+    }
+
+    @Test
+    void hardenedXPathExpressionEvaluateDoesNotLeak() throws Exception {
+        try {
+            final String result = 
XmlFactories.newXPathFactory().newXPath().compile(EXPRESSION).evaluate(AttackTestSupport.inputSource(entityPayload()));
+            assertFalse(result.contains(AttackTestSupport.LEAKED_MARKER), 
"external entity leaked into the compiled XPath result: " + result);
+        } catch (final XPathExpressionException ignored) {
+            // Throwing is an acceptable result, since it does not leak the 
marker.

Review Comment:
   Same as before and in other PRs: 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]

Reply via email to