Security

    XPath injection in mobile backends

    A 2026 view of XPath injection where unsanitized input adds XPath syntax to make a query always match a user node, contrasted with parameterized XPath binding input as a value

    If a mobile app's backend stores data or configuration in XML and queries it with XPath, building those queries from user input invites the same problem as SQL injection in a different language. XPath injection lets an attacker inject XPath syntax through unsanitized input to change which nodes a query selects, bypassing authentication or reading data they should not. It is one of the less common injection flaws, because not every backend uses XML and XPath, but where they are used, often for an XML-based user store or configuration, the risk is real and the fix mirrors the rest of the injection family. Here is what XPath injection is, how it shows up, and how to prevent it.

    Short answer

    XPath injection is a vulnerability where untrusted input is placed into an XPath query, used to select nodes from an XML document, without being parameterized or sanitized, letting an attacker inject XPath syntax that changes which nodes the query returns. Per OWASP, this can bypass authentication, return unauthorized data, or alter the query's logic, much like SQL injection but against XML data. It affects mobile apps whose backend queries XML, for example an XML-based user store or configuration, using user-supplied values. The fix is to use parameterized XPath with variable binding instead of building queries by concatenating input, validate and escape input, and apply least privilege. Keep user input as a value the query compares against, never as part of the XPath expression itself.

    What you should know

    • XPath injection manipulates an XML query: via unsanitized untrusted input.
    • It can bypass authentication: making a query select nodes it should not.
    • It can expose XML data: returning nodes the user should not see.
    • It affects backends that query XML with XPath: an XML store or configuration.
    • The fix is parameterized XPath: input as a value, not query syntax.

    What is XPath injection?

    It is injecting XPath syntax through untrusted input used to build an XML query. XPath is a language for selecting nodes from an XML document, with its own syntax, expressions, operators, and predicates that match elements and attributes by value. When an application builds an XPath query by inserting user input, for instance a username and password checked against an XML user store, without parameterizing it, an attacker can include XPath syntax in their input so that it is interpreted as part of the expression rather than as a plain value. That lets them change which nodes the query selects. The canonical result is authentication bypass: crafted input turns a query that should match a specific user with a specific password into one whose condition is always true, so it selects a user node regardless of the actual credentials. The root cause is the familiar one across injection flaws, untrusted input placed into a structured query without separating data from the query syntax, here the syntax being XPath.

    How does it manifest?

    In forms paralleling other injection flaws. The table lists them.

    FormWhat happens
    Authentication bypassA crafted condition that always matches a user node
    Unauthorized data accessThe query selects nodes the user should not see
    Logic manipulationInjected operators change the query's conditions
    Predicate abuseInjected predicates broaden or alter node selection
    Blind injectionInferring XML data from response differences

    The highest-impact form is authentication bypass: when a login query is built from a username and password and checked against an XML document, injected XPath can make the matching condition always true, selecting a valid user node without correct credentials. Beyond authentication, injecting into a query that reads XML data can return nodes the user should not have access to, by broadening or altering the selection. Injected operators can change the logical structure of the expression, and injected predicates can change which nodes are matched. As with SQL and LDAP injection, blind variants exist, where an attacker infers the document's contents from how responses differ when conditions are true or false. Every form comes back to the same gap, user input treated as XPath syntax rather than as a value the query compares against.

    How do you prevent it?

    Parameterize the query, validate input, and limit privilege. The core defense, as with SQL injection, is to separate data from query: use parameterized XPath with variable binding, where user input is bound as a value the expression compares against rather than concatenated into the XPath string, so injected syntax is treated as literal data and cannot change the expression's structure. Where your XPath or XQuery processor supports variables, use them instead of building the query by string concatenation. Validate input against what it should be, constraining it to an expected format, and escape XPath special characters if you must include input in an expression, though parameterization is preferable. Apply least privilege to the data the query can reach, so a successful injection is limited in scope, and consider whether an XML store and XPath are the right choice for sensitive data like credentials at all. Enforce this on the server, since the attacker supplies the input by calling your API directly. The principle is identical to the rest of the injection family: keep untrusted input as a bound value, never as part of the query expression.

    What to watch out for

    The first trap is building an XPath query by concatenating a username, password, or search term into it, which lets injected XPath bypass authentication or expose data; use parameterized XPath with variable binding. The second is assuming XML and XPath are immune because they are not SQL, when the same injection pattern applies in XPath syntax. The third is storing credentials in an XML document queried with input-built XPath, a high-risk pattern. XPath injection is server-side, so a pre-submission scan such as PTKD.com (https://ptkd.com), which reads the binary against OWASP MASVS, surfaces the endpoints and parameters your app sends as the surface to protect, while the query parameterization and validation are yours to implement on the backend.

    What to take away

    • XPath injection places untrusted input into an XPath query without parameterizing it, letting an attacker inject XPath syntax to bypass authentication, expose XML nodes, or alter the query.
    • It affects mobile apps whose backend queries XML with XPath using user input, the SQL-injection pattern applied to XML data.
    • Prevent it by using parameterized XPath with variable binding instead of string concatenation, validating and escaping input, applying least privilege, and reconsidering XML for sensitive stores like credentials.
    • XPath injection is server-side; use a pre-submission scan such as PTKD.com to surface the parameters your app sends, then parameterize and validate them on the backend.
    • #xpath-injection
    • #injection
    • #xml
    • #authentication
    • #backend
    • #owasp
    • #mobile

    Frequently asked questions

    What is XPath injection?
    It is injecting XPath syntax through untrusted input used to build an XML query. XPath selects nodes from an XML document using its own syntax of expressions, operators, and predicates. When an application builds an XPath query by inserting user input, such as a username and password checked against an XML user store, without parameterizing it, an attacker can include XPath syntax so their input is interpreted as part of the expression rather than a plain value, changing which nodes the query selects. The root cause matches SQL injection: untrusted input placed into a structured query without separating data from the query syntax.
    How does XPath injection bypass authentication?
    If a login query built from a username and password is checked against an XML document, injected XPath can make the matching condition always true, so the query selects a valid user node regardless of the actual credentials. The attacker supplies input containing XPath syntax that alters the expression's logic rather than serving as a value to compare. Because the backend trusted the input as data but it became part of the expression, the query selects a user node it should not, letting the attacker in. Parameterizing the query, binding input as a value, neutralizes this.
    Which apps are affected by XPath injection?
    Mobile apps whose backend stores data or configuration in XML and queries it with XPath using user-supplied values, for example an XML-based user store for authentication or an XML configuration searched with input. It is less common than SQL injection because not every backend uses XML and XPath, but where they are used the risk is real. The vulnerability is server-side, in how the backend builds the XPath query, and an attacker reaches it by calling the API directly with crafted input, so any backend that constructs XPath from user input without parameterizing it is exposed.
    How do I prevent XPath injection?
    Separate data from the query. Use parameterized XPath with variable binding, where user input is bound as a value the expression compares against rather than concatenated into the XPath string, so injected syntax is treated as literal data and cannot change the expression's structure. Where your XPath or XQuery processor supports variables, use them instead of string concatenation. Validate input against an expected format, escape XPath special characters if you must include input directly, apply least privilege to what the query can reach, and reconsider XML for sensitive stores like credentials. Enforce this server-side.
    Is XML safe from injection because it isn't SQL?
    No. The absence of SQL does not make an XML query immune; XPath injection simply uses XPath syntax instead, with its own operators and predicates, to change which nodes a query selects. Assuming XML and XPath are inherently safe is a common mistake that leads to building queries from raw input. The same discipline applies: use parameterized XPath, validate input, apply least privilege, and avoid putting sensitive data like credentials in an XML store queried with input-built XPath. A pre-submission scan such as PTKD.com surfaces the parameters your app sends as the surface to protect on the backend.

    Keep reading

    Scan your app in minutes

    Upload an APK, AAB, or IPA. PTKD returns an OWASP-aligned report with copy-paste fixes.

    Try PTKD free