Impact
POST /verify/sql returns VERIFIED, is_valid: true and admission ADMIT for queries that read every row or every column of a protected table. Schema: CREATE TABLE users (id INT, name TEXT, password TEXT);
| Dialect | Query | main @ d4ca8dc |
|---|---|---|
| sqlite | SELECT id, name FROM users WHERE id = 5 OR name LIKE '%' |
VERIFIED, is_valid: true, ADMIT |
| sqlite | SELECT id, name FROM users WHERE id = 5 OR id = id + 0 |
VERIFIED, is_valid: true, ADMIT |
| sqlite | SELECT id, name FROM users WHERE id = 5 OR abs(id) >= 0 |
VERIFIED, is_valid: true, ADMIT |
| postgres | SELECT row_to_json(u) FROM users u |
VERIFIED, is_valid: true, ADMIT |
| mysql | SELECT id FROM users WHERE id = 1 --1 OR 1=1 |
VERIFIED, is_valid: true, ADMIT |
| sqlite | ... WHERE id = 5 OR 1 = 1 (control) |
VERIFIED, is_valid: false, BLOCKED |
| postgres | SELECT password FROM users (control) |
VERIFIED, is_valid: false, BLOCKED |
| mysql | ... WHERE id = 1 OR 1=1 (control) |
VERIFIED, is_valid: false, BLOCKED |
The same results on sqlglot 28.0.0 and 30.7.0. The image pins 30.25.0, which was not tested.
Affected
qwed (PyPI) through 10.0.0. Reproduced on main @ d4ca8dc.
Reachable from POST /verify/sql, batch SQL items and SQLVerifier.verify_sql.
The SDK's SQLGuard.verify_query is affected by the MySQL comment form too. It does not use _AMBIGUOUS_TOKENIZATION, so with sqlglot 28.0.0 it returns verified: True for SELECT 1 --1; DROP TABLE users (dialect="mysql") as one read-only statement, while MySQL reads --1 as - -1 and, where multiple statements are allowed, runs DROP TABLE users as a second one. sqlglot 30.31.0 reads --1 as MySQL does.
Reproduction
from qwed_new.core.sql_verifier import SQLVerifier
from qwed_new.core.diagnostics import admission_decision
ddl = "CREATE TABLE users (id INT, name TEXT, password TEXT);"
for dialect, q in [("sqlite", "SELECT id, name FROM users WHERE id = 5 OR name LIKE '%'"),
("sqlite", "SELECT id, name FROM users WHERE id = 5 OR id = id + 0"),
("postgres", "SELECT row_to_json(u) FROM users u"),
("mysql", "SELECT id FROM users WHERE id = 1 --1 OR 1=1")]:
dr = SQLVerifier().verify_sql(q, ddl, dialect=dialect)
print(dialect, q, dr.status.value, dr.developer_fields.get("is_valid"), admission_decision(dr).value)
Details
- Tautologies that read a column. Constant predicates are folded and refused, but a predicate that reads a column is judged against known shapes: self-comparison is caught only for a bare column on both sides (
_self_comparison,src/qwed_new/core/sql_verifier.py:295-300).name LIKE '%',id = id + 0andabs(id) >= 0are true for every non-null row and pass. - Whole-row reads. Protected columns are matched by name. A whole-row reference (
row_to_json(u), orSELECT u FROM users u) returns thepasswordcolumn without naming it. - MySQL comments. MySQL treats
--as a comment only when whitespace follows it; sqlglot drops--1 OR 1=1as a comment for every dialect._AMBIGUOUS_TOKENIZATION(src/qwed_new/core/sql_verifier.py:66) describes this case but is used only by the stacked-query check (:88), so the MySQL server would runOR 1=1that the verifier never saw. The SDK'sSQLGuarddoes not use it at all.
Patches
Fixed in 11.0.0. Upgrade with pip install -U "qwed>=11.0.0". The operands of an OR, XOR or IN list in a filter must each restrict one column to values, and a negated AND is refused (sql_verifier.unrestricted_or). Whole-row references (a table or alias used as a column, COLUMNS(), positional columns and similar) are refused where the schema gives the table a protected column. A query that sqlglot and a database can split into tokens differently is BLOCKED (sql_verifier.ambiguous_tokenization), and SQLGuard refuses it with AMBIGUOUS_TOKENS.
Advisory page: https://qwedai.tech/security/advisories/ghsa-xpfc-qm4c-xf6q
