r/Supabase • u/Round-Metal6269 • 10h ago
database If you "fixed" infinite recursion on team_members with using (true), your member list is public. Here's the fix the docs use
Disclosure: I build a security scanner for AI-built apps. No link, just SQL.
I keep seeing the same thing in team / workspace apps. A policy on team_members needs to check team_members ("can I see other members of my team?"), Postgres throws 42P17 infinite recursion detected in policy for relation "team_members", and the fix that makes the error go away is using (true). Now every signed-in user can list who's in every team, and so can anyone with the anon key if the policy didn't say to authenticated.
The pattern in the Supabase RLS docs is a security definer function that does the membership lookup as its owner, so the policy doesn't call itself:
not exists private;
.my_team_ids
returns setof uuid
language sql
security definer
set search_path = ''
stable
as $$
select team_id from public.team_members
where user_id = (select auth.uid)
$$;
create policy "see members of my teams"
on public.team_members for select
to authenticated
using (team_id in (select private.my_team_ids));
Two things that matter:
- Put it in a schema that isn't in your API's exposed schemas. A security definer function in public can be called by anyone at /rest/v1/rpc.
- It only skips RLS because the owner is postgres. If you created it as some other role, it'll recurse again.
While you're in there, check the insert policy on team_members:
select policyname, cmd, with_check
from pg_policies
where tablename = 'team_members' and cmd in ('INSERT', 'ALL');
If with_check is just auth.uid() = user_id, it checks the row is you but not which team you're joining. Any signed-in user can insert themselves into another team's id and every "member of this team" policy lets them in. Safer is no client inserts at all (add members server-side after checking the invite) or only allowing inserts when the caller is already an owner of that team, using the same kind of function.
Quick test is two accounts. Make a team as A, sign up as B, try inserting B into A's team from the browser console. Delete the row after.
Curious how others handle invites here. Edge function, or a policy that checks an invites table?