Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

A green nightly RLS check can still scan nothing: the fingerprint your baseline needs

A nightly security check that returns zero findings has two very different meanings: the catalog really is unchanged, or the check stopped seeing the catalog. The second one is worse, because the dashboard stays green.

A nightly security check that returns zero findings has two very different meanings: the catalog really is unchanged, or the check stopped seeing the catalog. The second one is worse, because the dashboard stays green.

I hit that distinction while building a scheduled Supabase/PostgreSQL RLS drift monitor. Here is the pattern that made it trustworthy, and the ten read-only checks it runs.

Three ways a green check scans nothing

  1. Broken credential. The scheduled query fails; an error is swallowed somewhere in the workflow; the diff stage sees an empty result set and treats it as "no new findings". On the next run it may even overwrite the good baseline with an empty one.
  2. Filter drift. A schema or table-name filter changes (or a new schema stops matching). The row count drops to zero while the job keeps succeeding.
  3. Visibility drift. The checking role loses SELECT on pg_policies / pg_class, or a migration moves the objects. The catalog is still there; the role just no longer sees it.

All three produce the same user-visible output: a clean report.

The one field a baseline needs

Store a scanned-object fingerprint with the baseline, not just the finding list: the sorted relation names, their RLS flags, and the policy count, plus a hash of that snapshot.

with snap as (
  select
    quote_ident(n.nspname) || '.' || quote_ident(c.relname) as relation,
    c.relrowsecurity as rls_enabled,
    c.relforcerowsecurity as force_rls,
    count(p.polname) as policy_count
  from pg_class c
  join pg_namespace n on n.oid = c.relnamespace
  left join pg_policy p on p.polrelid = c.oid
  where c.relkind in ('r', 'p')
    and n.nspname not in ('pg_catalog', 'information_schema')
  group by n.nspname, c.relname, c.relrowsecurity, c.relforcerowsecurity
)
select md5(string_agg(relation || '|' || rls_enabled || '|' ||
                     force_rls || '|' || policy_count, ';' order by relation)) as fingerprint,
       count(*) as relations_scanned
from snap;

Then make the rule explicit: if the fingerprint is missing, empty, or structurally different from the previous run, do not overwrite the baseline — raise an alert instead. A broken query must be loud. That is the whole fail-closed idea: the baseline can only be replaced by a run that actually saw the same kind of catalog it saw before.

The ten catalog checks

The monitor I use wraps these as an n8n workflow plus the same checks as standalone SQL:

  • RLS disabled on a table
  • FORCE ROW LEVEL SECURITY absent, so the owner bypasses policies
  • permissive USING (true) on SELECT/ALL
  • FOR ALL policy missing WITH CHECK
  • write policy missing WITH CHECK
  • policy granted to PUBLIC
  • SECURITY DEFINER function without a pinned search_path
  • SECURITY DEFINER owned by a privileged role
  • write grants to anon/authenticated
  • EXECUTE on a function granted to PUBLIC

First run records the baseline silently. Later runs report only NEW and RESOLVED findings, ordered by severity.

Honest limits

This is a catalog check, not an exploit test. It reads pg_catalog, pg_policies, pg_roles and information_schema only — no application rows, no writes. Zero findings does not mean the project is safe, and a policy can be logically wrong while being perfectly present in the catalog. Test on a scratch database before scheduling it against anything you care about.

The reference implementation (14-node n8n workflow + plain SQL) is free: https://kurtoglu1.gumroad.com/l/supabase-rls-drift-monitor-free

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.