DEV Community

Cenk KURTOĞLU
Cenk KURTOĞLU

Posted on

10 read-only checks for Supabase RLS drift (and a free nightly n8n diff)

RLS does not usually break in a dramatic way. A migration drops FORCE ROW LEVEL SECURITY, someone clicks a console form and adds USING (true) to a SELECT policy, or a helper function loses its pinned search_path. The app keeps working, the last audit report is still in a drive folder, and the catalog is no longer what that report described.

I ended up with ten read-only catalog checks that answer one question each: did the database change in a way that widens access? They query pg_class, pg_policies, pg_proc, pg_roles and information_schema only. No application rows, no writes, no service_role needed.

1. “RLS enabled” is two flags, not one

relrowsecurity = true only means policies are applied to non-owners. If relforcerowsecurity = false, the table owner still reads every row. Most migration roles are the owner, so this is the first thing I check:

SELECT n.nspname || '.' || c.relname AS table_name,
       c.relrowsecurity, c.relforcerowsecurity
FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p') AND n.nspname = 'public';
Enter fullscreen mode Exit fullscreen mode

The opposite mistake is assuming permission denied for table x means RLS. It is often a missing USAGE grant on the schema or a missing SELECT grant for anon/authenticated. has_table_privilege('anon','public.x','SELECT') and information_schema.role_table_grants answer that directly.

2. Policy text has to be read with its role and composition

A permissive SELECT policy with USING (true) is public reads, whatever the table's RLS flag says. A FOR ALL policy without WITH CHECK lets a user UPDATE a row into a state no policy would have allowed at insert time. And pg_policies.permissive stores the full string PERMISSIVE/RESTRICTIVE — a check for P/R silently returns nothing. Permissive policies combine with OR, restrictive ones with AND, so the effective predicate is not the text of any single row.

SELECT tablename, policyname, cmd, permissive, roles, qual, with_check
FROM pg_policies
WHERE schemaname = 'public'
  AND ((qual IS NULL OR qual = 'true')
       OR (cmd = 'ALL' AND (with_check IS NULL OR with_check = 'true')));
Enter fullscreen mode Exit fullscreen mode

3. Grants and definer functions are part of the same picture

An anon or authenticated role holding INSERT/UPDATE/DELETE is a write path even if the policies look careful. So is a SECURITY DEFINER function with no pinned search_path: it runs under its owner's rights, and if that owner has BYPASSRLS, table policies no longer constrain it. Ten minutes in information_schema.role_table_grants and pg_proc.proconfig has saved me more time than any policy review.

4. A scan is stale the moment you run it

The part I found missing in one-off audits is the diff. I capture the catalog findings once as a baseline, then on every run compare and report only NEW and RESOLVED items with a severity order. The job must fail closed: if the credential breaks, the workflow stops. It must never write an empty baseline and announce that all findings were resolved — a silent reader is not a clean database.

I packed those ten checks into a free n8n workflow (nightly schedule plus a manual “Run now”), with the same checks as a standalone SQL file: Supabase RLS Drift Monitor — https://kurtoglu1.gumroad.com/l/supabase-rls-drift-monitor-free. Read-only by design, first run silent, later runs report the diff. No support commitment, no service_role, no application data read.

Honest limits: this is a catalog diff, not an exploit test. It cannot tell you whether a policy matches what your team meant, and zero findings is not proof that the database is safe. Test on a scratch database before pointing anything at production. When I need one concrete question answered with evidence — for example, whether the anon key still reaches a table it should not — I use a documents-only spot check (fixed scope, starts only after payment is secured): https://kurtoglu1.gumroad.com/l/supabase-rls-spot-check.

If you have a check that caught something these ten would miss, I would genuinely like to hear it in the comments.

Top comments (4)

Collapse
 
cekuu35 profile image
Cenk KURTOĞLU • • Edited

Following up in the comments on the pg_default_acl surface and its pg_class.relacl half — one is shipped, one is honestly still open:

K is in the free monitor. It reads pg_default_acl and flags default ACLs that grant SELECT/INSERT/UPDATE/DELETE/TRUNCATE/REFERENCES/TRIGGER on future tables to anon, authenticated, or PUBLIC, covering schema-wide defaults (defaclnamespace = 0) and the public schema. Your query is in the shipped file close to verbatim, with two deliberate narrowings: defaclobjtype = 'r' only (sequence defaults are not covered yet), and the report resolves defaclrole::regrole so it names the role that ran ALTER DEFAULT PRIVILEGES instead of printing an OID.

The relacl half is not covered — you found a real gap. Checks A/B/L read pg_class for relrowsecurity / relforcerowsecurity / reloptions but never relacl, and the grant check reads information_schema.role_table_grants for write privileges only — SELECT is not in that list, and that view shows nothing for a table with a NULL relacl that inherits an open default. So a table whose only SELECT grant arrives through an inherited default stays invisible to all twelve checks until something writes an explicit ACL row. The fix shape is to explode pg_class.relacl and merge it with the applicable pg_default_acl rows (matched on owner and schema) to resolve effective SELECT per table — read-only, no SET ROLE, because has_table_privilege evaluates for the checking role and a monitor role is not anon.

On the empty-baseline class you have seen ship twice: that is exactly what the workflow refuses to do. The Postgres node has no continue-on-error, so a bad credential or a timeout stops the run before the baseline node runs, and the baseline only exists in workflow static data written by a successful scan. There is no path where the failure result and the "no findings" result look the same downstream.

Thanks for the query and for the relacl angle — the second one goes on the list as a named gap, not as a shipped claim.

Collapse
 
cekuu35 profile image
Cenk KURTOĞLU •

Thanks for reading closely. Honest answers to both questions:

1) I don't have a 'top check' ranked by production drift frequency — I have not run this across a large enough set of client apps to make that claim. What I can say from the source level: check B (RLS enabled but no FORCE, so the table owner bypasses policies) and check C (permissive SELECT/ALL with USING (true)) are the two most common shapes I see in public threads, but that is an observation about what people discuss, not a measured frequency. The permissive trap you mention is real — the catalog value is the full string PERMISSIVE/RESTRICTIVE; a scan that tests P/R can end up with zero rows and report a false all-clear.

2) The nightly diff has no 'approve this new finding' UI. The baseline is a snapshot of the current finding fingerprints; every NEW fingerprint fires an alert until the baseline is refreshed. That is deliberate fail-closed behavior: a broken credential can never be mistaken for 'all findings resolved'. The practical workflow is to review the diff, mark the change as expected in your own notes/PR, then deliberately re-run once to record the new baseline. There is no silent auto-accept — an intentional change still shows up once as a NEW finding.

One correction/update to the article: the checks are now A–L (12, not 10). The pg_default_acl surface mentioned in the comments is now check K — ALTER DEFAULT PRIVILEGES attaches grants to future tables, so no per-table grant or policy row shows the leak. Check L covers views over RLS tables without security_invoker, which read with the view owner's rights. The downloadable files and site copy have already been updated to 12 checks; the article title still says 10 and I am leaving that as-is rather than rewriting history.

Collapse
 
aistackguide profile image
AI Stack Guide •

One more catalog surface I would add to the ten: pg_default_acl. ALTER DEFAULT PRIVILEGES attaches grants to future objects, so a migration role that ran GRANT SELECT ON TABLES TO anon as a default leaves no row in information_schema.role_table_grants until the next table is created — and then every new table in that schema is readable by anon at creation, with no policy change and no diff on any per-table check. The query is short:

SELECT defaclrole::regrole, defaclnamespace::regnamespace, defaclobjtype, defaclacl
FROM pg_default_acl;
Enter fullscreen mode Exit fullscreen mode

The other half of the same trap is pg_class.relacl IS NULL on a public table: no explicit ACL means the default applies, so checks 1–3 can all pass while the effective grant is wider than the rows you inspected.

Your line about a silent reader not being a clean database is the one I would put in bold. I have seen the empty-baseline bug ship twice, both times because the failure path wrote the same "no findings" object as the success path instead of exiting non-zero.

Collapse
 
userxnom profile image
Abdullah Naseer •

Check #1 alone justifies the whole post — relrowsecurity = true meaning nothing for the table owner is the kind of thing you only learn after a migration role quietly reads every row. Same with the pg_policies.permissive trap: checking for "P"/"R" and silently getting nothing is exactly how a scan reports "all clear" while a USING (true) policy sits there.

The part I keep coming back to is the diff, not the scan. A one-off audit goes stale the moment someone clicks a console form, so a nightly baseline that reports only NEW findings is the right shape — and read-only by design, no service_role, no application rows, makes it safe to actually run.

Two questions: of the 10 checks, which one catches the most real drift in the apps you review? And how does the nightly diff handle intentional changes — is there a way to approve a new policy as expected, or does every diff fire an alert until someone updates the baseline?