Skip to main content

SKY-D229 · Security in code

XPath injection

Critical

XPath is a query language for XML documents. XPath injection happens when outside text is pasted into an XPath expression instead of being passed as a variable. Skylos reports SKY-D229 when request data reaches the expression of XPath.compile() or XPath.evaluate(), or dom4j DocumentHelper.createXPath().

Languages
Java
Quality gate
counted toward the gate limits; a CRITICAL finding fails the gate on its own (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

A lookup such as //user[name=' + name + '] returns every user when the name is ' or '1'='1. When XML holds accounts or settings, that can bypass a login check or leak the whole document.

What Skylos flags

src/main/java/com/example/DirectoryController.java · JavaReported on line 22
package com.example; import javax.xml.xpath.XPath;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document; @RestControllerpublic class DirectoryController {    private final Document directory;     public DirectoryController(Document directory) {        this.directory = directory;    }     @GetMapping("/email")    public String email(@RequestParam String name) throws XPathExpressionException {        XPath xpath = XPathFactory.newInstance().newXPath();        return xpath.evaluate("//user[name='" + name + "']/email", directory);    }}

What Skylos leaves alone

src/main/java/com/example/DirectoryController.java · Java
package com.example; import javax.xml.xpath.XPath;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document; @RestControllerpublic class DirectoryController {    private final Document directory;     public DirectoryController(Document directory) {        this.directory = directory;    }     @GetMapping("/email")    public String email(@RequestParam String name) throws XPathExpressionException {        XPath xpath = XPathFactory.newInstance().newXPath();        xpath.setXPathVariableResolver(variable -> name);        return xpath.evaluate("//user[name=$user]/email", directory);    }}

Why Skylos leaves it alone

The expression is a fixed string with an XPath variable, $user. The resolver supplies the name as a value, so quotes in it are compared as text and can never change the query.

How Skylos decides

  1. Finds compile(), evaluate() and evaluateExpression() on a receiver whose declared type is javax.xml.xpath.XPath, and dom4j DocumentHelper.createXPath(). A Pattern.compile() or a service's own evaluate() is not matched.
  2. Checks the expression argument (the first one) for request data: servlet request getters, Spring and JAX-RS handler parameters such as @RequestParam and @PathVariable, and helpers in the file that return them. Number, boolean, UUID and date parameters never count.
  3. Reports CRITICAL when request data reaches the expression, directly or through string building and assignments.

How to fix it

Keep the XPath expression fixed and pass outside values through variables with an XPathVariableResolver. If a value must become part of the expression, check it against a strict allowlist first.

src/main/java/com/example/DirectoryController.java · Java · the fix, against the flagged file
package com.example; Added: import java.util.Map;Added: import javax.xml.namespace.QName;import javax.xml.xpath.XPath;import javax.xml.xpath.XPathExpressionException;import javax.xml.xpath.XPathFactory;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.w3c.dom.Document; @RestControllerpublic class DirectoryController {    private final Document directory;     public DirectoryController(Document directory) {        this.directory = directory;    }     @GetMapping("/email")    public String email(@RequestParam String name) throws XPathExpressionException {Added:         Map<QName, Object> values = Map.of(new QName("user"), name);        XPath xpath = XPathFactory.newInstance().newXPath();Removed:         return xpath.evaluate("//user[name='" + name + "']/email", directory);Added:         xpath.setXPathVariableResolver(values::get);Added:         return xpath.evaluate("//user[name=$user]/email", directory);    }}

When it can be wrong

  • A value checked against an allowlist before it is pasted into the expression is still reported: the check is not recognised.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Mark a reviewed line and say why. A bare // noqa does not hide CRITICAL security findings.

    Java
    return xpath.evaluate(expr, doc); // skylos: ignore[SKY-D229] expr from FIELDS
  • Accept the findings you already have with skylos baseline ., then scan with --baseline so only new ones are listed.

  • [tool.skylos] ignore = ["SKY-D229"] turns the rule off for the whole project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.