Skip to main content

SQLGuard Ninja

Security testing, policy enforcement, agent integration, privacy analysis and CI benchmarking for LLM-generated SQL.

CI PyPI Python License

When an LLM writes SQL, the system prompt is not a security boundary. SQLGuard Ninja sits between the model and the database. It checks every statement against a declared policy for the authenticated caller: which tables and columns a role may use, which data classes may appear only in aggregates or only after human approval, and which tenant the caller belongs to. It records an audit trail and gives the agent an explanation it can act on.

It ships with a labelled benchmark corpus (530 cases, 1,002 dialect evaluations) and a regression gate for CI. The same benchmark harness can evaluate other open-source guards side by side.

One layer, not the only one. Static SQL inspection cannot guarantee database security. Use a read-only least-privilege role, row-level security and statement timeouts as well. See the threat model.

What it does

Workstream What you get Status
Policy engine Read-only enforcement, table/column grants, tenant isolation, function and catalog rules, structural limits. Built on sqlglot. Fails closed when analysis is incomplete. PostgreSQL + SQLite tested
Privacy pack Explicit classification labels, deny / aggregate_only / review per role, JSON-path labels, small-cohort review, minimisation hints and alternatives implemented
Agent integrations SQLCheckpoint with block / review / audit modes, hash-bound single-use approvals, retry lock-out, redacted audit events. LangChain tool, LangGraph nodes, FastAPI router implemented
Benchmark engine Versioned corpus, metrics (recall, precision, FPR, FNR, exact match, per category/dialect, latency percentiles), adapters for native, callable and isolated external validators, JSON and Markdown reports implemented
Query cost Explainable static score; optional PostgreSQL EXPLAIN (FORMAT JSON) inspection, gated and read-only implemented
CI Composite GitHub Action and CLI gate: security, usability, label-change and error regressions against an approved baseline implemented

Not yet included: governed writes (all writes are denied in v0.1); benchmark cases for restricted functions and system catalogs (the rules exist, but the corpus does not cover them); dialects beyond PostgreSQL and SQLite (they parse, but are untested). Full list: KNOWN_LIMITATIONS.md.

Architecture

flowchart LR
    subgraph App["Your application"]
      Auth["Auth layer"] --> Ctx["RequestContext<br/>(principal, role, tenant)"]
      LLM["LLM / agent"] -->|SQL text| CP
      Ctx --> CP["SQLCheckpoint<br/>block / review / audit"]
    end
    CP --> G["Guard"]
    G --> AN["Analyser<br/>sqlglot qualify + scopes"]
    AN --> R1["Statement / catalog / function rules"]
    AN --> R2["Table and column grants"]
    AN --> R3["Privacy labels"]
    AN --> R4["Tenant pins"]
    AN --> R5["Limits + static cost"]
    CP -->|authorized only| EX["Read-only executor"] --> DB[("Database<br/>grants, RLS, timeouts")]
    CP --> AU["Audit sink<br/>hash + redacted SQL"]
    CP -.review.-> AP["ApprovalQueue (human)"]
    G -.ALLOW only.-> PI["PostgreSQL EXPLAIN inspector"]
    subgraph Bench["Benchmark + CI"]
      DS["Corpus v1"] --> RUN["Runner"] --> ADP["Adapters: native, callable,<br/>isolated external workers"]
      RUN --> RES["Result JSON / Markdown"] --> CMP["Baseline compare"] --> GATE["GitHub Action gate"]
    end

Design decisions and their trade-offs are recorded in ARCHITECTURE_DECISIONS.md.

Five-minute quick start

python -m pip install sqlguard-ninja            # core: sqlglot, pydantic, PyYAML
python -m pip install "sqlguard-ninja[all]"     # + LangChain, LangGraph, FastAPI, psycopg

Extras: langchain, langgraph, fastapi, postgres, all. Python 3.11+.

Validate a query against the bundled reference policy (builtin:retail_saas, a synthetic multi-tenant retail schema):

sqlninja validate --policy builtin:retail_saas --role support_analyst --tenant 1   --sql "SELECT o.id FROM orders o WHERE o.tenant_id = 1 OR 1 = 1"
DENY:
  [deny] tenant.missing_predicate: Every read of table 'orders' must filter on the caller's tenant. Suggestion: Add `o.tenant_id = :tenant_id` to the WHERE clause of the scope that reads this table.

Exit codes: 0 allow, 1 deny, 3 review, 2 usage error.

Run the bundled benchmark corpus, then start your own project:

sqlninja benchmark run --dataset builtin:v1 --output results/native.json
sqlninja init my-project && cd my-project
sqlninja benchmark run --config sqlninja.yaml

The runnable demos live in the repository (deterministic; no API key needed):

git clone https://github.com/Pratik653/SQLGuard_Ninjapy.git && cd SQLGuard_Ninjapy
python -m pip install -e ".[all]"
python examples/sdk/quickstart.py
python examples/sqlite/text_to_sql_demo.py
python examples/langgraph/sql_agent_demo.py

Python SDK

from sqlguard_ninja import Guard, Policy

from sqlguard_ninja.benchmarking.builtin import resolve

guard = Guard(Policy.from_file(resolve("builtin:retail_saas", "policy")))
result = guard.validate(
    "SELECT department, AVG(salary) FROM employees GROUP BY department",
    role="hr_analyst",
    dialect="postgres",
)
result.allowed      # True
result.findings     # () - structured Finding objects with rule_id, action, message, remediation

guard.validate("SELECT full_name, salary FROM employees", role="hr_analyst").explain()
# DENY:
#   [deny] privacy.aggregate_only: The column 'employees.salary' may only be used inside
#   statistical aggregates (COUNT, SUM, AVG, ...), not as individual values. ...

LangGraph

from sqlguard_ninja.integrations import RequestContext, SQLCheckpoint
from sqlguard_ninja.integrations.langgraph import (
    CONTEXT_KEY, make_execute_node, make_guard_node, route_after_guard)

checkpoint = SQLCheckpoint(guard, mode="block", max_denied_attempts=3)
graph.add_node("guard", make_guard_node(checkpoint))
graph.add_node("execute", make_execute_node(checkpoint, executor))   # re-validates
graph.add_conditional_edges("guard", route_after_guard(3),
                            {"execute": "execute", "revise": "generate", "end": END})

ctx = RequestContext(principal=user.id, role=user.role, tenant_id=user.tenant_id)
app.invoke(state, config={"configurable": {CONTEXT_KEY: ctx}})       # nodes cannot alter config

More in AGENT_INTEGRATION.md: the LangChain tool (its schema accepts only query), the FastAPI router, review-mode approvals and audit sinks.

Privacy policies

Sensitivity comes from labels you assign in the policy, never from column names, and access is resolved per role:

labels:
  payment_card: {sensitivity: critical, masking: use card_last4 and card_brand}
  financial:    {sensitivity: high}
catalog:
  tables:
    payments:
      tenant_column: tenant_id
      columns:
        card_number: {labels: [payment_card], alternatives: [card_last4]}
        card_last4: text
roles:
  support_analyst:
    tables: {payments: [id, tenant_id, card_last4, card_brand, amount, status]}
    labels: {payment_card: deny, financial: deny}
  hr_analyst:
    tables: {employees: "*"}
    labels: {hr: allow, financial: aggregate_only}   # salary only inside COUNT/SUM/AVG
support_analyst> SELECT p.card_number FROM payments p WHERE p.tenant_id = 1
DENY:
  [deny] privacy.label_denied: The column 'payments.card_number' contains classified data (payment_card) that this role may not access. Suggestion: Use column 'payments.card_last4' instead. ...

hr_analyst> SELECT AVG(salary) FROM employees WHERE id = 7
REVIEW:
  [review] privacy.small_cohort: An aggregate of restricted data is filtered or grouped by an identifying column, which can reveal individual values. ...

Labels also apply to paths inside JSON columns (profile->'health'->>'allergies'). sqlninja policy suggest proposes labels from column names for a human to review; those suggestions are never enforced. This is a technical control, not a certification of GDPR compliance. Full semantics: POLICY_REFERENCE.md.

Query cost analysis

Every validation includes an explainable static score that uses the catalog's estimated_rows:

platform_admin> SELECT o.id, i.id FROM orders o CROSS JOIN order_items i
DENY:
  [deny] cost.static_score: Estimated query cost risk is 100/100: join with i has no join condition (Cartesian product); o has an estimated 10,000,000 rows and is read without a filter; ...

Optionally, compare it with PostgreSQL's planner. This runs EXPLAIN (FORMAT JSON) only, never ANALYZE, and only on SQL the guard allowed, inside a read-only, time-limited, rolled-back transaction:

from sqlguard_ninja.query_cost.postgres import PostgresPlanInspector
report = PostgresPlanInspector(guard, lambda: psycopg.connect(READER_DSN)).inspect(
    sql, role="support_analyst", context={"tenant_id": 1})
report.total_cost, report.plan_rows, report.static.score, report.findings

See QUERY_COST.md and examples/postgresql/plan_inspection_demo.py.

Benchmark

sqlninja benchmark run --dataset builtin:v1 --adapter native
python scripts/setup_external_adapters.py          # (repository) isolated venvs for other tools
sqlninja benchmark run --dataset builtin:v1 --adapter external:sql-guardrail
sqlninja benchmark compare --baseline benchmarks/baselines/native.json --candidate results/latest.json

Results for corpus v1.0.0 (Windows 11, Python 3.11, 1,002 evaluations). Details and raw files: RESULTS_v1.md.

Validator Recall Precision FPR Recall / FPR within declared capability p95 ms
SQLGuard Ninja 0.1.0 100.0% 100.0% 0.0% 100.0% / 0.0% 2.15
sql-guardrail 0.5.2 98.6% 86.6% 21.2% 98.3% / 19.3% 1.72
agent-sql-guard 0.2.0 67.9% 86.6% 14.5% 89.5% / 17.2% 1.26
krish2105/sqlguard 0.1.0 65.8% 91.4% 8.6% 83.5% / 5.2% 1.22

Read the native row with care. The corpus and the engine were developed together, and every disagreement was triaged by fixing the engine or correcting a label (both are logged). 100% shows consistency with the documented semantics, not independent detection quality. The other tools have smaller policy models (no roles, labels, aggregate-only access or review), so the "within capability" column is the fairer comparison. Methodology and threats to validity: BENCHMARK_METHODOLOGY.md.

How it relates to existing tools

SQLGuard Ninja uses sqlglot for parsing, dialects and scope analysis, and does not re-implement them. sql-guardrail is a thorough read-only validator with a tautology catch-all, which this project does not duplicate. agent-sql-guard has a careful schema-less PII denylist. What SQLGuard Ninja adds: role- and label-based policies, the agent checkpoint contract, a shared benchmark with isolated adapters, planner inspection, and the CI gate. Full comparison: COMPETITIVE_ANALYSIS.md.

Security limitations

  • Static inspection can be wrong where sqlglot and the real database parse SQL differently. Only PostgreSQL and SQLite are tested.
  • It cannot see inside views or user-defined functions, and it does not stop inference across many permitted queries.
  • Cost scores and planner estimates are indicators, not guarantees.
  • v0.1 denies all writes.
  • The benchmark has no restricted-function or system-catalog cases yet. The rules exist, but those two categories are not measured.

Use database grants, row-level security and timeouts as the authoritative controls. See the threat model and KNOWN_LIMITATIONS.md.

Roadmap

Next: restricted-function and system-catalog benchmark cases, an independent held-out corpus, lineage-aware aggregate checks, and tenant predicate push-down. Later: governed writes, more tested dialects, and SARIF output. Details in ROADMAP.md.

Documentation

Supported versions

  • Python 3.11, 3.12, 3.13 (CI matrix on Linux and Windows).
  • SQL dialects: PostgreSQL (tested against PostgreSQL 16) and SQLite. Other sqlglot dialects parse but are experimental and untested.

Development

python -m pip install -e ".[dev]"
ruff check src tests scripts examples && ruff format --check src tests scripts examples
mypy
pytest                          # PostgreSQL tests run if SQLNINJA_PG_DSN is set or pgserver is installed
python scripts/generate_corpus.py --check

See CONTRIBUTING.md. Report vulnerabilities privately per SECURITY.md.

Licence

Apache-2.0. No third-party code is vendored; dependencies and their licences are listed in THIRD_PARTY_NOTICES.md. All benchmark data is synthetic.

Metadata

Release files for sqlguard-ninja 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 sqlguard-ninja 0.1.0
File Size Uploaded Provenance
sqlguard_ninja-0.1.0.tar.gz 222.2 kB With provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlguard-ninja 0.1.0
File Interpreter ABI Platform
sqlguard_ninja-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 345.4 kB

Release files / sqlguard_ninja-0.1.0.tar.gz

Download URL sqlguard_ninja-0.1.0.tar.gz
Size 222.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1a99c362b64d46e75607dfc57763706ad398a28bc72041fa33c6d5f014bd4920
BLAKE2b-256 checksum
How to use checksums
946f3c205cc47cc01ebce188ba2a993fdfcb91998d7a2b84a106cf794a2a1440
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

Release files / sqlguard_ninja-0.1.0-py3-none-any.whl

Download URL sqlguard_ninja-0.1.0-py3-none-any.whl
Size 123.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d059161d039fa431a5efd3d551be70dfb4072dccbf13b9702db98761bb41a56
BLAKE2b-256 checksum
How to use checksums
767887a5cc4ea8feadbcd4a70b5f2dad4773598d8c5b411e619fb0f217bb1331
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

Security for sqlguard-ninja 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.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sqlguard_ninja-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 24, 2026.

Transparency log

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 Full 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