# VNX-652: XQuery injection

Reports a Java XQuery (XQJ, Saxon, BaseX, eXist/XML:DB, MarkLogic XCC) query built by concatenation or formatting of a non-constant value.

| Field | Value |
|---|---|
| Rule ID | `VNX-652` |
| Severity | Medium |
| SARIF level | `warning` |
| Kind | `sast` |
| Languages | Java |
| CWE | [CWE-652](https://cwe.mitre.org/data/definitions/652.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:M/AP:A/AL:A/IC:N/FC:LT/RP:N/RL:A/AV:I/AS:N/IN:A/SC:A/BI:M/DI:H/EX:M/EC:N/P:C` |
| Tags | `xquery` `injection` `cwe-652` |
| Source | [`internal/sast/rules/vnx-652.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-652.rego) |

## Overview

VNX-652 maps to [CWE-652: Improper Neutralization of Data within XQuery Expressions](https://cwe.mitre.org/data/definitions/652.html). In Java files that use an XQuery API (`javax.xml.xquery`, `net.sf.saxon`, `org.basex`, `org.xmldb`, `com.marklogic.xcc`) it reports `executeQuery`, `prepareExpression`, `executeCommand`, `compile`, `query`, `execute`, `newAdhocQuery`, `new XQuery(...)` and `new QueryProcessor(...)` whose argument:

- concatenates a variable, parameter or method result into a string literal,
- uses `String.format`, `.formatted` or `MessageFormat.format`, or
- is a variable or `StringBuilder.toString()` built that way in the ten lines above (including a concatenation broken across lines).

Not reported: external variables bound with `bindString`/`bindObject`, constant queries and concatenation of literals or `UPPER_CASE` constants, SQL passed to JDBC, files without an XQuery API, comments and the call inside a string literal.

## Why this matters

A value such as `' or '1'='1` or `'] | doc('secrets.xml')//*[' ` rewrites the query, returning documents the caller should not see, and with some engines (BaseX, MarkLogic) reaching file and HTTP functions.

## What gets flagged

```java
// FLAGGED
return expr.executeQuery("for $b in doc('books.xml')//book where $b/author = '" + req.getParameter("author") + "' return $b");

String query = "doc('users.xml')/users/user[name='" + username + "']";
XQPreparedExpression pe = conn.prepareExpression(query);      // FLAGGED

return compiler.compile(String.format("//sale[region='%s']", region));   // FLAGGED
```

```java
// NOT FLAGGED: external variable
XQPreparedExpression pe = conn.prepareExpression("declare variable $author external; //book[author = $author]");
pe.bindString(new QName("author"), author, null);
```

## Remediation

1. Declare `external` variables in the query and bind values (`bindString`, Saxon `XQueryEvaluator.setExternalVariable`, BaseX `bind`).
2. Never build the query text from request data.

## References

- [CWE-652: Improper Neutralization of Data within XQuery Expressions](https://cwe.mitre.org/data/definitions/652.html)
- [OWASP Injection Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Injection_Prevention_Cheat_Sheet.html)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-652
// nosec vnx-652
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-652 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
