Repository navigation
Verified queries: a catalog of approved queries an agent runs by id (#287) - #325
Merged
Merged
Conversation
…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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #287.
What it adds (experimental)
A git-managed catalog (YAML or JSON) of approved, read-only business queries: a version, stable ids,
:nameSQL templates, typed parameters (string,integer,number,boolean,date) and allowed roles.sqlguardllm mcp --catalog FILEadds two tools next toquery, which does not change:list_verified_queries(the ids, descriptions and typed parameters this role may run) andrun_verified_query(an id and its values). The server owns the SQL.querywas refactored into a core that both tools share, so a verified query gets every protectionqueryhas: policy (tables, columns, masks, row filters bound to the trusted context), the row limit with exacttruncated, the read-only transaction, the timeout, taint tracking, row budgets, result delivery and plan checks. I did not copy those checks.%(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:namefortext(). Nothing but a real placeholder can bind a value (tested on four dialects).sqlguardllm catalog check FILE --policy ... --role ...(exit 0 / 1 refused by the policy / 2 unreadable).catalog_queryandcatalog_versionon 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).examples/catalog/): net sales by month, top customers, customer directory (masked e-mail), with fixtures small enough to check by hand andexpected.json. Docs EN/ES indocs/CATALOG.md.Findings along the way (worth reading)
LIMIT :ncannot 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 caseWHERE id > :n LIMIT :n, where one:nsurvives and the LIMIT is lost. Both are refused at startup with the reason; documented.guard.check, which recorded an audit event per query at every startup; it now usesexplain(a test asserts no events).query. Said plainly in the docs.Tested
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.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.Not covered
🤖 Generated with Claude Code