GHSA-3GPX-VWR3-XVWX
SQLChatAgent SELECT-only allowlist bypassed by data-modifying CTE and SELECT...INTO
Texto original en inglés · Ver en español
CVSS:3.1/AV:N/AC:L/PR:L/UI:N/S:U/C:N/I:H/A:H
Fuente: GitHub
Ciclo de vida de la vulnerabilidad
| Fecha | Fase | Evento | Detalle |
|---|---|---|---|
| Divulgación | Publicada | GHSA-3GPX-VWR3-XVWX divulgada · CVSS 8.1 (GitHub) | |
| Remediación | Parche 0.65.14 | langroid · 0 d desde divulgación | |
| Monitoreo | Detectada | Ingresó al índice | |
| Monitoreo | Verificada | Última verificación contra las fuentes |
## Summary
`SQLChatAgent._validate_query` enforces a SELECT-only statement-type allowlist (the primary control introduced by the CVE-2026-25879 fix) by testing ONLY the top-level parse node against `kind_map` via `isinstance`. PostgreSQL data-modifying CTEs (`WITH x AS (DELETE/UPDATE/INSERT ... RETURNING ...) SELECT ...`) and `SELECT ... INTO newtbl ...` parse with a top-level node of `Select`, so `kind="SELECT"` is in the default allowlist and the query is accepted and committed — while the embedded `DELETE`/`UPDATE`/`INSERT`/DDL is fully executed by the database engine. This defeats the exact "safe even with a write-capable role" guarantee of the SELECT-only default, needing only ordinary write privileges (a lower bar than the original CVE (CVE-2026-25879)'s superuser `COPY...PROGRAM`).
This targets the ALLOWLIST layer, which is untouched by all three prior blocklist advisories (CVE-2026-25879, CVE-2026-50180, CVE-2026-54760).
## Root Cause
`langroid/agent/special/sql/sql_chat_agent.py`, `_validate_query`: the statement-type classification loop (lines ~667-674) evaluates `isinstance(stmt, k)` against only the top-level parsed statement node. It never walks the AST for nested `Insert`/`Update`/`Delete`/`Merge` nodes, and it does not detect the `SELECT ... INTO` table-creation form. `sqlglot.parse(q, read="postgres")[0]` returns a top node of `Select` for both writable-CTE and `SELECT ... INTO` queries, so `kind` resolves to `"SELECT"` (in the default `allowed_statement_types=["SELECT"]`) and validation passes.
## Impact
Arbitrary data tampering/destruction: mass `DELETE`/`UPDATE`/`INSERT` corrupts application state (I:H + A:H). Not RCE by itself — the recursive AST function-name check (`find_all`) and the regex blocklist still catch `pg_read_file`/`COPY...PROGRAM` even when nested inside a CTE (verified asymmetry). Requires a write-capable DB role plus (direct or indirect) prompt injection shaping the agent's `RunQueryTool.query`.
## Proof of Concept
LLM emits (via direct or indirect prompt injection):
RunQueryTool.query = "WITH x AS (DELETE FROM users RETURNING *) SELECT count(*) FROM x"`_validate_query` returns None (valid) then `session.execute(text(query))` then `session.commit()` and all rows in `users` are removed and committed. `SELECT * INTO evil FROM users` similarly passes and creates/populates a table.
## Attack Chain
1. Entry: LLM emits `RunQueryTool.query` (free-form `str`, `utils/tools.py:12`; tool enabled by default via `enable_message([RunQueryTool, ...])`). Guard: none at entry. Bypass proof: field is an unconstrained string.
2. Regex blocklist (lines ~628-641). Guard: 15 dangerous patterns. Bypass proof: payload contains no `COPY...PROGRAM`/`pg_*`/`lo_*`/`load_*`/`create function` — regex matches nothing (verified ALLOWED).
3. Statement-type allowlist (lines ~643-674). Guard: `kind not in {{"SELECT"}}`. Bypass proof: `sqlglot.parse(q, read="postgres")[0]` top node is `Select`; `find_all(Insert,Update,Delete,Merge)` returns nested `['Delete']`/`['Update']`/`['Insert']` which the top-level `isinstance` loop never inspects; `kind="SELECT"` passes. `SELECT...INTO` gives `into=True`, still `Select` top node.
4. AST function-name check (lines ~690-704). Guard: `_is_dangerous_function_name`. Bypass proof: no dangerous function present; `_called_function_names` = [].
5. Sink: `session.execute(text(query))` + `session.commit()` (lines ~735-736). Impact: nested write / `SELECT...INTO` executes and commits — arbitrary data destruction/tampering under the default "read-only" configuration.
## Bypass Evidence
`verify_harness.py` (faithful replica of verbatim HEAD `_validate_query` logic, sqlglot 30.12.0): all writable-CTE and `SELECT...INTO` payloads return ALLOWED. Node-shape probe: top=`Select`; nested writes `['Delete']`/`['Update']`/`['Insert']`; `SELECT...INTO` gives `into=True`. Asymmetry: nested write in a CTE is allowed, while nested `pg_read_file` in the same CTE is REJECTED (proving the func-name check is recursive but the statement-type check is top-level-only). The replica correctly REJECTS the GHSA-6xc5 quoted/comment/schema-qualified `pg_read_file` forms — it is not under-blocking.
## Affected Versions
`<= 0.65.13` (pip package `langroid`). Verified on latest release tag 0.65.13 (== HEAD); `git log 0.65.13..HEAD -- sql_chat_agent.py` is empty. Default secure config affected (`allow_dangerous_operations=False`, `allowed_statement_types=["SELECT"]`). Note: the shipped `kind_map` references `sqlglot.exp.Alter` which requires sqlglot >= 26.0.0; on sqlglot < 26 `_validate_query` fails closed (agent unusable, no bypass). The bypass holds on any modern sqlglot (>= 26).
## Suggested Fix
Walk the full AST for nested `Insert`/`Update`/`Delete`/`Merge` nodes and detect the `into` attribute on `Select`, rather than classifying only the top-level node. Reject any statement whose AST contains a data-modifying node regardless of nesting.
---
Reported by zx (Jace) — GitHub: @manus-use
Incluye detalles técnicos publicados por el investigador. Úselos solo para validar y corregir sus propios sistemas.
Desglose de métricas
Rangos afectados
Estado del parche por paquete
| Paquete / producto | Estado | Corregida en | Desde divulgación | Última versión |
|---|---|---|---|---|
pypilangroid |
Corregida | 0.65.14 | 0 d | 0.69.22 no afectada |
0.65.14Arsenal público
Sin exploits, módulos ni PoC públicos conocidos a la fecha de verificación.
https://github.com/langroid/langroid/security/advisories/GHSA-3gpx-vwr3-xvwxLas referencias externas se muestran para copiar: cve.llama.ec no lo redirige a otros sitios.
