Supabase security · 7 minute read
Multi-tenant Supabase RLS: the org_id membership pattern, measured
A multi-tenant app stores every row with an org_id and decides access through a membership table. The instinctive policy — "users see rows where org_id matches their org" — is easy to write and easy to get half wrong. Below is a real PostgreSQL 17 fixture with four read-only catalog checks and three runtime identity tests, plus the one write-path gap that standard advice misses.
The fixture
Three tables, two orgs, three users, three projects. The relevant DDL (read-only roles; no service_role):
create table public.orgs (
id uuid primary key,
name text not null
);
create table public.org_members (
org_id uuid not null references public.orgs(id),
user_id uuid not null,
role text not null default 'member',
primary key (org_id, user_id)
);
create table public.projects (
id uuid primary key,
org_id uuid not null references public.orgs(id),
name text not null
);
alter table public.projects enable row level security;
create policy "members read projects"
on public.projects
for select
to authenticated
using (
exists (
select 1 from public.org_members m
where m.org_id = public.projects.org_id
and m.user_id = (select auth.uid())
)
);
create policy "members insert projects"
on public.projects
for insert
to authenticated
with check (
exists (
select 1 from public.org_members m
where m.org_id = NEW.org_id
and m.user_id = (select auth.uid())
)
);Policies are SECURITY INVOKER by default — they run as the caller, which is what makes them testable per identity. auth.uid()reads the caller's JWT sub claim.
Test 1: the anonymous key sees nothing
With no JWT (the anon role), a plain SELECT returns a permission error, not an empty result:
permission denied for table projectsThat is the correct boundary: anon must not enumerate tenant data even to learn it exists. If your anon-key probe earlier returned rows, the table has RLS off or an overly broad policy — see the checklist link at the end.
Test 2: same org, two members see the same two rows
Two different users in Acme (owner and member), each with their own JWT sub, both see exactly Acme's two projects:
rows_visible
------------
2Row-level security is per-request, not per-session cache. A shared org shows the same slice to every member of it.
Test 3: cross-org insert is blocked
A Globex member tries to insert a project into Acme's org_id:
new row violates row-level security policy for table "projects"WITH CHECK gates the row being produced, so a user cannot write into another tenant's org even if they can read their own. This is the part tutorials most often skip: they show USINGfor reads and forget WITH CHECK for writes.
Test 4: same-org read does not leak other tenants
A Globex owner queries select count(*) from projects and receives Globex's rows only — never Acme's. The policy uses the caller's auth.uid() against org_members.user_id, so identity is bound to the request, not to a client-suppliedorg_id in the body.
The write-path gap tutorials miss
An update policy that only lists USING (no WITH CHECK) still evaluates USING against the new row in PostgreSQL, so a naive "ownership-only" update can look safe in a read-heavy demo but behave differently once a column likeorg_id or status changes. The rule:
USING— which existing rows this statement may touch.WITH CHECK— which new row this statement may produce.
For multi-tenant data, both should reference the tenant boundary (org_id via org_members). A policy with only USING is not automatically a leak, but it is not a substitute for WITH CHECK either.
Four read-only catalog checks
Run these against a scratch copy of your project. None of them writes or reads application rows.
-- 1. RLS on every tenant table
select n.nspname, c.relname
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public'
and c.relkind = 'r'
and not c.relrowsecurity;
-- 2. Policies reference auth.uid() (not a client-supplied column)
select tablename, policyname, cmd, qual
from pg_policies
where schemaname = 'public'
and qual not like '%auth.uid()%'
and qual not like '%current_setting(%';
-- 3. INSERT policies have WITH CHECK
select tablename, policyname
from pg_policies
where schemaname = 'public'
and cmd = 'INSERT'
and with_check is null;
-- 4. anon holds no table grants
select table_name, grantee, privilege_type
from information_schema.role_table_grants
where table_schema = 'public'
and grantee = 'anon'
and privilege_type in ('INSERT','UPDATE','DELETE');Honest limits
These were measured against a local PostgreSQL 17 fixture with a hand-written auth.uid() shim — not against a hosted Supabase project. Hosted defaults, the exact GoTrue/PostgREST revision, and the real JWT your client sends are outside what a local catalog query can show. Zero findings means "no catalog gap I know how to detect", not "your tenant isolation is safe".
The recurring failure is not a missing check — it is that the schema changed after the check. A nightly read-only diff over the same catalog queries reports only new fingerprints and fails closed when the credential breaks:
Get the free RLS Drift Monitor (13 checks, n8n + SQL) →
If you want a person to read the output and separate real exposure from noise, that is a fixed-scope review: Supabase RLS Spot Check. Work starts only after payment is secured.
Related: the 13-point checklist for AI-built Supabase apps · Supabase Realtime security: table RLS does not protect your channels · the first 15 minutes of a Supabase security audit.