Supabase database
Supabase multi-tenant apps: how to test that one customer can’t see another’s data
If your app has teams, organisations, workspaces or clients, the question that matters is whether one customer can ever see another customer’s rows. This week a founder asked it on Reddit, with a budget for a one-day review. Most of the answer is five tests you can run yourself. One of them catches a hole that is easy for an AI builder to write: a members table that lets any signed-in user add themselves to any team. We rebuilt it on Postgres 17 to show what it does.
TL;DR
- Multi-tenant Supabase apps usually scope every table with one rule: “you can see this row if you are a member of its team”. That rule is only as strong as the members table behind it.
- If the members table has an insert policy like
with check (user_id = auth.uid()), any signed-in user can insert themselves into your team. In our Postgres 17 test, an outsider read 0 customer rows, ran one insert, then read 2. - Fix: nobody writes their own membership. Teams are created and invites accepted through
security definerfunctions; only an existing admin can add members. - Test isolation with two real users in two teams, through the API, not by reading policies. The five tests below take under an hour.
- Our free, open-source CLI flags the members-table hole as PL-038:
uvx --from git+https://github.com/manra399/patchlabs-cli patchlabs scan .
This guide is for founders and agencies with a Supabase app built in Lovable, Bolt, Cursor or Claude Code that serves more than one customer organisation. The test results are from our own run on 17 November 2026 on Postgres 17 with Supabase-style anon and authenticated roles and fake rows. They show the pattern, not any particular app.
How tenant isolation usually works in Supabase
There is one membership table, often team_members, org_members or memberships, with a user id, a team id and a role. Every table that holds customer data carries a team_id, and its policy asks whether the caller is a member of that team:
create policy "Members read customers" on public.customers
for select to authenticated
using (team_id in (select team_id from public.team_members
where user_id = (select auth.uid())));That policy is fine. Its safety depends entirely on who can write to team_members. If a user can add a row there, they decide which teams they belong to, and every policy in the app that says “members only” agrees with them.
The members-table hole
When an AI builder wires up “create a team” or “accept an invite”, the first insert into team_members fails with “new row violates row-level security policy”. The policy that makes the error go away is short and looks safe:
create policy "Users can join" on public.team_members
for insert to authenticated
with check (user_id = (select auth.uid())); -- "you can only add yourself"“You can only add yourself” is true. But it doesn’t say to which team. Team ids are not secrets: they appear in URLs, invite links and API responses. The same problem appears with an UPDATE policy that lets a user edit their own membership row, because they can change its team_id or its role.
What we saw on Postgres 17
We built the tables above with two teams, a victim team with two customer rows and an attacker in their own team, then acted as the attacker through the authenticated role:
| Step | As the attacker | Result |
|---|---|---|
| Before | select count(*) from customers | 0 rows |
| Join | insert into team_members values (victim_team, my_id, 'member') | Accepted |
| After | select count(*) from customers | 2 rows: the victim team’s customers |
| After the fix | The same insert | Refused by row level security; still 0 rows |
| After the fix | An admin adds a member to their own team | Works |
Through the Supabase API that insert is one call from the browser console: supabase.from('team_members').insert({ team_id, user_id, role: 'member' }). If the policy doesn’t pin role, the attacker can set it to admin too.
The fix
Nobody writes their own membership. Remove the self-service policies and route the three legitimate ways in through functions that check the right thing:
drop policy if exists "Users can join" on public.team_members;
-- 1. An existing admin adds members to their own team
create function public.is_team_admin(t uuid) returns boolean
language sql stable security definer set search_path = '' as $$
select exists (select 1 from public.team_members
where team_id = t and user_id = (select auth.uid()) and role = 'admin') $$;
create policy "Admins add members" on public.team_members
for insert to authenticated with check (public.is_team_admin(team_id));
-- 2. Creating a team adds the caller as its first admin, in one step
create function public.create_team(team_name text) returns uuid
language plpgsql security definer set search_path = '' as $$
declare t uuid;
begin
insert into public.teams (name) values (team_name) returning id into t;
insert into public.team_members (team_id, user_id, role) values (t, auth.uid(), 'admin');
return t;
end $$;
-- 3. Accepting an invite checks the token, the invited email and the expiry
-- (accept_invite(token): look up the invite, compare auth.email(), check expires_at,
-- insert the membership, mark the invite used)
revoke execute on function public.create_team(text) from anon;If you keep an UPDATE policy on the members table (to let people change a display setting, say), give it a with check that keeps team_id and role unchanged, or move those columns out of reach. The PL-038 finding page has a copy-paste prompt for your builder.
Why security definer with set search_path = '': the helper reads team_members inside a policy on team_members, and running it as its owner avoids an infinite policy loop. The empty search path stops someone shadowing team_members with their own table. See security definer functions.
Five tests for “can tenant A see tenant B”
Reading policies tells you what someone meant. These tests tell you what happens. Create two users in two different teams, with a few fake rows each, and sign in as each through the API (the Supabase client in a browser console works).
- Every table has row level security on. In the SQL editor:
select tablename from pg_tables where schemaname = 'public' and not rowsecurity;should return nothing you care about. - Read across. As user A, select from every table that has a
team_id. You should see only team A’s rows. Do the same through every view and everyrpc()function the app calls, because views and security definer functions skip policies by default. - Write across. As user A, try to insert a row with team B’s id, update one of team B’s rows, and delete one. Each should change 0 rows or be refused.
- Join across. As user A, insert yourself into team B’s members table and try to change your own membership’s
team_idandrole. Both should be refused. This is the test that catches PL-038. - Files and keys. Try to download team B’s files by path through Storage, and search the built frontend for the
service_rolekey, which skips every policy above.
Write the results down as pass or fail per table. If you hire someone for a review, ask them for exactly this list.
Find the members-table hole in a minute
Our command-line scanner is free and open source. It reads your SQL files as text, never connects to your database and sends nothing anywhere. From version 0.5.0 it finds the members tables your other policies depend on (directly, or through a helper such as is_member()) and flags write policies on them that only check “the row is mine”:
uvx --from git+https://github.com/manra399/patchlabs-cli patchlabs scan .It passes policies that check an admin role, an invite or a helper function, and it ran clean on six open-source Supabase SaaS starters, including basejump and makerkit, which add members through functions. It can’t see policies created by hand in the dashboard and never written to a migration, which is one more reason to run the five tests above.
Related findings
The checks behind this guide, each with example code, a self-check and the fix.
Any signed-in user can add themselves to any team
PL-002Any signed-in user can read every row
PL-004UPDATE policy missing WITH CHECK
PL-019Access decided by user_metadata, which users can edit
PL-006SECURITY DEFINER function exposed
Free check
Check your app for this, free.
Paste your public repo. PatchLabs rebuilds your database from its migrations, tests it as a visitor, a stranger and the owner, reads the code for keys and risky patterns, and hands you the fix.
Written by Manraj Singh Dhillon. Last checked 17 Nov 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.