CAPEC-83 - XPath Injection

An attacker can craft special user-controllable input consisting of XPath expressions to inject the XML database and bypass authentication or glean information that he normally would not be able to. XPath Injection enables an attacker to talk directly to the XML database, thus bypassing the application completely. XPath Injection results from the failure of an application to properly sanitize input used as part of dynamic XPath expressions used to query an XML database. In order to successfully inject XML and retrieve information from a database, an attacker:






  • Attack Methods 1
  • Injection
  • Purposes 2
  • Penetration
  • Exploitation
  • Sec Principles 3
  • Reluctance to Trust
  • Failing Securely
  • Defense in Depth
  • Scopes 2
  • Gain privileges / assume identity
  • Authorization
  • Access_Control
  • Confidentiality
  • Read application data
  • Confidentiality

Low level: XPath Injection shares the same basic premises with SQL Injection. An attacker must have knowledge of XPath syntax and constructs in order to successfully leverage XPath Injection

XPath queries used to retrieve information stored in XML documents

User-controllable input not properly sanitized before being used as part of XPath queries


The attacker tries to inject characters that can cause an XPath error, such as single-quote ('), or content that may cause a malformed XPath expression. If the injection of such content into the input causes an XPath error and the resulting error is displayed unfiltered, the attacker can begin to determine the nature of input validation and structure of XPath expressions used in queries.

Never Use Input as Part of a Directive to any Internal Component

Handle All Errors Safely

Step 1 -

Determines the user-controllable input that is used without proper validation as part of XPath queries.

Step 2 -

Determines the structure of queries that accept such input.

Step 1 -

Crafts malicious content containing XPath expressions that is not validated by the application and is executed as part of the XPath queries..

Special characters in user-controllable input must be escaped before use by the application.

Only use parameterized XPath expressions to query the XML database.

Custom error pages must be used to handle exceptions such that they do not reveal any information about the architecture of the application or the database.

Strong input validation - All user-controllable input must be validated and filtered for illegal characters as well as content that can be interpreted in the context of an XPath expression. Characters such as a single-quote(') or operators such as or (|), and (&) and such should be filtered if the application does not expect them in the context in which they appear. If such content cannot be filtered, it must at least be properly escaped to avoid them being interpreted as part of XPath expressions.

Use of parameterized XPath queries - Parameterization causes the input to be restricted to certain domains, such as strings or integers, and any input outside such domains is considered invalid and the query fails.

Use of custom error pages - Attackers can glean information about the nature of queries from descriptive error messages. Input validation must be coupled with customized error pages that inform about an error without disclosing information about the database or application.