Looking for the vulnerability index of Invicti's legacy products?
XPath injection vulnerability - Vulnerability Database

XPath injection vulnerability

Description

This application is vulnerable to XPath Injection, a security flaw that occurs when user-supplied input is directly incorporated into XPath queries without proper validation or sanitization. XPath is a query language used to navigate and select data from XML documents. When attackers inject malicious XPath syntax through input fields, they can manipulate queries to bypass authentication, access unauthorized data, or extract the entire XML document structure. This vulnerability is similar in nature to SQL Injection but targets XML data stores instead of relational databases.

Remediation

Implement the following security measures to prevent XPath Injection attacks:

1. Use Parameterized XPath Queries:
Utilize parameterized XPath queries or precompiled expressions that separate data from query logic. Many XML libraries support this approach.

2. Input Validation and Sanitization:
Validate all user input against a strict allowlist of acceptable characters. Reject or sanitize input containing XPath metacharacters such as: ' " / [ ] ( ) = @ , :

// Example: Input validation (Java)
String userInput = request.getParameter("username");
if (!userInput.matches("^[a-zA-Z0-9_]{3,20}$")) {
    throw new SecurityException("Invalid input");
}

3. Escape Special Characters:
If parameterization is not available, properly escape XPath special characters before incorporating user input into queries.

// Example: Using parameterized XPath (Java with Saxon)
XPathExpression expr = xpath.compile("//user[username=$uname]");
expr.setVariable("uname", userInput);

4. Principle of Least Privilege:
Ensure the application accesses XML data with minimal necessary permissions to limit the scope of potential data exposure.

5. Security Testing:
Regularly test input fields with XPath injection payloads during security assessments to verify proper input handling.

Related Vulnerabilities