XPath Injection
ID |
xpath_injection |
Severity |
critical |
Kind |
Injection |
CWE |
643 |
Description
XPath Injection occurs when an application incorporates user-controllable data into XPath queries without proper sanitization, allowing an attacker to interfere with the query logic. The attacker can craft malicious input to break out of the intended data context and alter the structure of the surrounding XPath expression. This can be used to bypass authentication, enumerate the structure of the underlying XML document, or extract its entire contents.
Rationale
A successful XPath Injection attack can allow an unauthenticated attacker to retrieve sensitive data such as credentials, personal information, or any other content stored in the queried XML document. Unlike SQL Injection, XPath has no access-control mechanism, so the entire XML document is accessible once the injection succeeds. This makes it possible to extract a complete copy of the data, compromise authentication logic, or alter the application behavior by manipulating the query results.
Remediation
Use parameterized or precompiled XPath queries so that user input is treated strictly as data and cannot alter the query structure. This is the most effective defense because it enforces a clear separation between code and data.
Validate all user input before incorporating it into XPath expressions. Accept only expected characters such as short alphanumeric strings, and reject input containing XPath metacharacters like single quotes, double quotes, slashes, brackets, parentheses, and the at sign.
Configure generic error messages that do not reveal details about the XML structure or the XPath query. Detailed error output can help an attacker refine a blind injection attack.
References
-
XPath Injection, in OWASP Community.
-
Blind XPath Injection, in OWASP Community.