Executing XPATH expressions is security-sensitive. It has led in the past to the following vulnerabilities:

User provided data such as URL parameters should always be considered as untrusted and tainted. Constructing XPath expressions directly from tainted data enables attackers to inject specially crafted values that changes the initial meaning of the expression itself. Successful XPath injections attacks can read sensitive information from the XML document.

Ask Yourself Whether

You are at risk if you answered yes to this question.

Recommended Secure Coding Practices

Sanitize any user input before using it in an XPATH expression.

Questionable Code Example

// === javax.xml.xpath.XPath ===
import javax.xml.namespace.QName;
import javax.xml.xpath.XPath;

import org.xml.sax.InputSource;

class M {
    void foo(XPath xpath, String expression, InputSource source, QName returnType, Object item) throws Exception {
        xpath.compile(expression); // Questionable
        xpath.evaluate(expression, source); // Questionable
        xpath.evaluate(expression, source, returnType); // Questionable
        xpath.evaluate(expression, item); // Questionable
        xpath.evaluate(expression, item, returnType); // Questionable
    }
}
// === Apache XML Security ===
import org.apache.xml.utils.PrefixResolver;
import org.apache.xml.security.utils.XPathAPI;
import org.w3c.dom.Node;

class M {
    void foo(XPathAPI api, Node contextNode, String str, Node namespaceNode, PrefixResolver prefixResolver,
            Node xpathnode) throws Exception {
        api.evaluate(contextNode, xpathnode, str, namespaceNode); // Questionable
        api.selectNodeList(contextNode, xpathnode, str, namespaceNode); // Questionable
    }
}
// === Apache Xalan ===
import org.apache.xml.utils.PrefixResolver;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Node;

class M {
    void foo(XPathAPI api, Node contextNode, String str, Node namespaceNode, PrefixResolver prefixResolver)
            throws Exception {
        XPathAPI.eval(contextNode, str); // Questionable
        XPathAPI.eval(contextNode, str, namespaceNode); // Questionable
        XPathAPI.eval(contextNode, str, prefixResolver); // Questionable
        XPathAPI.selectNodeIterator(contextNode, str); // Questionable
        XPathAPI.selectNodeIterator(contextNode, str, namespaceNode); // Questionable
        XPathAPI.selectNodeList(contextNode, str); // Questionable
        XPathAPI.selectNodeList(contextNode, str, namespaceNode); // Questionable
        XPathAPI.selectSingleNode(contextNode, str); // Questionable
        XPathAPI.selectSingleNode(contextNode, str, namespaceNode); // Questionable
    }
}
// === org.apache.commons.jxpath ===
import org.apache.commons.jxpath.JXPathContext;

abstract class A extends JXPathContext{
    A(JXPathContext compilationContext, Object contextBean) {
        super(compilationContext, contextBean);
    }


    void foo(JXPathContext context, String str, Object obj, Class<?> requiredType) {
        JXPathContext.compile(str); // Questionable
        this.compilePath(str); // Questionable
        context.createPath(str); // Questionable
        context.createPathAndSetValue(str, obj); // Questionable
        context.getPointer(str); // Questionable
        context.getValue(str); // Questionable
        context.getValue(str, requiredType); // Questionable
        context.iterate(str); // Questionable
        context.iteratePointers(str); // Questionable
        context.removeAll(str); // Questionable
        context.removePath(str); // Questionable
        context.selectNodes(str); // Questionable
        context.selectSingleNode(str); // Questionable
        context.setValue(str, obj); // Questionable
    }
}

See