AAK-LLM-SQL-RCE-001: LLM-generated SQL executed on an RCE-capable database role
- Severity
- CRITICAL
- Category
- Taint Analysis (
taint-analysis) - Scanner
agent_audit_kit/scanners/llm_sql_rce.py- Auto-fixable
- No.
- SARIF name
LlmSqlRce
What it detects
An agent / LLM application feeds model-generated SQL into a database executor whose connection role holds code-execution or filesystem privileges, turning prompt-injected SQL into remote code execution. Two arms fire this rule: (a) an LLM-output value reaches a SQL-execution sink (cursor.execute / conn.execute / SQLAlchemy text(...) / a TS .query()/.raw() call) as the query itself — not as a bound parameter — with no allow-list or query-validation step; and (b) a DB connection string / role that grants the dangerous primitives (a superuser connection account, or a literal PostgreSQL COPY ... FROM PROGRAM / pg_execute_server_program, MySQL FILE / INTO OUTFILE / LOAD_FILE, or MS SQL xp_cmdshell) inside an LLM/agent context. CVE-2026-25879 is the documented instance: a text-to-SQL chat agent ran model output on a superuser connection and a prompt-injected COPY ... FROM PROGRAM produced a shell. CWE-94 (Code Injection) / CWE-89 (SQL Injection) chained to CWE-78 (OS Command Injection) through CWE-250 (Execution with Unnecessary Privileges). This is distinct from AAK-TAINT-005, where the tainted source is a tool parameter string-formatted into SQL rather than LLM output landing on an RCE-capable role.
How to fix
Never execute LLM-generated SQL directly. Constrain the model to parameterised, allow-listed queries (validate with sqlglot/sqlparse and reject anything that is not a single read-only SELECT against an approved table set), and run every agent query on a dedicated least-privilege, read-only role that lacks pg_execute_server_program / FILE / xp_cmdshell and cannot COPY ... FROM PROGRAM. Treat the DB role as the last line of defence: even a perfect prompt filter must not sit in front of a superuser connection.
To confirm the finding is gone, report only this rule:
agent-audit-kit scan . --rules AAK-LLM-SQL-RCE-001
Limitations
The flow arm follows model output through assignments into the first argument of a call named execute, executemany, executescript or exec_driver_sql; LangChain's db.run(sql) is not a sink. Any identifier, import or string in the file containing sqlglot, sqlparse, allowlist or whitelist switches that arm off, so sqlparse.format(sql, reindent=True), which only pretty-prints, counts as validation. The privilege arm judges the database role from a literal connection URI or literal SQL text in the source: a DSN read from os.environ is not seen.
References
- CVE: CVE-2026-25879
- OWASP Agentic Top 10: ASI02 (Tool Misuse and Exploitation), ASI05 (Unexpected Code Execution (RCE))
- Adversa AI MCP Top 25 (AAK-defined ids): ADV-INJECT-07 (Tool Param SQL Injection)
All rules. This page is generated from the rule registry when the documentation is built; to change it, change the rule in agent_audit_kit/rules/builtin.py. To give the rule a hand-written write-up, add docs/rules/AAK-LLM-SQL-RCE-001.md: the page then shows that text, with the registry reference appended under it.