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
- the XPATH expression might contain some unsafe input coming from a user.
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.
Sensitive Code Example
// === Server side ===
var xpath = require('xpath');
var xmldom = require('xmldom');
var doc = new xmldom.DOMParser().parseFromString(xml);
var nodes = xpath.select(userinput, doc); // Sensitive
var node = xpath.select1(userinput, doc); // Sensitive
// === Client side ===
// Chrome, Firefox, Edge, Opera, and Safari use the evaluate() method to select nodes:
var nodes = document.evaluate(userinput, xmlDoc, null, XPathResult.ANY_TYPE, null); // Sensitive
// Internet Explorer uses its own methods to select nodes:
var nodes = xmlDoc.selectNodes(userinput); // Sensitive
var node = xmlDoc.SelectSingleNode(userinput); // Sensitive
This rule is deprecated, and will eventually be removed.