sqlguardllm
Health Warn
- License — License: Apache-2.0
- Description — Repository has a description
- Active repo — Last push 0 days ago
- Low visibility — Only 8 GitHub stars
Code Pass
- Code scan — Scanned 12 files during light audit, no dangerous patterns found
Permissions Pass
- Permissions — No dangerous permissions requested
No AI report is available for this listing yet.
The SQL firewall for LLM apps: validates SQL written by AI and rewrites it read-only, allowlisted, row-filtered and limited. 32 dialects, built on sqlglot.
The SQL firewall for LLM apps.
Your model writes SQL. sqlguardllm makes sure it can only read what it's allowed to read: one read-only statement, allowlisted tables and columns, row-level filters per user, and a row limit, in any of the 32 dialects sqlglot speaks.
[!NOTE]
Status: alpha.0.4.0a1is on PyPI (pip install sqlguardllm,import sqlguardllm), with the rules of docs/DESIGN.md, optional complexity limits and a 107-case adversarial corpus. The API may still change before 1.0; violation codes are stable. ⭐ Star the repo to follow along, and tell us what you think in Discussions.
Why
LLM apps that talk to databases (text-to-SQL, data agents, MCP database tools) all share the same risk: the model writes SQL, and someone will eventually make it write the wrong SQL. Through a prompt injection, a malicious row in your own data, or a plain hallucination.
The usual defense is a keyword blocklist. It fails in both directions:
| SQL | Keyword filter ("DELETE" in sql) |
What it actually is |
|---|---|---|
SELECT 'x; DROP TABLE invoices' AS note |
❌ Blocks it | A harmless read with a string literal |
WITH d AS (DELETE FROM invoices RETURNING *) SELECT * FROM d |
✅ Blocks it, by luck | A write hidden inside a CTE. Spell it with a comment or odd casing and the filter misses it |
SELECT * INTO backup FROM invoices |
⚠️ Lets it through: it starts with SELECT |
Creates a new table (SQL Server) |
SELECT name, email FROM customers |
⚠️ Lets it through | Reads personal data the model should never see |
sqlguardllm parses the statement into a syntax tree with sqlglot and decides on the tree, not on the text. Then it rewrites the query so it can only see the rows the current user is allowed to see.
What it guarantees
| Rule | |
|---|---|
| 1️⃣ | One statement that is a query (SELECT, UNION, WITH … SELECT). Nothing stacked after a ; |
| 2️⃣ | Read-only across the whole tree: no INSERT, UPDATE, DELETE, MERGE, SELECT … INTO, DDL, COPY, ATTACH, PRAGMA, EXEC, row locks… at any depth |
| 3️⃣ | Allowlisted tables and hidden columns: CTE-aware; SELECT * on a table with hidden columns is rejected |
| 4️⃣ | Denied functions per dialect: file readers, sleeps, shells, network calls (pg_read_file, pg_sleep, LOAD_FILE, OPENROWSET, DBMS_PIPE.*…) |
| 5️⃣ | Row-level filters: every occurrence of a table becomes a filtered derived table, with the user's values bound as escaped literals |
| 6️⃣ | Row limit applied or lowered, in the dialect's own syntax (LIMIT, TOP, FETCH FIRST) |
| 🔒 | Fails closed: if the SQL can't be parsed, or the user context is missing a value a filter needs, the answer is no |
Optional limits
Off by default; turn on what you need. They stop queries that are far more expensive than the question that produced them.
| Policy option | What it does | Violation |
|---|---|---|
max_joins=4 |
Caps joins in the whole query, CTEs and subqueries included | too_complex |
max_subquery_depth=2 |
Caps nesting of subqueries, derived tables and CTEs | too_complex |
forbid_cartesian=True |
Rejects CROSS JOIN and comma joins with no linking equality in WHERE |
too_complex |
required_filters={"invoices": ["issued_at"]} |
Every read of the table must filter on one of the columns, such as a date range on a big fact table | missing_required_filter |
output_dialect="tsql" |
The model writes PostgreSQL; the rewritten SQL comes out for SQL Server, Oracle or any sqlglot dialect | — |
masked_columns={"customers.email": "partial"} |
The column can be selected but comes back masked: null, redact (***), hash (SHA-256) or partial (ju***). Filtering, joining, grouping or sorting by it is rejected |
hidden_column |
aggregate_only={"salaries": 5} |
The table can only be read through aggregates, and groups smaller than 5 rows never come back (HAVING COUNT(*) >= 5) |
aggregate_required |
Quick look
pip install sqlguardllm # Python 3.10+, one dependency: sqlglot
from sqlguardllm import Guard, Policy
guard = Guard(Policy(
dialect="postgres",
tables={"invoices", "branches", "customers"},
hidden_columns={"customers.email", "customers.tax_id"},
row_filters={"invoices": "branch_id IN ({user.branches})"},
max_rows=1000,
))
result = guard.check(sql_from_llm, context={"user": {"branches": [1, 2]}})
if result.ok:
rows = db.execute(result.sql) # read-only, row-filtered, limited
else:
for v in result.violations:
print(v.code, v.message) # write_operation: DELETE inside a CTE…
It doesn't care which model, agent framework or ORM produced the SQL: it only needs the SQL string.
How it works
flowchart LR
LLM["LLM / agent<br/>writes SQL"] --> P{"Parse with<br/>sqlglot"}
P -- "can't parse" --> X["❌ Rejected<br/>with a reason"]
P --> S{"One read-only<br/>query?"}
S -- no --> X
S --> A{"Allowed tables,<br/>columns, functions?"}
A -- no --> X
A --> R["Inject row filters<br/>as derived tables"]
R --> L["Apply row limit"]
L --> G["Generate SQL<br/>in your dialect"]
G --> DB[("Your database<br/>read-only user")]
What runs is the SQL regenerated from the validated tree, never the original string.
Row-level security, rewritten
Each occurrence of a filtered table is replaced by a derived table that keeps its alias, so the filter holds inside joins, subqueries and CTEs. Real sqlglot output for a user who can only see branches 1 and 2:
| The LLM wrote | What runs |
|---|---|
|
|
Same idea on SQL Server, where the limit becomes TOP 500, or Oracle, where it becomes FETCH FIRST 100 ROWS ONLY. More examples in docs/DESIGN.md.
Attack gallery
What sqlglot 30.35 actually sees for each attempt, and what sqlguardllm does about it. Every row is a test case. The full list is in docs/ATTACK_GALLERY.md.
| Dialect | Attempt | sqlglot sees | sqlguardllm |
|---|---|---|---|
| postgres | WITH d AS (DELETE FROM invoices RETURNING *) SELECT * FROM d |
Select root with a Delete inside |
❌ write_operation |
| tsql | SELECT 1; DROP TABLE invoices |
2 statements | ❌ multiple_statements |
| tsql | SELECT * INTO backup FROM invoices |
Select with an Into node |
❌ write_operation |
| postgres | SELECT * FROM invoices FOR UPDATE |
Select with a Lock |
❌ row_lock |
| mysql | SELECT * FROM invoices INTO OUTFILE '/tmp/x' |
parse error | ❌ parse_error (fails closed) |
| sqlite | ATTACH DATABASE '/tmp/x.db' AS x |
Attach |
❌ not_a_query |
| postgres | SELECT pg_read_file('/etc/passwd') |
function pg_read_file |
❌ denied_function |
| oracle | SELECT DBMS_PIPE.RECEIVE_MESSAGE('a', 10) FROM dual |
qualified function call | ❌ denied_function |
| postgres | SELECT 'x; DROP TABLE invoices' AS note |
one Select, a string literal |
✅ allowed |
| postgres | SELECT 1 /* ; DROP TABLE invoices */ |
one Select, a comment |
✅ allowed |
Found a way through? That's a vulnerability: please report it privately.
Where it fits
sqlguardllm is one layer, not the only one. Keep the others:
- A read-only database user with access to only the tables the app needs. Non-negotiable.
- sqlguardllm between the model and the database: shape, allowlists, row filters, limits.
- Statement timeouts in the database, against expensive queries.
- Audit logs of every SQL that ran and for whom.
What it does not do, and why, is in the threat model.
Works with your stack
sqlguardllm sits wherever your app executes the SQL the model wrote. Recipes with runnable, tested code: a generic execute() wrapper (DB-API and SQLAlchemy), LangChain, LlamaIndex, Vanna and MCP. See also docs/INTEGRATIONS.md.
The command-line tool is handy in CI and for trying policies. Exit code 0 means allowed (it prints the rewritten SQL), 1 rejected (it prints the violations) and 2 a usage or policy error:
sqlguardllm check --policy policy.yaml --context '{"user": {"branches": [1, 2]}}' \
"SELECT * FROM invoices"
echo "SELECT …" | sqlguardllm check --policy policy.yaml --format json -
Policy files have a JSON Schema for autocompletion in VS Code and other editors.
MCP server: sqlguardllm mcp gives Claude Desktop, Cursor and VS Code a database tool that can't write, see hidden data or escape row filters; copy-paste setup in docs/MCP.md.
Other languages
sqlguardllm is a Python library today: its engine is sqlglot, which only exists in Python, and three engines in three languages could disagree, which in a security library means a bypass. Node.js and .NET are planned as one engine with many clients: sqlguardllm serve (an HTTP sidecar and Docker image) plus official npm and NuGet clients, with the attack corpus as a conformance suite so every language behaves the same. Follow it in Later · Node.js and .NET and the design. Until then, any language can use the MCP server (v0.4) or the CLI (v0.2).
Playground: try to break it
A static playground (planned for v0.3) will run sqlguardllm entirely in your browser with Pyodide and a demo SQLite database: pick a role, type the nastiest SQL you can think of, and watch it get blocked or rewritten. No server, no sign-up.
Roadmap
| Milestone | Scope |
|---|---|
| v0.1 · Core guard | Statement and read-only checks, allowlists, hidden columns, denied functions, row limit, adversarial corpus |
| v0.2 · Row-level security & CLI | Row filters as derived tables, policy files, CLI, dialect test matrix, docs site, PyPI release |
| v0.3 · More power | Complexity limits, required filters, PII masking, aggregate-only tables (k-anonymity), exact schema from the database, policies per role, explain(), dialect translation |
| v0.4 · MCP server | A safe database tool for Claude Desktop, Cursor and any agent, with an optional cost guard |
| v0.5 · Ecosystem & playground | Integration recipes, browser playground, performance budget |
| Later · Node.js and .NET | One engine, many clients: server mode plus npm and NuGet clients |
Details in docs/ROADMAP.md.
Contributing
Contributions are very welcome, and some of the most valuable ones need no deep knowledge of the codebase:
- Add attack cases to the adversarial corpus, especially for dialects you know well.
- Extend the denied-function lists for your database.
- Translate violation messages.
- Challenge the design in Discussions before v0.1 freezes the API.
Start with CONTRIBUTING.md and the good first issues. This project follows a Code of Conduct.
Security
A bypass of sqlguardllm is a security vulnerability. Don't open a public issue: report it privately as described in SECURITY.md.
License
Apache-2.0 © 2026 Juan Castillo. sqlguardllm was born inside ChatIApp, an open-source chat that turns business questions into safe SQL.
Reviews (0)
Sign in to leave a review.
Leave a reviewNo results found