Skip to content

Policy diff

Español: es/POLICY_DIFF.md

Experimental: not covered by the 1.x stability guarantee (see STABILITY.md); it may change in a minor release.

A reviewer of a pull request that edits the policy can't see which queries change or how. A dropped row filter or an unmasked column is easy to miss in a YAML diff. sqlguardllm diff runs a set of cases under the current policy and the proposed one and shows what changed, and it can fail CI when something got looser.

git show origin/main:policy.yaml > base.yaml
sqlguardllm diff --base base.yaml --candidate policy.yaml cases.yaml \
  --format md --redact --fail-on newly-allowed,filter-removed,mask-removed
2 of 3 cases changed, 2 policy changes
  policy: mask-removed: customers.email
  policy: filter-removed: invoices: branch_id IN ({user.branches})
CHANGED total per branch: rewritten, filter-removed
  allow -> allow (no violation)
  --- base
  +++ candidate
  @@ -1 +1 @@
  -SELECT SUM(total) FROM (SELECT * FROM invoices WHERE branch_id IN (?, ?)) AS invoices LIMIT ?
  +SELECT SUM(total) FROM invoices LIMIT ?
CHANGED customer contact: rewritten, mask-removed
  allow -> allow (no violation)
  --- base
  +++ candidate
  @@ -1 +1 @@
  -SELECT name, CASE WHEN email IS NULL THEN NULL ELSE SUBSTRING(CAST(email AS VARCHAR(?)) FROM ? FOR ?) || ? END AS email FROM customers LIMIT ?
  +SELECT name, email FROM customers LIMIT ?
GATES TRIPPED: filter-removed, mask-removed

Cases

The same file as sqlguardllm test (cases: with name, sql, role, context; expect, violation and sql_contains are accepted and ignored), a .jsonl file (one object per line) or a .sql file (one query per line, -- comments skipped). With a policy set, every case needs a role. With no cases file it still compares the two policies.

What it reports

Per case: the decision before and after (allow or deny), the violation codes, a diff of the rewritten SQL, the tables with row filters, the masked columns, the row limit and, when advice is on, the score. The change labels are newly-allowed, newly-denied, codes-changed, rewritten, filter-removed, filter-added, mask-removed, mask-added, limit-raised, limit-lowered and score-changed.

The policies themselves, even when no case touches the change: tables added or removed (or an allowlist dropped), hidden columns removed or added (classes from data classes count), masks added, removed or changed, row filters added, removed or changed, max_rows, denied functions, required filters and aggregate_only minimum group sizes, and roles added or removed in a policy set.

Gates

--fail-on takes a comma-separated list. The exit code is 0 when none trips, 1 when one does and 2 for a usage or policy error. Without --fail-on it only reports.

Gate Trips when
newly-allowed A case that was denied is now allowed
newly-denied A case that was allowed is now denied
filter-removed A row filter was dropped from the policy, or from a case's rewritten SQL
mask-removed A mask was dropped, in the policy or on a column a case reads
hidden-removed A hidden column was unhidden
limit-raised max_rows went up or was removed, or a case's limit did
any-change Anything above or below changed

Output and privacy

--format text (default), json (schema) or md for $GITHUB_STEP_SUMMARY or a PR comment.

Use it on authored fixtures that are safe to show in a pull request. The output holds the SQL of your cases and the context you gave them (IN (1, 2)). --redact replaces every literal with ?, including the numbers in a LIMIT, which stay in their own limit field. For private traffic use shadow mode, which keeps no SQL.

A ready workflow is in examples/policy-diff/, with a pair of fixture policies that trip the gates. This repository runs them in CI (the Policy diff example workflow writes the Markdown report to the job summary, and a test checks the gates).

Limits

  • It tests the cases you wrote. A change nobody wrote a case for is only caught by the policy comparison, and only for the fields it compares.
  • Both files must be policies or both policy sets. A role is compared with the same role.
  • It never runs a query: it asks the guard what it would do.