Metadata-Version: 2.5
Name: agent-sql-fence
Version: 0.1.1
Summary: Static guardrails for LLM-generated SQL: read-only enforcement, allowlists and row caps.
Project-URL: Homepage, https://github.com/revanth-potluri1/agent-sql-fence
Project-URL: Issues, https://github.com/revanth-potluri1/agent-sql-fence/issues
Author: Revanth Potluri
License: Apache-2.0
License-File: LICENSE
Requires-Python: >=3.10
Requires-Dist: sqlglot>=25
Provides-Extra: dev
Requires-Dist: pytest; extra == 'dev'
Provides-Extra: postgres
Requires-Dist: psycopg[binary]>=3.1; extra == 'postgres'
Description-Content-Type: text/markdown

# agent-sql-fence

[![CI](https://github.com/revanth-potluri1/agent-sql-fence/actions/workflows/ci.yml/badge.svg)](https://github.com/revanth-potluri1/agent-sql-fence/actions/workflows/ci.yml)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.24840041.svg)](https://doi.org/10.5281/zenodo.24840041)

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](https://github.com/tobymao/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

```bash
pip install agent-sql-fence
```

## Usage

```python
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

```bash
python bench/run.py
```

Preliminary results on the public SQLShield dataset are in [docs/EVALUATION.md](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)`). Use `statement_timeout`, cost limits and database resource controls. These cases are
  listed in `KNOWN_GAPS` in 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
