Impact
SQLVerifier.verify_sql with a schema (POST /verify/sql, which requires one, and batch SQL items) treats the declared schema as the set of tables a query may touch: a table outside it is BLOCKED (schema_table_missing). The check compares only the last part of a table name, skips any table named like a CTE defined anywhere in the query, and does not check columns. So these queries return VERIFIED · ADMIT.
Checked on main @ ec50c2ac with sqlglot 28.0.0 and 30.31.0, schema CREATE TABLE users (id INT, name TEXT, password TEXT);:
| Dialect | Query | verify_sql |
|---|---|---|
| postgres | SELECT id, name FROM hr.users |
ADMIT |
| tsql | SELECT id, name FROM other_db.dbo.users |
ADMIT |
| postgres | SELECT id FROM (WITH payroll AS (SELECT 1 AS id) SELECT id FROM payroll) a, payroll (the outer payroll is a real table) |
ADMIT |
| oracle | SELECT get_secret FROM users |
ADMIT |
| postgres | SELECT id FROM payroll (control) |
BLOCKED |
Oracle reads an unqualified name with no brackets as a column first, and calls a function of that name with no arguments when no column has it, so SELECT get_secret FROM users calls get_secret.
A caller that relies on the schema to limit what a query reads lets it read another schema's or database's table of a declared name, any table named like a CTE defined anywhere in the query, and, in Oracle, call a function with no arguments.
Affected
qwed (PyPI) through 12.0.0. Reachable from POST /verify/sql, batch SQL items and SQLVerifier.verify_sql with schema_ddl.
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 [("postgres", "SELECT id, name FROM hr.users"),
("postgres", "SELECT id FROM (WITH payroll AS (SELECT 1 AS id) SELECT id FROM payroll) a, payroll"),
("oracle", "SELECT get_secret FROM users"),
("postgres", "SELECT id FROM payroll")]:
print(dialect, q, admission_decision(SQLVerifier().verify_sql(q, ddl, dialect=dialect)).value)
Details
_validate_against_schema (src/qwed_new/core/sql_verifier.py:1307) keeps each declared table by its last name part and compares table.name (:1342), the last part of each table in the query, so hr.users, other_db.dbo.users and users are one table to it. It collects every CTE name in the query (:1340) and skips a table of that name anywhere, whatever the CTE's scope. Columns are not compared with the schema, so a name that is no column of any source is not caught.
Patches
Fixed in 13.0.0. Upgrade with pip install -U "qwed>=13.0.0". With a schema, SQLVerifier compares a table name part by part, as the dialect compares names, counts a CTE name only where the CTE is in scope, and requires every column, in every clause (row counts at any depth included), to be a column of a source in scope; a query with PIVOT or UNPIVOT is refused. An INSERT, UPDATE, DELETE or MERGE is checked as the query that reads what it reads. A declared users no longer covers public.users: declare the name the query uses.
Workarounds
Run verified SQL with a database user that can read only the declared tables.
Advisory page: https://qwedai.tech/security/advisories/ghsa-jm6g-2j2p-cprr
