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


##########
src/main/java/org/apache/commons/xml/HardeningXPath.java:
##########
@@ -0,0 +1,141 @@
+/*
+ * 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 java.io.IOException;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+
+import org.w3c.dom.Document;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * {@link XPath} wrapper that performs the document build behind every {@link 
InputSource}-taking {@code evaluate} call with a hardened, namespace-aware
+ * {@link javax.xml.parsers.DocumentBuilder} and evaluates the delegate 
against the parsed {@link Document}, so the engine's own parser never runs.
+ *
+ * <p>The JAXP contract for {@link XPath#evaluate(String, InputSource, QName)} 
is "build a document from the source, then evaluate against it", and both the
+ * stock JDK and Apache Xalan provision an internal parser for that build 
which {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} on the
+ * {@link javax.xml.xpath.XPathFactory} does not reach. Parsing here puts the 
build on the library's resolver floor: an external reference inside the document
+ * resolves to empty content, so it is neither fetched nor leaked, and the 
evaluation proceeds on whatever the parse produced. {@link #compile(String)} 
wraps
+ * the compiled expression in a {@link HardeningXPathExpression} on the same 
terms.</p>
+ *
+ * <p>The {@code evaluateExpression} default methods added to the interface by 
Java 9 route through the {@code evaluate} overloads overridden here, so they
+ * carry the same rewrite on newer runtimes even though this class targets 
Java 8.</p>
+ */
+final class HardeningXPath implements XPath {
+
+    /**
+     * Parses the source through a hardened, namespace-aware {@link 
javax.xml.parsers.DocumentBuilder}, mirroring the namespace awareness of the 
parser the
+     * engine would have provisioned.
+     *
+     * @param source The document to evaluate against.
+     * @return The parsed document.
+     * @throws NullPointerException     if {@code source} is {@code null}, per 
the {@link XPath} contract.
+     * @throws XPathExpressionException if the source cannot be parsed.
+     */
+    static Document parse(final InputSource source) throws 
XPathExpressionException {
+        if (source == null) {
+            throw new NullPointerException("source cannot be null");
+        }
+        try {
+            final DocumentBuilderFactory factory = 
DocumentBuilderHardener.harden(DocumentBuilderFactory.newInstance());
+            factory.setNamespaceAware(true);
+            return factory.newDocumentBuilder().parse(source);
+        } catch (final ParserConfigurationException | SAXException | 
IOException e) {
+            throw new XPathExpressionException(e);
+        }
+    }
+
+    private final XPath delegate;
+
+    HardeningXPath(final XPath delegate) {
+        this.delegate = delegate;

Review Comment:
   I applied the suggested `Objects.requireNonNull` in 
https://github.com/apache/commons-xml/pull/44/commits/6315ef84d6731d2fc57e2cc6355e354a0db85738,
 but maybe we should use nullability annotations or comments instead, so we 
don't introduce a (cheap) runtime check even if no caller ever passes `null`>



##########
src/main/java/org/apache/commons/xml/HardeningXPathFactory.java:
##########
@@ -0,0 +1,73 @@
+/*
+ * 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 javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathFactory;
+import javax.xml.xpath.XPathFactoryConfigurationException;
+import javax.xml.xpath.XPathFunctionResolver;
+import javax.xml.xpath.XPathVariableResolver;
+
+/**
+ * {@link XPathFactory} wrapper that returns a {@link HardeningXPath} from 
{@link #newXPath()}.
+ *
+ * <p>Required because {@link 
javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} on the factory governs only 
the XPath engine: the stock JDK and Apache Xalan
+ * implement the {@link org.xml.sax.InputSource}-taking {@code evaluate} entry 
points by provisioning an internal document parser the feature does not reach.
+ * The wrapper performs that document build itself through a hardened parser 
instead; see {@link HardeningXPath}.</p>
+ */
+final class HardeningXPathFactory extends XPathFactory {
+
+    private final XPathFactory delegate;
+
+    HardeningXPathFactory(final XPathFactory delegate) {
+        this.delegate = delegate;

Review Comment:
   Fixed in 
https://github.com/apache/commons-xml/pull/44/commits/6315ef84d6731d2fc57e2cc6355e354a0db85738



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