CWE

Common Weakness Enumeration

A Community-Developed Dictionary of Software Weakness Types

CWE/SANS Top 25 Most Dangerous Software Errors Common Weakness Scoring System
Common Weakness Risk Analysis Framework
Home > CWE List > CWE- Individual Dictionary Definition (2.7)  

Presentation Filter:

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

 
Improper Neutralization of Data within XPath Expressions ('XPath Injection')
Weakness ID: 643 (Weakness Base)Status: Incomplete
+ Description

Description Summary

The software 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.

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

+ Time of Introduction
  • Implementation
+ Applicable Platforms

Languages

All

+ Common Consequences
ScopeEffect

Technical Impact: Bypass protection mechanism

Controlling application flow (e.g. bypassing authentication).

Technical Impact: Read application data

The attacker could read restricted XML content.

+ Likelihood of Exploit

High

+ Enabling Factors for Exploitation

XPath queries are constructed dynamically using user supplied input

+ Demonstrative Examples

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:

Example Language: XML 
<users>
<user>
<login>john</login>
<password>abracadabra</password>
<home_dir>/home/john</home_dir>
</user>
<user>
<login>cbc</login>
<password>1mgr8</password>
<home_dir>/home/cbc</home_dir>
</user>
</users>

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

(Bad Code)
Example Language: Java 
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

(Attack)
 
//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.

+ Potential Mitigations

Phase: Implementation

Use parameterized XPath queries (e.g. using XQuery). This will help ensure separation between data plane and control plane.

Phase: Implementation

Properly validate user input. Reject data where appropriate, filter where appropriate and escape where appropriate. Make sure input that will be used in XPath queries is safe in that context.

+ Relationships
NatureTypeIDNameView(s) this relationship pertains toView(s)
ChildOfWeakness BaseWeakness Base91XML Injection (aka Blind XPath Injection)
Development Concepts699
Research Concepts1000
ChildOfCategoryCategory896SFP Cluster: Tainted Input
Software Fault Pattern (SFP) Clusters (primary)888
ChildOfWeakness ClassWeakness Class943Improper Neutralization of Special Elements in Data Query Logic
Development Concepts (primary)699
Research Concepts (primary)1000
+ Relationship Notes

This weakness is similar to other weaknesses that enable injection style attacks, such as SQL injection, command injection and LDAP injection. The main difference is that the target of attack here is the XML database.

+ Taxonomy Mappings
Mapped Taxonomy NameNode IDFitMapped Node Name
WASC39XPath Injection
+ References
Web Application Security Consortium. "XPath Injection". <http://www.webappsec.org/projects/threat/classes/xpath_injection.shtml>.
[REF-7] Mark Dowd, John McDonald and Justin Schuh. "The Art of Software Security Assessment". Chapter 17, "XPath Injection", Page 1070.. 1st Edition. Addison Wesley. 2006.
+ Content History
Submissions
Submission DateSubmitterOrganizationSource
2008-01-30CigitalExternal Submission
Modifications
Modification DateModifierOrganizationSource
2008-09-08MITREInternal
updated Common_Consequences, Relationships
2008-10-14MITREInternal
updated Description, Name, References, Relationship_Notes
2009-03-10MITREInternal
updated Demonstrative_Examples
2009-05-27MITREInternal
updated Name
2009-10-29MITREInternal
updated Common_Consequences
2010-02-16MITREInternal
updated Taxonomy_Mappings
2010-04-05MITREInternal
updated Description, Name
2010-06-21MITREInternal
updated Enabling_Factors_for_Exploitation
2010-12-13MITREInternal
updated Common_Consequences
2011-06-01MITREInternal
updated Common_Consequences
2012-05-11MITREInternal
updated References, Relationships
2012-10-30MITREInternal
updated Potential_Mitigations
2014-06-23MITREInternal
updated Relationships
Previous Entry Names
Change DatePrevious Entry Name
2008-10-14Unsafe Treatment of XPath Input
2009-05-27Failure to Sanitize Data within XPath Expressions (aka 'XPath injection')
2010-04-05Failure to Sanitize Data within XPath Expressions ('XPath injection')
Page Last Updated: June 23, 2014