Abbi Labs

Guides

Supabase team and organization RLS: can any user add themselves to someone else's team?

By Abbi Labs · Last updated 27 November 2026

Scan your app free and see what a stranger can already read.

Abbi Labs checks your live site from the outside: Supabase tables readable with your public key, secret keys like service_role in the JavaScript, exposed .env and .git files, security headers, CORS, cookie flags and TLS certificate expiry. Read-only. No sign-up for the quick check.

No app handy?

Example result · our deliberately broken demo app

6 issues found: 3 critical, 1 high, 1 medium, 1 low

  • Critical: Stripe live secret key in public JavaScript
  • Critical: /.env file readable by anyone
  • High: Git repository exposed (/.git/HEAD)

The demo's problems are planted on purpose. Paste your own URL above to see yours.

No sign-up. Read-only public-resource and protocol metadata checks. No model prompts or tool execution. Scan only sites you own or are allowed to test.

Run on every deploy or use from Cursor / Claude Code via MCP →

Weekly AI-app security checklist

One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.

Most team apps built on Supabase have the same shape: a teams (or organizations, workspaces) table, a team_members table, and every other table with a team_id. The policies on projects, documents and invoices all say the same thing: you can see a row if you're a member of its team. That makes team_members the lock on every door. If anyone can write to it, every other policy is open.

Policy behaviour below is from Supabase's Row Level Security docs. Short version in our tip: can any user add themselves to someone else's team?

The policy that lets anyone join any team

The insert policy on team_members often gets written like every other own-row policy:

-- Looks safe, isn't
create policy "users can add themselves"
on public.team_members for insert
to authenticated
with check ((select auth.uid()) = user_id);

It checks that the new row has your user id. It never checks the team_id. So any signed-in user can do this from the browser console with your public key:

await supabase.from("team_members")
  .insert({ team_id: "<someone else's team id>", user_id: myUserId });

Now every policy that says "member of this team" lets them in. If sign-ups are open (the default), "signed-in user" means anyone with an email address.

Why AI-built apps end up with this

Find it: one query in the SQL Editor

select tablename, policyname, cmd, roles, qual, with_check
from pg_policies
where schemaname = 'public'
  and tablename in ('team_members', 'organization_members', 'memberships', 'workspace_members')
order by tablename, cmd;

Change the table names to yours. For each INSERT, UPDATE or ALL row, read with_check. If it only mentions user_id and auth.uid() and never team_id, anyone can join any team. If there's no INSERT policy at all and RLS is on, the browser can't insert, which is what you want if members are added from the server.

Prove it with two accounts (2 minutes)

  1. As account A, create a team. Copy its team_id from the Table Editor.
  2. In a private window, sign up as account B. Open the console on your app and run the insert above with A's team_id and B's user id.
  3. If it succeeds, reload as B. Can B see A's projects or documents? That's the bug.
  4. Also try, as B on B's own membership row: update({ role: 'owner' }). It should fail or change 0 rows.
  5. Delete the test row afterwards.

Prefer SQL? Our guide to testing RLS as a real user in the SQL Editor shows how to run the same insert as B inside a transaction you roll back.

The fix

Option 1 (simplest): no direct inserts from the browser. Drop the insert policy. Add members from server code (an API route or Edge Function) after checking an invite: it exists, isn't expired, and its email matches the signed-in user. The team creator's own owner row gets added in the same server call that creates the team.

drop policy "users can add themselves" on public.team_members;
-- with RLS on and no INSERT policy, the browser can't insert

Option 2: only existing owners can add members. Use a security definer function in a schema that isn't exposed, so the policy doesn't read team_members through RLS (this also avoids recursion):

create schema if not exists private;

create function private.is_team_owner(t uuid)
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
  select exists (
    select 1 from public.team_members
    where team_id = t
      and user_id = (select auth.uid())
      and role = 'owner'
  );
$$;

create policy "owners add members"
on public.team_members for insert
to authenticated
with check ((select private.is_team_owner(team_id)));

And don't let members edit their own membership row. If you need role changes, do them as an owner-only policy or from the server.

Fixing "infinite recursion detected in policy for relation team_members"

This is Postgres error 42P17. It happens when a policy on team_members selects from team_members, or when teams and team_members policies read each other. Supabase's docs break the cycle with a security definer function that reads memberships as its owner (postgres, which bypasses RLS on Supabase):

create function 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 "members read projects"
on public.projects for select
to authenticated
using (team_id in (select private.my_team_ids()));

Two rules from the docs worth repeating: put the function in a schema that's not listed under Exposed schemas in your API settings (a security definer function in public can be called by anyone over /rest/v1/rpc), and set search_path = ''. Don't fix the recursion with using (true). That turns the membership table into a public list of who belongs to which team.

Paste this into your AI builder

Audit the RLS on team_members (and any organization/workspace members table).
1. Users must not be able to insert a membership for a team they don't own.
   Remove any INSERT policy that only checks auth.uid() = user_id.
   Add members from server code after validating an invite, or allow
   inserts only when the caller is already an owner of that team_id.
2. Members must not be able to update their own role.
3. Use a security definer function in a non-exposed "private" schema with
   set search_path = '' for membership checks, to avoid 42P17 recursion.
   Never use using (true) on team_members.
4. Show me the final policies from pg_policies.

Then run the two-account test yourself. Don't take "fixed" from the agent as the test.

What Abbi Labs checks

Honest scope: the free outside scan reads your live site signed out with your own public key. It tests whether tables your app's JavaScript references (plus common names) return rows to a signed-out visitor, so a team_members or teams table open to strangers shows up. It also reports secret keys (including service_role) in your JavaScript, exposed .env and .git files, security headers, CORS, cookie flags and TLS expiry. It never writes to your database and doesn't sign in, so it can't try to join a team. The free policy checker reads your policies in your browser and flags INSERT and UPDATE policies that only check "signed in" and with check (true). A user_id-only check on a membership table looks like a normal own-row policy to it, so use the query above for that one.

Scan your app free

FAQ

Isn't with check ((select auth.uid()) = user_id) enough on team_members?

No. It proves the new row is about you, not that you're allowed into that team. The team_id is whatever the client sends, so a signed-in user can insert their own id next to any team_id they know or guess.

How do users find another team's ID?

IDs leak in invite links, URLs, shared screenshots, logs and other API responses. Random UUIDs make guessing harder, but they aren't a permission check.

What causes 'infinite recursion detected in policy for relation team_members'?

Postgres error 42P17. A policy on team_members reads team_members (or two tables' policies read each other), so evaluating one policy triggers the other forever. Supabase's RLS docs fix it with a security definer function in a schema that isn't exposed to the Data API, which reads memberships as its owner.

Should I just use the service_role key to add members?

Adding members from server code after you've checked the invite is fine, and it's a common pattern. Keep that key on the server only, and make the server code do the checks: the invite exists, isn't expired, and matches the signed-in user's email.

Can users also change their own role in the team?

If there's an UPDATE policy on team_members that only checks user_id, yes: a member can set role = 'owner' on their own row. Don't allow members to update their own membership row, or restrict the columns they can update.

Does Abbi Labs catch this?

Partly. The free outside scan is signed out, so it reports team or membership tables that a stranger can read with your public key, but it can't sign in and try to join a team. The free policy checker flags INSERT and UPDATE policies that only check the caller is signed in. A team_members policy that checks user_id but not team_id looks like a normal own-row policy, so use the query and two-account test on this page.

Related guides

Weekly AI-app security checklist

One short email a week: a practical check for apps built with Supabase, Next.js and AI tools (RLS, leaked keys, headers and more). Unsubscribe anytime.