SKY-D229 · Security in code
XPath injection
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
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
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
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
- Finds
compile(),evaluate()andevaluateExpression()on a receiver whose declared type isjavax.xml.xpath.XPath, and dom4jDocumentHelper.createXPath(). APattern.compile()or a service's ownevaluate()is not matched. - Checks the expression argument (the first one) for request data: servlet request getters, Spring and JAX-RS handler parameters such as
@RequestParamand@PathVariable, and helpers in the file that return them. Number, boolean,UUIDand date parameters never count. - 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.
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
// noqadoes not hide CRITICAL security findings.Java return xpath.evaluate(expr, doc); // skylos: ignore[SKY-D229] expr from FIELDSAccept the findings you already have with
skylos baseline ., then scan with--baselineso only new ones are listed.[tool.skylos] ignore = ["SKY-D229"]turns the rule off for the whole project.