Your first check is free. No card needed.Run a free security check

Can a user make themselves admin? How roles break in Supabase and Firebase apps

Yes, if the role lives in a field the user is allowed to write. The usual cause is a profiles table with a role, is_admin or plan column and an update policy that only checks that the row is the user's own, or Firebase rules that let a user write their whole user document. Keep roles where users cannot write them (a column they have no update privilege on, a separate roles table, Supabase app_metadata or Firebase custom claims), check them on the server, and confirm with a test account that changing your own role fails.

Imagine your app has a profiles table with display_name, avatar_url and role. A curious user opens the developer tools, copies the request your settings page sends when they rename themselves, adds "role": "admin" to the body and sends it again. The response is a success. They reload, and the admin menu appears.

Nothing in the database misbehaved. The policy said "users can update their own row", and the role is part of that row.

Why does "users can update their own row" include the role?

This is the policy most tutorials and AI coding tools produce for a profiles table:

create policy "Users can update own profile"
on public.profiles for update
to authenticated
using (auth.uid() = id);

Row level security decides which rows a request can touch. It says nothing about columns. Supabase's documentation notes that by default the authenticated role holds a table-level UPDATE privilege, "which means that the authenticated role can update all the columns" (column level security).

A with check does not save you either. PostgreSQL evaluates it "against the proposed new contents of the row, not the original contents", and when only using is given, that same expression is reused as the check (PostgreSQL docs on CREATE POLICY). The new row still has id = auth.uid(), so the update with role = 'admin' passes.

Firebase has the same shape. A rule like allow write: if request.auth.uid == userId on users/{userId} lets the owner write every field. Firebase's own docs show admin checks that read a user document, such as get(/databases/$(database)/documents/users/$(request.auth.uid)).data.admin == true (rule conditions). That check is only as strong as the rule that stops users writing admin on their own document.

The same applies to plan, credits, is_verified, tier and org_id. OWASP calls this mass assignment: a request sets a field the form never showed, such as isAdmin=true (OWASP cheat sheet).

What does this look like in a real app?

On August 3, 2025, between about 2 AM and 8 AM EST, the drawing app DrawAFish had every username changed to offensive text. The developer's postmortem, published the next day, lists the causes plainly. "The username modification did not perform any auth logic whatsoever." Admin actions used a sign-in token but "never confirmed that the JWT token belonged to the userId / email" in the request, and an old admin password was still active.

He calls it a blameful postmortem: "And I blame me. (Not the LLM, sorry)." A user named iceweaselfan44 used the same token flaw to remove the worst drawings, then worked with him on the fixes. The lesson for roles: the server has to decide who the caller is and what they may change, every time.

How do I stop users changing their role in Supabase?

Pick one of these. We would start with the first or second, and add the trigger as a backstop.

Take the column away with privileges

revoke update on table public.profiles from authenticated;
grant update (display_name, avatar_url) on table public.profiles to authenticated;

Now an update that includes role fails with a permission error, even though the row policy allows it. Supabase calls column privileges an advanced feature and does not recommend them for most projects; every new safe column has to be added to the grant (column level security).

Move roles to their own table

Supabase's role-based access guide keeps roles in a user_roles table, runs revoke all on table public.user_roles from authenticated, anon, public, and adds the role to the token with a custom access token hook. A simpler version that reads the table in policies:

create table public.user_roles (
  user_id uuid primary key references auth.users on delete cascade,
  role text not null check (role in ('member', 'admin'))
);
alter table public.user_roles enable row level security;
revoke all on table public.user_roles from anon, authenticated;

create schema if not exists private;
create function private.is_admin() returns boolean
language sql stable security definer set search_path = ''
as $$ select exists (
  select 1 from public.user_roles
  where user_id = auth.uid() and role = 'admin'
) $$;
grant usage on schema private to authenticated;

Then policies on admin-only tables use using ((select private.is_admin())). The function sits outside public on purpose: Supabase warns that a security definer function in an exposed schema "is callable over the Data API with the creator's privileges" (row level security). Only your server, using the secret key, or the SQL editor writes to user_roles.

Add a trigger as a backstop

create function public.block_role_change() returns trigger
language plpgsql as $$
begin
  if new.role is distinct from old.role
     and current_user in ('anon', 'authenticated') then
    raise exception 'role can only be changed by the server';
  end if;
  return new;
end $$;

create trigger profiles_block_role_change
before update on public.profiles
for each row execute function public.block_role_change();

This catches the change even if someone later adds a broad grant back.

Is user_metadata safe for roles?

No. Supabase is direct about it: "Users can edit their own user metadata, so a role or tenant ID stored there lets a user grant themselves access" (managing user data). Any signed-in user can call supabase.auth.updateUser({ data: { role: 'admin' } }).

-- Don't: the user controls this value
using ((select auth.jwt() -> 'user_metadata' ->> 'role') = 'admin')

-- Do: app_metadata can't be changed by the user
using ((select auth.jwt() -> 'app_metadata' ->> 'role') = 'admin')

Set app_metadata from a trusted server with your secret key. Search your code for user_metadata and raw_user_meta_data in policies, functions and route handlers.

How do I do the same in Firebase?

Pin the fields a user may change. Firebase recommends hasOnly() because new fields are then refused by default (control access to specific fields):

match /users/{userId} {
  allow read: if request.auth != null && request.auth.uid == userId;
  allow create: if request.auth != null && request.auth.uid == userId
    && request.resource.data.keys().hasOnly(['displayName', 'photoURL']);
  allow update: if request.auth != null && request.auth.uid == userId
    && request.resource.data.diff(resource.data).affectedKeys()
         .hasOnly(['displayName', 'photoURL']);
}

For real roles, use custom claims. They "should only be set from a privileged server environment by the Firebase Admin SDK", for example getAuth().setCustomUserClaims(uid, { admin: true }), and they are meant for access control, not profile data (custom claims). Rules then check request.auth.token.admin == true (rules and Auth).

Isn't hiding the admin page enough?

No. A React check like {profile.role === 'admin' && <AdminLink />} hides a link; it does not stop the request behind it. Every admin route, edge function and server action has to work out the caller from a verified token, look up their role in a place they cannot write, and refuse otherwise. Never take the role, or the user id, from the request body. Our broken access control guide covers the wider version of this test.

Try it on your own app with two test accounts

Use a staging copy or your own app with test accounts: A is an ordinary user, B is an admin.

  1. Find the fields worth testing. In the Supabase SQL editor, run select has_column_privilege('authenticated', 'public.profiles', 'role', 'UPDATE');. PostgreSQL counts both table-wide and column grants (system functions), so true means users can write it.
  2. Signed in as A, change your display name with the developer tools open. In the Network tab, right-click that request and choose Copy as fetch.
  3. Paste it into the Console, add "role": "admin" (and plan, is_admin, credits if you have them) to the body, and run it. Expect a permission error from Supabase or PERMISSION_DENIED from Firestore.
  4. Reload and confirm the role did not change, even if the response looked like a success. Then try the same edit through user_metadata and check that no admin access follows.
  5. As A, open /admin directly and replay one request copied from B's admin session. Expect the server to refuse it. A hidden page alone is not a pass.

A pass covers that one table and those fields. Repeat it for every table with a field like role or plan, and for your Supabase policies or Firebase rules generally.

Where does Saqr fit?

If you would rather not repeat this by hand on every release, a Saqr Full task signs in with a test login you provide and tries fields a user should not control, such as role or price, plus admin pages an ordinary user can reach. Every finding shows the request and response behind it, with a fix prompt for your AI coding tool. Your first check is free, no card needed.

Saqr (usesaqr.com) is AI security for web apps. Its AI agents attack your live app from the outside, the way a real attacker would, and show you exactly what to fix. From $19 a month, cancel any time. Tests only apps you have shown are yours.

Questions

Is it fine to keep the role in profiles if the form never shows it?

No. The form is not the boundary; the database grant and policy are. Anyone can send their own request with extra fields, so either remove the update privilege on that column or move the role to a table users cannot write.

Why does my role change not show up right away after I set app_metadata or a custom claim?

The role is copied into the sign-in token, and a token is not refreshed on every request. Supabase warns that a JWT is not always up to date, and Firebase says new claims reach the ID token the next time one is issued, which the client can force with getIdToken(true).

Do plan and credit columns need the same protection?

Yes. Anything that unlocks paid features should be written only by your server, usually from a verified payment webhook. Our Stripe article covers the payment side of this.

Can my app use the service role key to set roles?

Only from server code such as an edge function or API route, and only after checking that the caller is an admin. The secret key and the legacy service_role key bypass row level security, so they must never reach the browser.

Are security definer functions a risk here?

They can be. A security definer function runs with its creator's privileges, so a callable function like set_role must check who is calling. Keep such functions out of exposed schemas, as Supabase advises.