Supabase security · 8 minute read
Supabase RLS: USING vs WITH CHECK — the half of policy review that gets skipped
A policy has two expression slots, and they do different jobs. USING decides which existing rows a command may touch. WITH CHECK decides which new row a write may produce. Reviews that read only the USING clause pass policies that still let a user rewrite a privileged column — or open every command through one FOR ALL line.
Two clauses, two jobs
PostgreSQL evaluates USING as a row filter on existing data and WITH CHECK as a constraint on the row being inserted or updated. Per command, the mapping is fixed:
| Command | USING | WITH CHECK |
|---|---|---|
| SELECT | Which existing rows are returned | Not used |
| INSERT | Not used — there is no existing row | Which new rows may be written |
| UPDATE | Which existing rows may be targeted | Which resulting rows are allowed; omitted, the USING expression is reused for the new row |
| DELETE | Which existing rows may be removed | Not used |
The reuse rule is the first thing people get wrong. When WITH CHECK is omitted on an UPDATE policy, PostgreSQL falls back to the USING expression for the updated row. That looks equivalent — and for a filter like owner_id = auth.uid() it is. It stops being equivalent the moment the write changes a column the check was supposed to constrain.
The privileged-column trap
Consider a profiles table where a user may edit their own row:
CREATE POLICY own_row ON public.profiles
FOR ALL USING (id = auth.uid());The user can target their own row and update it. Because WITH CHECK is omitted, the new row must satisfy id = auth.uid() — the id stays theirs. But nothing in that policy mentions role. An UPDATE that writes role = 'admin' on their own row passes both clauses. The policy protected the rows the user could reach; it never constrained the values the user could write.
The fix pins the privileged column inside the check:
CREATE POLICY own_row ON public.profiles
FOR UPDATE
USING (id = auth.uid())
WITH CHECK (id = auth.uid() AND role = 'member'::public.app_role);The USING clause still decides which row is reachable, and the check now decides what the resulting row may look like. A cleaner shape for apps that legitimately promote users: keep self-editing on a policy that fixes the role, and grant role changes only through a service_role path — never a policy—and never a policy that anon or authenticated can call.
FOR ALL is a shorthand, not a safety net
FOR ALL applies one expression set to every command. Used with a broad USING clause it opens all of them at once: FOR ALL USING (true) reads, inserts, updates and deletes with no per-command distinction. Two catalog checks exist for exactly this shape:
- Check C flags a policy whose USING expression is the constant
trueonSELECTorALL— the whole table becomes world-readable through the anon role if grants allow. - Check D flags a
FOR ALLpolicy with noWITH CHECKexpression at all, so the write path relies entirely on the reused USING clause and no new-row rule is recorded anywhere.
Check D is not a bug by itself — the reuse rule is documented behaviour. It is a finding because it forces a human decision: does the USING expression constrain the new row the way you intend, or only the row was reachable? Policies that silently inherit a read-shaped filter for writes are where the privilege bugs live.
Multiple policies combine with OR — unless you say otherwise
Policies are permissive by default: rows pass if any matching permissive policy allows them. A restrictive policy changes the logic to AND — it cannot grant access, only narrow it. Two consequences worth memorising:
- One accidental
USING (true)permissive policy is enough to expose the table, no matter how strict the other policies are. The OR combines them, andtruemakes the rest dead weight. - A restrictive policy that mentions a role you did not intend (or
PUBLIC) narrows access for everyone, including the legitimate path, and the symptom is usually reported as “RLS blocking valid reads.”
The catalog shows which is which in one row per policy: permissive reads PERMISSIVE or RESTRICTIVE, and roles lists the roles the policy applies to.
Read it from the catalog in one query
Every clause above is stored in pg_policies in node-parsed form: qual is the compiled USING expression and with_check the compiled WITH CHECK expression (NULL when omitted). The command each policy covers is in cmd. A read-only query that surfaces the interesting cases:
SELECT tablename, policyname, cmd, permissive, roles,
qual::text AS using_expr,
with_check::text AS with_check_expr
FROM pg_policies
WHERE schemaname = 'public'
AND ( qual::text ILIKE '%true%'
OR with_check IS NULL
OR 'public' = ANY (roles) )
ORDER BY tablename, policyname;Three signals in one result: a USING clause reduced to true, a policy with no WITH CHECK at all, and a policy scoped to PUBLIC so it applies to every role. They are findings to review, not proof of exposure — a true USING clause on a table with no anon grant is harmless, and a NULL with_check is only a bug when the write path needs one.
Why this belongs in a nightly diff
Policy reviews happen at release time; policies change in migration files nobody re-reads. The failure mode is silent: a column added later, a role switched from restrictive to permissive, a USING (true) added to unblock a support ticket. A one-time audit cannot see that. A read-only nightly check diffs the pg_policiesfingerprint against a baseline, reports only new or resolved rows, and fails closed when the credential breaks instead of writing an empty baseline that looks like “clean.”
Checks C, D and E in the free, read-only RLS drift monitor cover the three shapes above (constant-true USING on read/ALL, missing WITH CHECK on write policies, and PUBLIC-scoped policies), plus ten more catalog checks. The standalone SQL runs them on any PostgreSQL without n8n, with a read-only role and no service_role key. Get the free RLS Drift Monitor (13 checks, n8n + SQL) →
Honest limits
The query above reads catalog metadata; it does not prove anyone exploited anything. It does not read application rows, does not run as service_role, and does not modify the database. The semantics described here were verified against a local PostgreSQL 17 fixture, not against hosted Supabase projects; hosted defaults and the revision a project runs are outside what a public catalog query can tell you. Zero findings does not mean safe — a policy can be syntactically fine and still wrong for your access model. If you want a person to read the results and separate real exposure from noise, that is a separate fixed-scope review: Supabase RLS Spot Check. Work starts only after payment is secured.
Related: two RLS gaps a per-table review misses (pg_default_acl, view owner) · detect RLS drift with a read-only nightly check · anon vs service_role vs sb_secret_ key types.