agent-sql-fence
Static guardrails for LLM-generated SQL. Put it between your text-to-SQL agent and your database.
Status: v0.1, early. It is defense in depth, not a security boundary on its own. Always also use a least-privilege read-only database role, a read-only transaction and a statement timeout.
What it does
- Parses SQL with sqlglot and allows exactly one
SELECT-family statement. - Blocks writes, DDL, privilege changes, locks (
FOR UPDATE),SELECT INTO, and data-modifying CTEs. - Blocks dangerous functions (
pg_sleep,pg_read_file,dblink,set_config, ...), configurable. - Optional table/schema allowlists and denied columns (PII). Blocks system catalogs by default.
- Applies a row cap by rewriting the query.
- Ships a red-team corpus and benchmark so you can measure it.
Install
pip install agent-sql-fence
Usage
from agent_sql_fence import Policy, validate
policy = Policy(
allowed_tables=frozenset({"public.orders", "public.customers"}),
denied_columns=frozenset({"ssn", "customers.email"}),
max_rows=500,
)
verdict = validate("SELECT name FROM customers", policy)
if verdict.allowed:
run(verdict.sql) # rewritten with LIMIT 500
else:
print(verdict.reasons)
For Postgres, agent_sql_fence.executor.run_readonly validates, then executes inside
SET TRANSACTION READ ONLY with a statement_timeout (needs pip install agent-sql-fence[postgres]).
Choose the right dialect
Set Policy(dialect="mysql") (or "postgres", "sqlite") to match the database you protect. A mismatch changes
what the guard sees (for example, # is a comment in MySQL but a syntax error in Postgres). For agents, also consider
Policy(deny_system_info=True) to block user(), database(), version() and @@variables.
Measure it
python bench/run.py
Preliminary results on the public SQLShield dataset are in docs/EVALUATION.md.
The corpus in bench/corpus.py is synthetic: about 150 hand-written cases under two policies, plus automatic
mutations (case, whitespace, comments). It was written by the same author as the guard, so a perfect score
shows regression safety, not security. Independent bypass reports are the most useful contribution.
Known limits
-
The guard does not stop expensive queries (huge
generate_series, cross joins, runaway recursive CTEs,repeat('x', 1e9)). Usestatement_timeout, cost limits and database resource controls. These cases are listed inKNOWN_GAPSin the corpus and covered by an integration test for the timeout. -
Harmless information functions (
current_database(),current_setting(...)) are allowed. -
Static analysis cannot see runtime effects such as expensive queries; use timeouts and cost limits.
-
Views or functions that write are only covered if you deny them by name or allowlist tables.
-
Dialect support is Postgres-first. Other dialects are untested.
-
Integration tests run against PostgreSQL only; other databases are untested.
License
Apache-2.0
Metadata
Release files for agent-sql-fence 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| agent_sql_fence-0.1.1.tar.gz | 20.5 kB | Without provenance | Details for agent_sql_fence-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| agent_sql_fence-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for agent_sql_fence-0.1.1-py3-none-any.whl |
Total release size: 32.4 kB
Release files / agent_sql_fence-0.1.1.tar.gz
| Download URL | agent_sql_fence-0.1.1.tar.gz |
|---|---|
| Size | 20.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4613e7e6f87574ae3ce1f4e8bbd4b183f1cdb2435c23293743768db58c5d51f1
|
|
BLAKE2b-256 checksum How to use checksums |
5e2ac30593a3d433bb0eecb111ecef140421d06ecfd61deb7c5e18d77c7f0ff3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Release files / agent_sql_fence-0.1.1-py3-none-any.whl
| Download URL | agent_sql_fence-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d3a29b7cd5b4ff77db83735c23d277f17d5b900f10cada19802bdb5843cf3e80
|
|
BLAKE2b-256 checksum How to use checksums |
a37dd2d6f8ed63aa44be0ff3ec088416e6042bfbb621550dfbbfdb2221e2114f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.15.2
|
Security for agent-sql-fence 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.