CWE-643: Improper Neutralization of Data within XPath Expressions ('XPath Injection')

Description

The product uses external input to dynamically construct an XPath expression used to retrieve data from an XML database, but it does not neutralize or incorrectly neutralizes that input. This allows an attacker to control the structure of the query.

Submission Date :

Jan. 30, 2008, midnight

Modification Date :

2023-06-29 00:00:00+00:00

Organization :

Cigital
Extended Description

The net effect is that the attacker will have control over the information selected from the XML database and may use that ability to control application flow, modify logic, retrieve unauthorized data, or bypass important checks (e.g. authentication).

Example Vulnerable Codes

Example - 1

Consider the following simple XML document that stores authentication information and a snippet of Java code that uses XPath query to retrieve authentication information:


<login>john</login><password>abracadabra</password><home_dir>/home/john</home_dir>

<login>cbc</login><password>1mgr8</password><home_dir>/home/cbc</home_dir><user></user><user></user><users></users>

The Java code used to retrieve the home directory based on the provided credentials is:


XPath xpath = XPathFactory.newInstance().newXPath();XPathExpression xlogin = xpath.compile("//users/user[login/text()='" + login.getUserName() + "' and password/text() = '" + login.getPassword() + "']/home_dir/text()");Document d = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("db.xml"));String homedir = xlogin.evaluate(d);

Assume that user "john" wishes to leverage XPath Injection and login without a valid password. By providing a username "john" and password "' or ''='" the XPath expression now becomes

//users/user[login/text()='john' or ''='' and password/text() = '' or ''='']/home_dir/text()

which, of course, lets user "john" login without a valid password, thus bypassing authentication.

Related Weaknesses

This table shows the weaknesses and high level categories that are related to this weakness. These relationships are defined to give an overview of the different insight to similar items that may exist at higher and lower levels of abstraction.

Visit http://cwe.mitre.org/ for more details.