Skip to main content

agent-sql-fence

DOI

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.

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]).

Measure it

python bench/run.py

The corpus in bench/corpus.py is synthetic. Pull requests that add bypasses are the most useful contribution.

Known limits

  • 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 against a live database are not written yet.

License

Apache-2.0

Metadata

Release files for agent-sql-fence 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agent-sql-fence 0.1.0
File Size Uploaded Provenance Actions
agent_sql_fence-0.1.0.tar.gz 11.9 kB Without provenance Details for agent_sql_fence-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agent-sql-fence 0.1.0
File Interpreter ABI Platform Actions
agent_sql_fence-0.1.0-py3-none-any.whl Python 3 none any Details for agent_sql_fence-0.1.0-py3-none-any.whl

Total release size: 22.0 kB

Release files / agent_sql_fence-0.1.0.tar.gz

Download URL agent_sql_fence-0.1.0.tar.gz
Size 11.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8d33be4de998063dd6e39bc333166d99fb8121256b5d130dc597c40efda4eb35
BLAKE2b-256 checksum
How to use checksums
74ad6a7f4d95e197064eeb87f351712581fa6cc949eb202b5df614236f02d052
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.0-py3-none-any.whl

Download URL agent_sql_fence-0.1.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6010fbe88b689de01d130d1fc5467a476aba2a86b0e65c784e2fec1a958ba859
BLAKE2b-256 checksum
How to use checksums
fcf901ee2de68d8c5d160e06a9cd99be793b8ead30104072e840fee2a9f8c8e0
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.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page