Short answer
If the only thing that separates admins from users is an if statement in your React code, every signed-in user can call the same API the admin page uses, and often promote themselves by editing a role column on their own profile. Store roles where users cannot write them, check them in RLS policies through a security definer helper, and let the frontend use the role only to decide what to show.
Ask an AI about this page:ChatGPTClaude
On this page
An admin check that only lives in the frontend decides what your app shows, not what the database returns. Any signed-in user can call the same API the admin page calls, and if the role sits in a column users can edit, they can make themselves admin in one request. It is rated critical because the admin page usually reads every user’s personal data, and the person exploiting it only needs a free account.
Why it matters
A typical generated admin area looks like this: the React page checks a role and redirects everyone else.
const { data: profile } = useProfile();
if (profile?.role !== "admin") return <Navigate to="/" replace />;
const { data: users } = await supabase
.from("profiles")
.select("id, email, full_name, role");For that query to work for the admin, the database has to let it through, and the simplest policy that does is one for every signed-in user:
create table public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
email text not null,
full_name text,
role text not null default 'user'
);
alter table public.profiles enable row level security;
create policy "Signed-in users can read profiles"
on public.profiles for select
to authenticated
using (true);
create policy "Users can update their own profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = id);Two holes follow. Every signed-in user can read every email address, because the redirect is the only thing between them and the data. And the update policy lets users change any column of their own row, including role. RLS restricts rows, not columns. One request setting role to admin turns any user into an admin everywhere your code trusts that column. OWASP lists this as elevation of privilege under broken access control; the root cause is client-side enforcement of server-side security (CWE-602 (opens in a new tab)).
How to find it in your app
Search your frontend for role checks (role ===, isAdmin, is_admin) and note what data each admin screen loads. Then list the role-like columns that signed-in users are allowed to update:
select table_name, column_name
from information_schema.column_privileges
where table_schema = 'public'
and grantee = 'authenticated'
and privilege_type = 'UPDATE'
and column_name ~* 'role|admin|staff|permission'
order by 1, 2;A hit matters when an update policy lets the user reach their own row, which is the normal case for a profile. Also open the Security Advisor: lint 0015 flags policies that rely on user_metadata, which users can change themselves.
How to fix it
Move the role into a table that users cannot write, check it inside the policies, and lock the role column. In a new migration:
create schema if not exists private;
create table public.user_roles (
user_id uuid primary key references auth.users (id) on delete cascade,
role text not null check (role in ('admin'))
);
alter table public.user_roles enable row level security;
revoke all on table public.user_roles from anon, authenticated;
grant select on table public.user_roles to authenticated;
create policy "Users can read their own role"
on public.user_roles for select
to authenticated
using ((select auth.uid()) = user_id);
create or replace function private.is_admin()
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1 from public.user_roles r
where r.user_id = (select auth.uid()) and r.role = 'admin'
);
$$;
drop policy "Signed-in users can read profiles" on public.profiles;
create policy "Users read their own profile, admins read all"
on public.profiles for select
to authenticated
using ((select auth.uid()) = id or (select private.is_admin()));
revoke update on table public.profiles from authenticated;
grant update (full_name) on table public.profiles to authenticated;user_roles has no insert, update or delete grant for the API roles, so only you (SQL Editor) or your server can assign roles. private.is_admin() runs as a security definer so it can read the roles table inside a policy, with an empty search_path and in a schema the Data API does not expose. The column grant leaves users able to edit their name and nothing else. Then make yourself admin from the SQL Editor:
insert into public.user_roles (user_id, role)
values ('11111111-1111-1111-1111-111111111111', 'admin');Stop reading profiles.role anywhere. The frontend may still read the caller’s own row in user_roles to decide whether to show the admin menu; that is fine, because the database now decides what the menu can load. Admin actions that need the secret key, like deleting accounts, belong in an edge function that checks the role first, never in the browser (see service_role key in the frontend). The roles and permissions guide covers more roles than one.
The admin area is protected only by a role check in React, and the role is stored in public.profiles.role, which users can update on their own row. Add a new Supabase migration that: creates public.user_roles (user_id primary key referencing auth.users, role text limited to ‘admin’) with RLS enabled, no insert, update or delete grants for anon or authenticated, and a select policy so users can read only their own row; creates private.is_admin() as a security definer SQL function with set search_path = ‘’ that checks user_roles for (select auth.uid()); replaces the select policy on public.profiles so users read their own row and admins read all rows; revokes table-level update on public.profiles from authenticated and grants update only on full_name.
Then change the frontend to read the role from user_roles instead of profiles.role. Do not move any admin query to the service role key and do not copy existing profiles.role values into user_roles; I will add admins by hand.
Check that the fix works
Act as a normal user and try both holes: reading everyone’s profile and promoting yourself:
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
select count(*) as profiles_visible from public.profiles;
update public.profiles set role = 'admin'
where id = '22222222-2222-2222-2222-222222222222';
rollback;With a real non-admin user ID in both places, the count must be 1 and the update must fail with permission denied for table profiles. Run the same block with your admin’s ID up to the select: the count must be the number of all profiles. Finally, open the admin page while signed in as a normal user and watch the network tab: requests to profiles may return only that user’s own row.
Frequently asked questions
Isn’t a protected route in React enough if users can’t see the admin page?
No. The route only decides what your app renders. The data behind it comes from the Data API, which anyone can call directly with your public key and their own session. The database has to refuse non-admins.
Can I store the admin role in user_metadata?
No. Supabase lets every signed-in user change their own user_metadata, so a role stored there can be self-assigned. Use app_metadata, which only a server with the secret key can write, or a roles table without client write access.
How do I make the first admin?
Insert a row into the roles table from the SQL Editor or a server script, as shown on this page. Never build an API path that lets a user assign roles unless that path itself checks that the caller is already an admin.
What about admin actions that need the secret key, like deleting users?
Run them in an edge function. The function verifies the caller’s session, checks the role on the server and only then uses the secret key. The key itself never goes to the browser.
Sources
- 1.Row Level Security — Supabase Docs
- 2.Custom Claims & Role-based Access Control (RBAC) — Supabase Docs
- 3.Managing user data — Supabase Docs
- 4.Privileges — PostgreSQL Documentation
- 5.CWE-602: Client-Side Enforcement of Server-Side Security — MITRE
- 6.A01 Broken Access Control — OWASP Top 10