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.
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).
Potential Impact
Access Control
Bypass Protection Mechanism
Confidentiality
Read Application Data
Demonstrative Examples
<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>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);//users/user[login/text()='john' or ''='' and password/text() = '' or ''='']/home_dir/text()Mitigations & Prevention
Use parameterized XPath queries (e.g. using XQuery). This will help ensure separation between data plane and control plane.
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.
Detection Methods
- Automated Static Analysis High — Automated static analysis, commonly referred to as Static Application Security Testing (SAST), can find some instances of this weakness by analyzing source code (or binary/compiled code) without having to execute it. Typically, this is done by building a model of data flow and control flow, then sea
Related Weaknesses
Taxonomy Mappings
- WASC: 39 — XPath Injection
- Software Fault Patterns: SFP24 — Tainted input to command
Frequently Asked Questions
What is CWE-643?
CWE-643 (Improper Neutralization of Data within XPath Expressions ('XPath Injection')) is a software weakness identified by MITRE's Common Weakness Enumeration. It is classified as a Base-level weakness. 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 a...
How can CWE-643 be exploited?
Attackers can exploit CWE-643 (Improper Neutralization of Data within XPath Expressions ('XPath Injection')) to bypass protection mechanism. This weakness is typically introduced during the Implementation phase of software development.
How do I prevent CWE-643?
Key mitigations include: Use parameterized XPath queries (e.g. using XQuery). This will help ensure separation between data plane and control plane.
What is the severity of CWE-643?
CWE-643 is classified as a Base-level weakness (Medium abstraction). Its actual severity depends on the specific context and how the weakness manifests in your application.