Skip to main content
Updated in v7.0.0 (breaking). SQLVerifier.verify_sql() now returns a DiagnosticResult instead of an ad-hoc dict, and a proven-malicious query is reported as VERIFIED (truth) with a separate admission decision (policy). See the changelog for migration details.
SQL query validation and injection detection.

Overview

The SQL Engine validates queries for:
  • SQL injection patterns
  • Destructive operations
  • Schema compliance
  • Syntax correctness
Verification is static AST analysis (SQLGlot). The engine never connects to a database.

The DiagnosticResult contract

verify_sql() returns a DiagnosticResult with a status, an agent-safe agent_message, structured developer_fields, and a proof_ref bound to the query AST: Proving that a query is malicious is a successful proof, so a malicious query is VERIFIED — not BLOCKED. BLOCKED is reserved for cases where verification itself could not complete, and blocked results never carry a proof_ref. If the DDL schema fails to parse, the incomplete analysis is BLOCKED even when the query itself looks malicious (schema_parse_error takes precedence). agent_message never leaks detection rules, rule IDs, or raw parser output. Rule-level detail lives in developer_fields.

Admission is a separate decision

Do not gate execution on status alone. POST /verify/sql returns an explicit admission field (ADMIT or BLOCKED) alongside the verdict. A malicious query is VERIFIED at the truth layer but BLOCKED at the admission layer. Gate on admission (or developer_fields.is_valid), never on status == "VERIFIED".

Usage

Schema compliance

When you pass schema_ddl, every table the query reads or writes must be declared in that DDL. A reference to an undeclared table is an admission failure: the result is BLOCKED with is_valid: false and constraint_id: "sql_verifier.schema_violation". Names the query defines itself, such as CTEs, are not treated as tables. Without schema_ddl, the engine does not check table names. With schema_ddl, the engine also expands SELECT * (qualified or not, such as SELECT u.*) against the declared columns. If the star covers a sensitive column, the query is not valid (is_valid: false) with a CRITICAL sensitive_column_access issue. Select the non-sensitive columns you need instead of *. Without schema_ddl, the engine can’t tell which columns a star covers.
Before v7.2.3, a query on a table outside the schema was admitted with a warning. See GHSA-q5g6-6cw2-cjm3.

MySQL and MariaDB executable comments

MySQL and MariaDB run SQL placed inside executable comments (/*! ... */, /*!NNNNN ... */, and /*M! ... */). The SQL engine expands these comments before analysis, so injection, destructive-operation, and schema checks apply to the SQL inside them. The engine expands them whatever dialect you declare. developer_fields.executable_comments_expanded is true when the query contained an executable comment.

Injection detection

Detected patterns

Tautologies are detected by value, not by matching fixed text shapes. The engine constant-folds every column-free comparison, so OR 1=1, OR 'a'='a', OR 1=1.0, OR 2>1, and arithmetic forms are all reported as injection_tautology. A WHERE or HAVING clause whose whole predicate folds to true, such as WHERE 1, is flagged too.

Destructive operations

Dangerous functions

New in v8.0.0 (GHSA-45jv-2j44-4c2q)
The SQL engine denies calls to functions that read or write server files, load code, or reach the OS or another server. The list is DANGEROUS_SQL_FUNCTIONS. It covers every dialect, whatever dialect you declare. A call anywhere in a statement, including inside a SELECT, is a CRITICAL issue with type dangerous_function. The query is not valid, sets malicious_classification: true, and is not admitted.
Before v8.0.0, SQLVerifier admitted calls such as load_extension(), pg_read_file(), and xp_cmdshell(). QWEDClient.verify_sql also answered from the local SQLGuard instead of the server. It now always calls POST /verify/sql and returns UNVERIFIABLE when the server can’t be reached. See SQLGuard for the local pre-filter.

Statements are classified across the whole query tree

The engine inspects every node of the parsed query, not just the top-level statement. A data-modifying statement hidden in a CTE or subquery is reported as destructive_command, even though the outer statement is a SELECT:
Administrative and side-effecting statement families are reported as admin_command wherever they appear (see the table below). Beyond that, only read-only query types are admitted at the top level: SELECT, UNION, INTERSECT, EXCEPT, and their WITH and parenthesized forms. Any other top-level statement is reported as non_read_only_statement with is_valid: false, so the engine fails closed on statement types it doesn’t recognize.

Administrative commands

The SQL engine also blocks administrative SQL commands by default, wherever they appear in the query: These statement families are also reported as CRITICAL admin_command issues: COPY (including COPY ... FROM PROGRAM), ATTACH, DETACH, PRAGMA, USE, LOAD DATA, ANALYZE, CACHE, UNCACHE, VACUUM, EXPORT, IMPORT, CALL, EXECUTE, and any statement the SQL parser doesn’t model.
Before v7.3.0, the engine checked only the top-level statement type and matched tautologies by text shape, and SELECT * bypassed the sensitive-column check. See GHSA-6p94-4f75-g2wq and GHSA-6cpx-8jrq-r7h3.
A resource-limit violation is a CRITICAL admission failure but not evidence of malicious intent. Only true-malice issue types set malicious_classification: true.

Supported dialects

  • PostgreSQL
  • MySQL
  • SQLite
  • SQL Server
  • BigQuery