Supabase RBAC with custom claims: 5 ways roles leak
#supabase#rls#rbac#auth#postgres
Role-based access control in Supabase is three pieces: a table that says who has which role, a Custom Access Token hook that copies the role into the JWT, and an authorize() function your RLS policies call. The design is sound. The leaks come from where the role is stored and who can write it: a role column users can update on their own row, a role read from user_metadata, a sign-up trigger that copies the role the client sent, roles tables without RLS, and a demoted admin whose token still says admin. Supabase's own RBAC guide created its roles tables without RLS until November 26, 2026.
How does RBAC work in Supabase?
Supabase's RBAC guide builds it in three steps:
- Tables.
user_rolesmaps a user to a role, androle_permissionsmaps a role to permissions such asmessages.delete. - A Custom Access Token hook. Before Supabase Auth issues a token, the hook looks up the user's role and adds it as a
user_roleclaim. The hook has to return the whole event, or every login fails withoutput claims do not conform to the expected schema. - An
authorize()function. It readsuser_rolefromauth.jwt(), checksrole_permissions, and returns true or false. Policies call it:using ((select authorize('messages.delete'))).
One quirk in the guide's hook: it reads the role with select role into user_role from public.user_roles where user_id = …, but the table allows several roles per user (unique (user_id, role)). PL/pgSQL's select … into quietly takes the first row it finds, so a user with two roles gets whichever one Postgres returns first. Either allow one role per user (unique (user_id)) or put every role in the claim and check membership.
Everything below is about the role itself: where it lives, and who can change it.
| Where the role lives | Who can change it | Safe for RBAC? |
|---|---|---|
A user_roles table with RLS and no write policy | Your server, the hook's admin role | Yes |
app_metadata | Only server code with the secret key | Yes |
A role column on profiles | The user, if they can update their own row | Only with column grants |
user_metadata | The user, any time | Never |
1. A role column users can update on their own row
The most common version. Roles live on profiles, and users may edit their own profile:
create table public.profiles (
id uuid primary key references auth.users on delete cascade,
display_name text,
role text not null default 'member'
);
alter table public.profiles enable row level security;
create policy "users update own profile" on public.profiles for update to authenticated
using ((select auth.uid()) = id) with check ((select auth.uid()) = id);
create table public.invoices (id uuid primary key default gen_random_uuid(), amount numeric);
alter table public.invoices enable row level security;
create policy "admins read invoices" on public.invoices for select to authenticated
using (exists (select 1 from public.profiles p where p.id = (select auth.uid()) and p.role = 'admin'));
- Warningsupabase/migrations/20261128120000_profiles_roles.sql:7
Users can update their own role, plan or credits
Limit what users can write: `REVOKE UPDATE ON public.<table> FROM authenticated; GRANT UPDATE (display_name, avatar_url) ON public.<table> TO authenticated;` (list only the columns users should edit). Or add a BEFORE UPDATE trigger that rejects changes to the privileged columns when current_user is authenticated. Better still, keep roles and plans in a table only your server writes.
RLS decides which rows an update may touch, not which columns. The update policy correctly limits users to their own row, and the row includes role. So one call from the browser makes anyone an admin:
await supabase.from("profiles").update({ role: "admin" }).eq("id", user.id);
This is the bug in the VivTrack case study, where the column was the subscription tier instead of the role. The fix is a column grant, so users can update only the columns that are theirs to change:
revoke update on public.profiles from authenticated;
grant update (display_name) on public.profiles to authenticated;
Or move the role out of profiles into user_roles, which users can't write at all. Column-level security covers the grant syntax and how it interacts with select('*').
2. A role read from user_metadata
create policy "admins read reports" on public.reports for select to authenticated
using ((auth.jwt() -> 'user_metadata' ->> 'role') = 'admin');
user_metadata belongs to the user. supabase.auth.updateUser({ data: { role: "admin" } }) succeeds, the next token carries the new value, and the policy lets them in. Supabase's docs say not to use it in RLS policies or authorization logic. user_metadata vs app_metadata covers the difference and how to set app_metadata instead.
3. A sign-up trigger that copies the role the client sent
The same mistake, one step removed. The policy reads profiles.role, which looks safe, but the value came from the client:
insert into public.profiles (id, full_name, role)
values (new.id, new.raw_user_meta_data ->> 'full_name',
coalesce(new.raw_user_meta_data ->> 'role', 'member'));
At sign-up, raw_user_meta_data is exactly what was passed in signUp({ options: { data } }). Anyone can sign up with data: { role: "admin" }. Give every new user the default role in the trigger, and assign real roles from your server.
4. Roles tables without RLS
Until November 26, 2026, Supabase's RBAC guide created user_roles and role_permissions without enabling RLS. The guide now enables it on both, with a warning: on projects that grant Data API privileges on new tables by default, which covers every table created before the October 30 Data API change, "any signed-in user could then read and write these tables, including inserting a row that grants themselves the admin role."
If you followed the guide before that date, check what you have. The hook step did revoke everything on user_roles from anon and authenticated, so that table was closed if you ran the hook migration. role_permissions got no revoke and no RLS, so on a project with the default grants, anyone holding your publishable key could rewrite which role has which permission, or delete them all.
select c.relname, c.relrowsecurity,
has_table_privilege('anon', c.oid, 'insert') as anon_insert,
has_table_privilege('authenticated', c.oid, 'insert') as authenticated_insert
from pg_class c
where c.relnamespace = 'public'::regnamespace
and c.relname in ('user_roles', 'role_permissions');
If relrowsecurity is false on either, enable it now. The guide's design needs no policies for anon or authenticated: the hook reads user_roles as supabase_auth_admin, and authorize() is security definer, so it reads role_permissions on the caller's behalf.
alter table public.user_roles enable row level security;
alter table public.role_permissions enable row level security;
5. A demoted admin keeps the role until the token expires
A role in a JWT claim is a copy. Remove someone's row from user_roles and their current access token still says "user_role": "admin" until it expires, one hour by default. The hook only runs again when the token is refreshed. Signing them out doesn't revoke a token they already have either: signOut ends the session, not the JWT.
For most roles, an hour of lag is acceptable. For the ones where it isn't:
- Check the table, not the claim, for sensitive actions. An
authorize()variant that looks upuser_rolesbyauth.uid()sees a demotion on the next query. - Shorten the JWT expiry under Authentication settings, which narrows the window for every role.
- End their sessions so the token can't be refreshed, and accept that the token in hand works until it expires.
A quick self-check
-- Policies that read user_metadata
select tablename, policyname from pg_policies
where qual ilike '%user_metadata%' or with_check ilike '%user_metadata%';
-- Can signed-in users update the role column on profiles?
select has_column_privilege('authenticated', 'public.profiles', 'role', 'update');
-- Functions that read raw_user_meta_data and mention a role
select p.oid::regprocedure from pg_proc p
where p.pronamespace = 'public'::regnamespace
and p.prosrc ilike '%raw_user_meta_data%' and p.prosrc ilike '%role%';
The first should return nothing, the second should be false, and every function from the third deserves a read. Then run the roles-table query from section 4.
Does GuardLayer catch this?
Yes, for the first four. The code scan reads your migrations together and flags a policy whose decision reads user_metadata (supabase/rls-policy-user-metadata, critical), an own-row update policy on a table with a role or plan column that other policies trust (supabase/self-update-privileged-column), a sign-up trigger that copies a role, plan or tenant out of raw_user_meta_data (supabase/signup-trigger-trusts-user-metadata), and a table created without RLS (supabase/rls-missing-on-table), each with the file, the line and the fix. The fifth is runtime behaviour, a token that hasn't expired yet, and no static scan can see it.
FAQ
Should I store roles in app_metadata or a user_roles table?
Both are safe, because users can't write either. app_metadata is simpler for one role per user. A table is better when roles change often or you need a history, and it's what the Custom Access Token hook in Supabase's guide reads.
Do I need the Custom Access Token hook for RBAC?
No. Without it, authorize() can look up the role in user_roles by auth.uid() on every call. The hook saves that lookup by putting the role in the token, at the cost of the lag in section 5.
How do I do RBAC in a multi-tenant app?
Make the role per tenant: user_roles gets a tenant_id, and authorize() takes the tenant as an argument and checks membership in it. Don't put a single tenant id in the JWT if users can belong to several. Multi-tenant RLS covers the table design.
Is Supabase's authorize() function safe to call over the API?
Yes. It's security definer in public, so anyone can call it through /rest/v1/rpc/authorize, but it only answers whether the caller's own token has a permission. If you'd rather not expose even that, revoke it from public and anon (revoking from anon alone isn't enough, because every role inherits PUBLIC's grant) and grant it back to authenticated, which your policies run as.
Check your Supabase Auth settings and tables, free
Connect your Supabase project read-only. GuardLayer checks your Auth settings (leaked passwords, email confirmation, secure email change, token rotation, CAPTCHA, MFA) and your RLS, policies and grants, and says how to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.