5 min readView Markdown

Why SQL parsers are not a security boundary for AI agents

When teams wire an AI assistant up to PostgreSQL or MySQL, the first safety check they reach for is a SQL filter: inspect the statement before sending it, reject anything that does not start with SELECT, and call it read-only.

That feels reasonable right up until you look at what a real database accepts inside a statement that starts with SELECT.

Where SQL parsing breaks down

A regex or lightweight AST check runs outside the database engine, against a different grammar than the server actually executes. Every mismatch between the parser and the server is a bypass.

  • Writable CTEs. In PostgreSQL, WITH purged AS (DELETE FROM sessions RETURNING id) SELECT count(*) FROM purged is a SELECT at the top level and a DELETE in the body.
  • Side-effecting functions. SELECT reset_account_state(42) or SELECT pg_terminate_backend(pid) parses as an ordinary projection. No parser can tell which stored procedures mutate rows, write files, or kick off background jobs without understanding your schema and extensions.
  • Dialect drift and comments. MySQL versioned comments (/*!50000 ... */), dollar-quoted strings in Postgres, multi-statement strings, COPY ... TO PROGRAM, SELECT ... INTO OUTFILE, and warehouse-specific statements (CALL, MERGE, EXECUTE IMMEDIATE) routinely parse one way in a client library and another way on the server.
  • Privilege confusion. Even if a parser blocked every write, it cannot tell which schemas or tables a caller is supposed to read. An agent helping support should not be able to read payroll or password-reset tokens just because the query is a SELECT.

The deeper problem is architectural: a SQL filter tries to re-implement a database's authorization layer inside a proxy, using a weaker parser and less context than the database already has.

Put the boundary in the database

PostgreSQL, MySQL, Snowflake, and every serious database already ship a battle-tested enforcement mechanism: roles, grants, and read-only transactions.

Create a dedicated role for agents with only the grants they need:

CREATE ROLE swarmvia_ro LOGIN PASSWORD '...';
REVOKE ALL ON SCHEMA public FROM PUBLIC;
GRANT USAGE ON SCHEMA public TO swarmvia_ro;
GRANT SELECT ON TABLE orders, customers, invoices TO swarmvia_ro;

Then run every statement inside a read-only transaction (BEGIN TRANSACTION READ ONLY on PostgreSQL, SET TRANSACTION READ ONLY on MySQL, or a read-only JDBC connection with rollback).

Now look at what happens under every failure mode:

  • A writable CTE fails inside the database engine, regardless of how clever the syntax is.
  • A stored procedure that mutates state fails because the transaction is read-only and the role lacks EXECUTE and UPDATE grants.
  • A query against a restricted table fails because the role has no SELECT grant on it, and source_schema never even reveals the table exists.
  • The refusal comes straight from the database and lands in the access log under the person and session purpose that issued it.

There is no parser to keep in sync with Postgres 18, MySQL 8.4, or Snowflake's latest dialect extension.

What about JDBC drivers and warehouses?

Not every JDBC driver enforces setReadOnly(true) with the same strictness as PostgreSQL's READ ONLY transaction mode. That makes the database role even more important, not less.

On Snowflake, Databricks, BigQuery, or NetSuite, start with a dedicated warehouse role granted SELECT only on the schemas or views agents should see. On swarmvia, Test connection reports what the driver itself declares about read-only enforcement before you grant the source to anyone.

Defense in depth, in the right order

Keeping query timeouts, row limits, concurrency caps, and parameter binding at the edge still matters—an unbounded cross-join on a read-only role can still tie up a replica. That is what an edge layer is good at: bounding load, holding the password so it never lands in .env, attributing every query to a named session, and logging every statement and refusal.

Deciding whether a statement is allowed to touch a table or modify a row belongs in the database. Use the role system you already have, and stop asking a regex to guard production.

Try it on your own systems

Free to start, no credit card. Connect a database, MCP server, or API in a few minutes.

Related reading

More from the blog