DEV Community

Cenk KURTOĞLU
Cenk KURTOĞLU

Posted on Edited on

Multi-tenant RLS in Supabase: the 7 isolation bugs I find in every AI-built SaaS

Multi-tenant RLS in Supabase: the 7 isolation bugs I find in every AI-built SaaS

I review multi-tenant Supabase security for a living. Last month I audited an AI-built admission-gate system — here are the seven isolation bugs that keep showing up, in the exact order you should check for them.

by Cenk Kurtoglu — github.com/cekuu35


1. RLS enabled, zero policies = silent deny-all (or silent chaos)

ALTER TABLE ... ENABLE ROW LEVEL SECURITY with no policies means every query returns empty — which developers "fix" by adding a policy that allows everything. Check: does every tenant-visible table have a policy set, and does every policy deny by default?

2. USING (true) policies that eat your restrictive ones

Postgres combines permissive policies with OR. One sloppy USING (true) policy on a table makes every other restrictive policy irrelevant — the OR chain lets anyone through. Two restrictive policies plus one permissive one = zero protection.

Check: SELECT * FROM pg_policies WHERE tablename = 'your_table'; — count permissive policies. Any USING (true) that isn't deliberate is a tenant leak.

3. Missing WITH CHECK on UPDATE

UPDATE policies have two halves: USING (can I see the row to update?) and WITH CHECK (can the row look like this AFTER my update?). Missing WITH CHECK means a tenant can update someone else's row if they can see it — or worse, mutate tenant_id itself and migrate the row into their tenant.

4. SECURITY DEFINER functions reachable by anonymous callers

An anon-reachable SECURITY DEFINER function runs with owner privileges — bypassing every RLS policy on every table it touches. Grep your migrations: every SECURITY DEFINER function needs a revoke all on function ... from public, anon and an explicit grant to the roles that need it.

5. The INVOKER/privilege contradiction that breaks your claim RPC

This one is subtle and I missed it on first pass in a recent audit (the client's own team caught it — credit where due). A claim function declared SECURITY INVOKER while the target table's INSERT privilege is revoked from authenticated (and no INSERT policy exists) means: the function executes with the caller's rights, and the inner INSERT fails with permission-denied. The function "exists and is callable" but never succeeds.

The fix: either SECURITY DEFINER with an internal auth.uid() gate and set search_path = '', or INVOKER plus a scoped INSERT policy (actor_user_id = auth.uid()). Then prove it with a three-session test: anon rejected, authenticated claims once, concurrent claim raises a serialization error.

6. Unique constraint + ON CONFLICT DO NOTHING = permanent lockout

A lease table with unique(org, tenant, run_key) and ON CONFLICT DO NOTHING prevents double-claims — but it ALSO permanently blocks legitimate re-claims after the lease expires or is revoked. The surviving row wins forever.

The fix: ON CONFLICT ... DO UPDATE SET ... WHERE your_table.lease_expires_at <= now() OR your_table.revoked_at IS NOT NULL. Live rows still conflict; dead rows yield. Add the three-session test: expired → re-claim succeeds; revoked → re-claim succeeds; live → conflict.

7. App-layer-only enforcement (the invisible single point of failure)

If your scheduler returns "reservation commands" and some unreviewed executor applies them, your atomicity lives in code nobody audited. Enforcement that isn't in the database can be bypassed by any code path that skips it.

The fix: the DB must own the invariant — constraints, exclusion rules, atomic claims. App code coordinates; the database decides.


The 20-minute self-audit

  1. List every table with SELECT * FROM pg_tables WHERE schemaname = 'public';
  2. For each: SELECT * FROM pg_policies WHERE tablename = ... — deny by default? WITH CHECK everywhere?
  3. Grep migrations for SECURITY DEFINER — revoke anon on every one
  4. Find every RPC your app calls — verify each definition's privileges and org binding
  5. Write the three-session concurrency test for any claim/lease table

If step 2 or 4 scares you, that's the audit talking.


I do fixed-scope multi-tenant Supabase security reviews (policy matrix, negative tests, ready-to-run fix SQL) — github.com/cekuu35, cenkkurtoglu.com. I also disclose leaked keys for free, permanently.


Want the exact SQL pack? All 7 checks + fix SQL + the 3-session test, packaged for a 20-minute self-audit: Multi-Tenant RLS Self-Audit Pack - $39.

Top comments (0)