What should a multi-tenant policy actually check?
In the usual Supabase layout, every tenant table has an org_id, and an org_members table links users to organisations. A correct read policy answers one question for each row: is the caller a member of the organisation that owns this row?
Supabase's performance guide shows the shape, with a membership table called team_user: team_id in (select team_id from team_user where user_id = (select auth.uid())) (RLS performance). Most leaks between tenants are policies that drifted from that question. OWASP lists cross-tenant data leakage first among the risks of shared databases (OWASP Multi-Tenant Security Cheat Sheet).
We ran each broken policy below on a local PostgreSQL 17 copy with two organisations and Supabase's authenticated role. Each returned the other organisation's rows; each fix returned only the caller's.
Mistake 1: "is a member of something" instead of "is a member of this"
-- Broken: true for any user who belongs to any organisation
create policy "Members read projects" on public.projects
for select to authenticated
using (
exists (select 1 from public.org_members
where user_id = (select auth.uid()))
);
The subquery never mentions projects.org_id, so anyone with one membership reads every organisation's projects.
-- Fixed: the row's org_id must be one of the caller's organisations
create policy "Members read projects" on public.projects
for select to authenticated
using (
org_id in (select om.organization_id from public.org_members om
where om.user_id = (select auth.uid()))
);
Mistake 2: a column name that quietly means the outer table
This one looks correct. The membership table calls its column organization_id, the projects table calls it org_id, and the policy was written as if both used org_id:
-- Broken: org_members has no org_id column, yet this runs without error
using (
org_id in (select org_id from public.org_members
where user_id = (select auth.uid()))
)
PostgreSQL raises no error. When a name in a subquery is not a column of the subquery's own table, it is looked up in the outer query, so select org_id returns projects.org_id and the test becomes org_id in (org_id), true for every row whenever the caller has at least one membership. The PostgreSQL wiki explains this behaviour, which follows the SQL standard, and recommends qualifying column names so the mistake raises an error instead (PostgreSQL FAQ).
The fix is the one from mistake 1: give every table an alias and write om.organization_id. With the alias, a wrong column name fails loudly.
Mistake 3: reads are scoped, writes are not
Generated schemas often pair a careful select policy with a loose one for writes:
-- Broken: any signed-in user can create rows in any organisation
create policy "Members add projects" on public.projects
for insert to authenticated
with check (true);
The same goes for an update policy whose using checks membership but whose new row is never checked against the caller's organisations. PostgreSQL evaluates with check "against the proposed new contents of the row" (PostgreSQL, CREATE POLICY), so it is the clause that stops a user writing org_id for an organisation they are not in, or moving their own row into someone else's workspace. Supabase's linter flags WITH CHECK (true) and a missing WITH CHECK on insert and update policies as unrestricted access (lint 0024).
create policy "Members add projects" on public.projects
for insert to authenticated
with check (org_id in (select private.my_org_ids()));
create policy "Members edit projects" on public.projects
for update to authenticated
using (org_id in (select private.my_org_ids()))
with check (org_id in (select private.my_org_ids()));
private.my_org_ids() is defined further down.
Mistake 4: anyone can join any organisation
-- Broken: a user may insert a membership row for themselves, in any organisation
create policy "Users join orgs" on public.org_members
for insert to authenticated
with check (user_id = (select auth.uid()));
Every tenant policy trusts org_members, so whoever can write to it widens their own access. In our test, one insert of (the other organisation's id, my user id) was enough for every correctly written policy to start returning the other organisation's rows. Membership rows should be created by an organisation admin, or by your server when it accepts an invitation:
create policy "Admins add members" on public.org_members
for insert to authenticated
with check ((select private.is_org_admin(organization_id)));
private.is_org_admin is a helper like the one below that also checks role in ('owner', 'admin'). Your server adds the first owner when it creates an organisation.
Mistake 5: the organisation comes from somewhere the user controls
Three versions of the same mistake:
- The client filters, the policy does not. The app sends
.eq('org_id', currentOrg)and the policy isusing (true)forauthenticated. Removing the filter from the request removes the isolation. - The organisation is read from
user_metadata. Supabase warns that users can edit their own user metadata, "so a role or tenant ID stored there lets a user grant themselves access" (managing user data). Useapp_metadataor the membership table. - An old permissive policy is still there. PostgreSQL combines permissive policies with OR (PostgreSQL, CREATE POLICY), so a forgotten
using (true)from an early prototype cancels the careful policy written later.
The pattern that holds up
Supabase recommends a security definer function in a schema the API does not expose for membership checks, which also avoids the infinite recursion error when org_members has policies of its own (row level security):
create schema if not exists private;
create function private.my_org_ids()
returns setof uuid
language sql stable
security definer
set search_path = ''
as $$
select om.organization_id from public.org_members om
where om.user_id = (select auth.uid())
$$;
revoke execute on function private.my_org_ids() from public;
grant usage on schema private to authenticated;
grant execute on function private.my_org_ids() to authenticated;
alter table public.projects enable row level security;
create policy "Members read projects" on public.projects
for select to authenticated
using (org_id in (select private.my_org_ids()));
-- plus the insert and update policies above, and a delete policy with the same using
create index on public.projects (org_id);
create index on public.org_members (user_id);
Supabase notes that a composite primary key on (team_id, user_id) does not index user_id on its own, so the membership lookup needs its own index (same page).
Supabase also shows a policy that reads organisation ids from the token's app_metadata, with a caveat: removing someone from a team only takes effect when their token is refreshed (same page).
How do I check my own app?
1. Read every policy on every tenant table. In the SQL Editor:
select c.relname as table_name,
c.relrowsecurity as rls_on,
p.policyname, p.cmd, p.roles, p.qual, p.with_check
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
join pg_attribute a on a.attrelid = c.oid
and a.attname in ('org_id', 'organization_id', 'team_id', 'workspace_id', 'tenant_id')
and not a.attisdropped
left join pg_policies p on p.schemaname = n.nspname and p.tablename = c.relname
where n.nspname = 'public' and c.relkind = 'r'
order by 1, 4, 3;
Look for rls_on false, a qual of true or one that never mentions the tenant column, insert policies with a loose with_check, and any membership insert policy that lets users add themselves.
2. Query as a real user of one organisation. Supabase's own policy tests switch role and user inside a transaction (testing RLS with pgTAP). You can do the same by hand:
begin;
set local role authenticated;
set local request.jwt.claim.sub = 'USER-ID-FROM-ORG-A';
select org_id, count(*) from public.projects group by org_id;
rollback;
One row, for organisation A, is a pass. Repeat for each tenant table, then try an insert with organisation B's id; it should fail with "new row violates row-level security policy".
3. Repeat it through the app. Create test accounts in two organisations, copy a request from the Network tab, change or remove the org_id filter and send it as the other account. OWASP advises testing through the role and connection path the app uses, proving that cross-tenant operations fail and same-tenant ones work (OWASP cheat sheet).
Views and security definer functions in public can skip these policies; our article on views and functions that skip row level security shows how to list them.
Checklist
| Check | Pass |
|---|---|
| RLS on every table with a tenant column | rls_on true for each |
| Read policy ties the row to the caller's organisations | Mentions org_id and the membership list |
Insert and update check the new org_id | with check uses the same list |
| Membership table writable only by admins or the server | No self-insert policy |
Tenant id never from user_metadata or the request | Policies read org_members or app_metadata |
No leftover using (true) | Advisor lint 0024 is clear |
| Two-organisation test passes | Zero rows from the other organisation |