A simple diagram of the XPath Injection risk path
Risk path: an input or context crosses a security boundary.
A simple three-step test path for XPath Injection
Testing path: establish a baseline, make one controlled change, and verify the effect.

01Issue

Overview

XPath is a language for selecting elements from XML documents. XPath injection occurs when application input is inserted into an XPath expression and changes which nodes the expression selects or what condition it evaluates. It resembles SQL injection in concept, but XPath syntax and the data being queried are different.

Common causes

  • The application concatenates user input into an XPath expression.
  • Special characters are not escaped for the exact XPath context.
  • An XPath result is treated as proof of identity or permission without a separate access check.
  • Different search and login paths construct expressions differently.

References: CWE-643: XPath Injection ↗OWASP WSTG: Testing for XPath Injection ↗

02Examples

EXAMPLE

Illustrative search: a feature looks up a test user by name=alex. Compare the normal value with a harmless metacharacter-containing value and inspect which test XML record is returned. A parser error only shows that the expression changed or became invalid; it does not prove protected data was exposed.

03Impact

User input must reach a dynamically built XPath expression, and the changed selection or condition must affect information returned or a decision made by the application.

A confirmed issue may expose XML records or change a login or lookup decision. An expression error or a different page alone does not prove access to protected data; verify the actual returned nodes or action.

04Testing

Test cases

1. Check whether input changes an XML query

Compare a normal search with a controlled expression using known XML test records. Inspect which records are returned.

Example: An XPath error is a clue that input reaches a query. It does not by itself prove an authorization bypass.

GET /api/search?name=test%27 HTTP/1.1
Host: app.example.test

2. Compare literal and structured values

Submit special characters in a test value and compare the response with the baseline. Review the constructed XPath expression where code or logs are available.

Example: An apostrophe may be part of a legitimate name. Check whether it remains data or changes query structure.

POST /api/search HTTP/1.1
Host: app.example.test
Content-Type: application/json
 
{"name":"O'Brien"}

3. Review XML query construction

Check authentication and search paths that build XPath expressions, including stored values used later. Confirm the actual query and returned test records.

Example: A parser error alone does not show that data was disclosed. Report the response and access that were demonstrated.

Testing references: CWE-643: XPath Injection ↗OWASP WSTG: Testing for XPath Injection ↗

05Mitigation

Use XPath variable binding where supported instead of concatenating strings. Otherwise, apply correct XPath literal escaping for the exact context and validate the expected input format. Keep authorization checks separate from query selection and limit the data the XML document contains.

Retest

Repeat lookup and login cases using normal and metacharacter-containing inputs. Confirm the query returns only intended records and invalid values do not change authentication or access decisions.