Skip to content

Verified queries: a catalog of approved queries an agent runs by id (#287) - #325

Merged
jmmana merged 1 commit into
mainfrom
feat/verified-catalog
Dec 3, 2026
Merged

jmmana merged 1 commit into
mainfrom
feat/verified-catalog

Conversation

@jmmana

@jmmana jmmana commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Closes #287.

What it adds (experimental)

A git-managed catalog (YAML or JSON) of approved, read-only business queries: a version, stable ids, :name SQL templates, typed parameters (string, integer, number, boolean, date) and allowed roles. sqlguardllm mcp --catalog FILE adds two tools next to query, which does not change: list_verified_queries (the ids, descriptions and typed parameters this role may run) and run_verified_query (an id and its values). The server owns the SQL.

  • No extra privileges. query was refactored into a core that both tools share, so a verified query gets every protection query has: policy (tables, columns, masks, row filters bound to the trusted context), the row limit with exact truncated, the read-only transaction, the timeout, taint tracking, row budgets, result delivery and plan checks. I did not copy those checks.
  • Values are bound, never written into the SQL. The guard rewrites the template (each dialect spells a placeholder differently: PostgreSQL %(n)s, BigQuery @n, DuckDB $n), so the rewritten SQL is read back, each placeholder swapped on the tree for a unique marker, every remaining colon escaped (:: casts, '12:30', a tenant value like 'a:b') and only then turned into :name for text(). Nothing but a real placeholder can bind a value (tested on four dialects).
  • Checked at startup, for the server's role: invalid definitions, duplicate ids, undeclared or unused parameters, a dialect that differs from the policy's, unknown roles, any query the policy refuses, and a guard rewrite that loses a parameter (counted per use). Same check for CI: sqlguardllm catalog check FILE --policy ... --role ... (exit 0 / 1 refused by the policy / 2 unreadable).
  • Roles: an unauthorized role gets the same answer for "not yours" as for "does not exist", and does not see it in discovery.
  • Audit: catalog_query and catalog_version on the event, set the way the OIDC subject already is. No value and no row, even with raw SQL logging (the SQL recorded is the template).
  • Three runnable PostgreSQL examples (examples/catalog/): net sales by month, top customers, customer directory (masked e-mail), with fixtures small enough to check by hand and expected.json. Docs EN/ES in docs/CATALOG.md.

Findings along the way (worth reading)

  • A LIMIT :n cannot be a parameter. The guard replaces a non-literal limit with the policy's row limit, so the parameter silently vanishes. Counting placeholders per use (not by name) caught the subtle case WHERE id > :n LIMIT :n, where one :n survives and the LIMIT is lost. Both are refused at startup with the reason; documented.
  • Startup validation must not audit. My first version used guard.check, which recorded an audit event per query at every startup; it now uses explain (a test asserts no events).
  • A catalog is not a restriction: the agent can still call query. Said plainly in the docs.

Tested

  • 74 unit tests (tests/test_catalog.py): every validation rule, every parameter type and error (no message ever holds a value), binding on four dialects, the tools on SQLite (visibility, unauthorized, invalid parameters, masks, row limit, tenant from the trusted context incl. a colon in it, a hostile value stays data and the table survives, defaults, audit with and without raw SQL), startup refusals and the command's exit codes.
  • 10 tests on a real PostgreSQL (tests/engines/test_catalog_engines.py, run locally on PostgreSQL 17; runs in the Engines job): the expected business results of each example, a changed metric definition (net → gross) is caught while the policy still passes, role visibility, tenant isolation, masks and the row limit, the audit trail, and invalid parameters.
  • Full suite, ruff, mypy and a strict docs build (plus a fragment-link check) pass locally.

Not covered

  • Only PostgreSQL and SQLite were executed; MySQL and T-SQL are covered by conversion tests only (the docs say so).
  • With OIDC the policy check at startup is skipped (the context comes from each caller's token) and happens on every call.

🤖 Generated with Claude Code

…id (#287)

A git-managed catalog (YAML or JSON) with ids, :name SQL templates, typed parameters
and roles. sqlguardllm mcp --catalog adds list_verified_queries and run_verified_query:
the server owns the SQL, values are bound, and every protection of query still applies.
Checked at startup against the policy; audit events carry the id and version only.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 23:00

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jmmana
jmmana merged commit d023d05 into main Dec 3, 2026
31 checks passed
@jmmana
jmmana deleted the feat/verified-catalog branch December 3, 2026 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FEAT — Verified query catalog for MCP agents

2 participants