Abbi Labs

Guides

Supabase custom claims and RBAC with the custom access token hook: the 5 places it goes wrong

By Pritesh Ghodge, Senior Solutions Engineer (cybersecurity) · Last updated 15 November 2026

Scan your app free — see which Supabase tables your public key can read.

Abbi Labs looks at your live site from the outside: Supabase tables readable with your public key, secret keys in the JavaScript, exposed .env and .git files, source maps, and security headers. No sign-up for the quick check.

No app handy?

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.

Sooner or later every Supabase app needs an admin. The pattern Supabase recommends is a user_roles table plus a custom access token hook that copies the user's role into the JWT as a claim like user_role. RLS policies then read auth.jwt() ->> 'user_role' instead of querying a table on every row. It's a good pattern. But when an AI agent wires it up from a half-remembered snippet, a few small omissions turn it into "any user can become admin". Here are the five I'd check.

1. Users can write their own role

The whole system trusts user_roles. If authenticated has an INSERT or UPDATE policy on it (or RLS is off), a user can add admin for themselves and get it in their next token. Supabase's guide locks the table to the hook only:

revoke all on table public.user_roles from authenticated, anon, public;
grant all on table public.user_roles to supabase_auth_admin;

alter table public.user_roles enable row level security;
create policy "Allow auth admin to read user roles" on public.user_roles
  as permissive for select to supabase_auth_admin using (true);

Check what's actually there:

select grantee, privilege_type from information_schema.role_table_grants
where table_schema = 'public' and table_name = 'user_roles';

select policyname, roles, cmd, qual, with_check from pg_policies
where schemaname = 'public' and tablename = 'user_roles';

You want no rows for anon or authenticated in the first query, and no policy for them in the second. Assign roles from server code (secret key) or SQL, never from the client. Same problem, older shape: the is_admin column users can flip themselves.

2. The hook function is callable over the API

A function in the public schema is exposed at /rest/v1/rpc/custom_access_token_hook unless you revoke execute. Supabase's guide does exactly that:

grant usage on schema public to supabase_auth_admin;
grant execute on function public.custom_access_token_hook to supabase_auth_admin;
revoke execute on function public.custom_access_token_hook from authenticated, anon, public;

If the revoke is missing, anyone with your anon key can call it with a made-up event and any user_id, and read back that user's role. Test it from outside:

curl -s -X POST "https://<project>.supabase.co/rest/v1/rpc/custom_access_token_hook" \
  -H "apikey: <anon key>" -H "Content-Type: application/json" \
  -d '{"event": {"user_id": "00000000-0000-0000-0000-000000000000", "claims": {}}}'

You should get a permission error, not JSON back.

3. The app trusts a decoded token

The hook only changes the access token, not the auth response, so to show an admin menu you decode session.access_token (Supabase's guide uses jwt-decode). That's fine for UI. It is not authorization: decoding doesn't verify anything, and the browser is the user's. Every admin action must be blocked by an RLS policy or by server code that verifies the token. If your admin page loads data with a query that RLS would allow for any signed-in user, hiding the button changed nothing.

4. The policy reads the wrong claim

Your hook sets a top-level user_role. A policy that checks auth.jwt() -> 'user_metadata' ->> 'role' instead reads a value users can change themselves with supabase.auth.updateUser(). Search your policies:

select tablename, policyname, qual, with_check from pg_policies
where qual ilike '%user_metadata%' or with_check ilike '%user_metadata%';

Anything that comes back for a role or permission check needs fixing. Supabase's guide wraps the check in an authorize() function that is security definer with set search_path = '' and calls it as (select authorize('messages.delete')) in the policy.

5. Removing a role isn't instant

The claim is written when the token is issued. Delete someone's admin row and they keep the claim until their access token is refreshed or expires. For most apps that's acceptable if access tokens are short-lived. For something like refunds or deleting other users' data, check user_roles directly in that one policy or server route instead of only the claim.

A 3-minute test with two accounts

What an outside scan adds, and what it can't do

Abbi Labs can't see your hook function or your policies, so the SQL above is on you. Paste policies into the free Supabase RLS policy checker (runs in your browser). The scan checks what your live site exposes: Supabase tables readable with your public anon key (tables your site's JavaScript references plus a short list of common names like profiles and users), secret keys in the JavaScript, exposed .env/.git files, source maps and security headers.

Scan your app free

FAQ

What is the Supabase custom access token hook?

A Postgres (or HTTP) function that Supabase Auth runs before it issues an access token. It receives the user_id and the claims and can add your own, for example a user_role claim read from a user_roles table. Your RLS policies can then read it with auth.jwt() ->> 'user_role'. You turn it on in the dashboard under Authentication, Hooks.

Who should be able to read and write the user_roles table?

Only the auth hook (the supabase_auth_admin role) and your own server code. Supabase's RBAC guide revokes all on user_roles from authenticated, anon and public and adds a single select policy for supabase_auth_admin. If a signed-in user can insert or update a row there, they can make themselves admin on their next token refresh.

Can anyone call my custom_access_token_hook function?

Functions in the public schema can be called through the API at /rest/v1/rpc/<name> unless you revoke execute. Supabase's guide grants execute to supabase_auth_admin and revokes it from authenticated, anon and public. If you skipped that, a caller could pass any user_id and read back that user's role.

Can I trust the role I decode from the token in the browser?

Only for showing or hiding UI. Decoding a JWT with a package like jwt-decode doesn't verify it, and anything in the browser can be changed by the user. Real protection has to be in RLS policies or in server code that verifies the token.

Why didn't removing a user's admin role take effect?

The claim is baked into the access token when it's issued. It only changes when the token is refreshed or the user signs in again, so a removed admin keeps the claim until their current token expires. Keep access tokens short-lived and, for sensitive actions, check the roles table directly instead of only the claim.

Does Abbi Labs check my auth hook?

Not directly. It can't see your database functions or policies from the outside. The free quick check looks at what your live site serves and what your public anon key can read: Supabase tables readable with your public key, secret-looking keys in the JavaScript, exposed .env and .git files, source maps and security headers. For policies themselves, paste them into the free in-browser Supabase policy checker.

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.