Skip to content

RLS testing

potalab base lint and the dashboard Advisors page read the catalog and report missing protection. potalab base test rls goes further: it runs statements against your policies as two fake users and tells you which policies leak. It needs no LLM and no extension (pgTAP is optional).

Terminal window
potalab base test rls # local stack
potalab base test rls --db-url postgres://postgres@host:5432/mydb
potalab base test rls --json --fail-on high --min-score 80 # CI
potalab base test rls --emit potalab/tests/90_rls_generated.sql # also export the probes as pgTAP
  1. Reads the exposed schemas (default public, change with --schema): tables, views, columns, foreign keys, policies and the privileges of anon and authenticated.
  2. Classifies each table by its isolation column: owner (a foreign key to auth.users, or a column such as user_id, owner_id, created_by), tenant (tenant_id, org_id, workspace_id …) or unscoped. Override with --owner-column public.notes=writer and --tenant-column public.notes=company_id.
  3. Seeds two rows per table inside a transaction, for user A and user B.
  4. Attacks as B and as anon, only for the commands those roles are granted:
Check Statement Passes when
read_cross SELECT A’s row 0 rows
insert_spoof INSERT a row owned by A RLS violation (42501)
update_cross, delete_cross change A’s row 0 rows
move_owner, move_tenant hand B’s own row to A 0 rows or 42501
anon_read, anon_insert, anon_update, anon_delete the same without a token blocked
  1. Runs static checks: rls_disabled, view_bypasses_rls (a view without security_invoker), foreign_table_exposed, rls_no_policy.

The dashboard has the same test under Security > RLS test (project admins only). It shows the score, the findings per table and a download of the pgTAP file. It runs in a rolled-back transaction with these limits:

  • at most 100 tables and views per run;
  • 5 seconds per statement;
  • 30 seconds for the whole run (a run that hits the limit returns a partial result and says so);
  • one run at a time per project (a second request gets a conflict).

Untick execute (execute: false in the Management API, POST /v1/projects/:ref/security/rls-test) to only generate the pgTAP file: no row is seeded and no probe runs. Each run is written to the audit log.

Security > RLS playground in the dashboard answers “what does user X see, and why”. Pick a role (anon, authenticated or service_role). For authenticated, search an end user (this fills sub and the email) and optionally set tenant_id, aal and extra claims. Then browse a table or write one SELECT.

  • It runs in a read-only transaction with SET LOCAL ROLE and the chosen request.jwt.claims, always rolled back, with at most 200 rows and 5 seconds. Writes are refused. A statement that fails for that identity (permission denied, an RLS violation) is shown as the result.
  • Below the rows, per table: RLS on, off or forced, BYPASSRLS, the policies that apply to that role for SELECT (permissive ones are OR-ed, restrictive ones AND-ed, default deny when none applies), the USING and WITH CHECK of INSERT, UPDATE and DELETE, and, for a single-table query, a row-by-row trace of each policy on the first 20 rows, including rows the user cannot see.
  • Generate pgTAP test downloads a test built with the same probe as the CLI, asserting the row count (or SQLSTATE) you just observed.

Only project admins can use it.

The score starts at 100 and loses 25 per critical finding, 15 per high, 7 per medium and 2 per low (info costs nothing). Grades: A at 90 or more, B at 75, C at 50, D at 25, otherwise F.

  • critical: anyone, including anonymous visitors, can change or see other people’s data.
  • high: a signed-in user can read or hand over another user’s rows.
  • medium / low: the table has no isolation column, which may be intentional (use --ignore).

Inconclusive means a probe failed for a reason other than RLS (a trigger or CHECK); it never counts as a pass.

Option Meaning
--db-url <url> Database to test (default: the local stack; the password may come from BASE_DB_PASSWORD).
--schema, --table, --ignore Choose exposed schemas, restrict to tables, skip tables.
--owner-column, --tenant-column Override the isolation column as schema.table=column.
--fail-on <severity> Exit 5 when a finding at or above this level exists (info, low, medium, high, critical, none; default high).
--min-score <n> Exit 5 when the score is below n (0-100).
--emit <file> Also write the probes as a pgTAP file.
--json Print the whole report as JSON.

Exit codes: 5 for a failed gate, 2 for a usage error, 4 for a connection error.

--emit file.sql writes the generated probes as a pgTAP test (BEGIN; SELECT plan(n); ... SELECT * FROM finish(); ROLLBACK;). Put it in potalab/tests/ and it runs with your other SQL tests, so a policy regression fails CI without the generator.

See also Row level security and the CLI reference.