XPath Injection
User input is inserted into an XPath expression that queries XML data, so it can change the expression and return or test data it was not meant to reach.
| Severity | Medium |
| CVSS 3.1 | 6.5 CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:N Open in the calculator |
| CVSS 4.0 | 6.9 CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:L/VA:N/SC:N/SI:N/SA:N Open in the calculator |
| CWE | CWE-643: Improper Neutralization of Data within XPath Expressions ('XPath Injection') |
| OWASP Top 10 | A05:2025 Injection |
| MITRE ATT&CK | T1190 Exploit Public-Facing Application |
| Category | Injection |
Description
The application stores data such as user accounts, configuration or catalogue records in XML documents and queries them with XPath expressions built by concatenating values supplied by the user. A quotation mark in the input ends the intended string literal, after which the rest of the input is read as XPath syntax, so a condition such as ' or '1'='1 changes which nodes the expression selects.
XPath has no access control within a document, so an attacker can read any data in the XML document that the expression queries, which can include credentials or personal data where the document is used as a user store. Functions such as count(), string-length() and substring() allow a whole document to be extracted one character at a time even when results are not shown. The attacker may also be able to bypass authentication or other checks that depend on the expression's result. XPath 1.0 cannot change a document, so the impact is normally limited to the confidentiality of that document and to decisions based on it.
XPath injection typically shows itself on a test when a quotation mark causes an XPath error, when an always-true condition bypasses a login or returns all records, or when always-true and always-false conditions give consistently different responses. Scanners, including Burp Suite and OWASP ZAP, report XPath injection from error messages and response differences.
Remediation
Keep XPath expressions fixed and pass user input as values:
- Use an XPath API that supports variables: write each expression in the application code with variables in place of values, and bind the user's input to them, for example with an
XPathVariableResolverin Java or keyword variables to thexpath()method of Python's lxml. - Where the user chooses between kinds of query, map each choice to a complete, fixed expression and reject unknown choices, rather than building paths or predicates from input.
- Validate input against the expected type and format before use, as a second layer of defence.
- Keep XPath errors, expressions and document contents out of responses.
- Where the document holds credentials, store them as salted password hashes rather than in a form that would be useful if extracted.
Confirm the fix by repeating the original tests with quotation marks and always-true conditions, and checking that they are treated as literal values.
References
Add This Finding to Your Library
In Pental this write-up is in Settings, Library. Add to my library copies it into your own, rated in your firm's CVSS version, ready to put in your own words.
Free trialYour trial starts when your portal goes live, not when you sign up, so the time you spend setting up does not count. · No card required
Only need the reporting? Our reporting tool is free and open source, with the same library.