🔧 Unbreak My AppAll fixesError codesFix KitsSponsor

Fixes › Database / Supabase RLS

Any user can make themselves admin or Pro: Supabase RLS lets users update their own role, plan or is_admin column

LovableBoltCursorReplitv0

Published

Weakness type: CWE-269 (Improper Privilege Management)

The error

A user changed their own role to 'admin', is_admin to true or plan to 'pro' from the browser console / supabase.from('profiles').update({ role: 'admin' }).eq('id', user.id) succeeds for a normal user / a security review says users can escalate privileges through the profiles table / free users unlocked paid features without paying

Why it happens

Row Level Security decides which rows a user can touch, not which columns. A typical AI-generated policy such as create policy "Users can update own profile" on profiles for update using ((select auth.uid()) = id) is correct for the row, but it lets the owner change every column in that row. If the same profiles table also holds role, is_admin, plan, credits or subscription_status, anyone who signs up can open the browser console, take your public anon or publishable key (it is meant to be public) and run update({ role: 'admin' }) or update({ plan: 'pro' }) on their own row. The policy passes, because it is their row. Supabase's Column Level Security guide shows exactly this: an owner UPDATE policy gives the post owner full access to update the row, including all of the columns, and by default the authenticated role has a table-level UPDATE privilege on every column. A WITH CHECK clause does not save you on its own either: Postgres checks WITH CHECK against the new row only, so it cannot ask 'did this column change?'. The same trap exists outside tables: Supabase warns that raw_user_meta_data can be updated by the user with supabase.auth.updateUser() and is not a good place to store authorization data, while raw_app_meta_data cannot be changed by the user.

How to fix it

  1. Test it first, as a normal signed-in user in the browser console of your live app: await supabase.from('profiles').update({ role: 'admin' }).eq('id', (await supabase.auth.getUser()).data.user.id).select() (use your real table and column names, such as is_admin, plan or credits). If the row comes back with the new value, any user can do it. Change it back afterwards.
  2. List every column that grants power or money: role, is_admin, plan, tier, credits, subscription_status, stripe_customer_id, trial_ends_at, approved, verified. These must only ever be written by your server (service role), a webhook or an admin, never by the user's own browser session.
  3. Best fix, recommended by Supabase: move roles and plans out of the user-editable table. Create a separate table such as public.user_roles (user_id, role) or public.subscriptions, revoke all on it from anon and authenticated, and let only your server, Stripe webhook or a Custom Access Token Auth Hook write it. Users can still edit their name and avatar in profiles.
  4. Quick fix if the columns must stay in profiles: use column-level privileges. revoke update on table public.profiles from anon, authenticated; then grant update (display_name, avatar_url, bio) on table public.profiles to authenticated; listing only the columns users may edit. Postgres warns that revoking one column while the table-level grant remains does nothing, so revoke the table-level UPDATE first. Do the same for INSERT if the browser inserts profile rows: revoke insert, then grant insert (id, display_name, avatar_url).
  5. After the grant change, check your app code. Restricted roles must name their columns: an update or upsert that sends the whole profile object, including role or plan, will now fail with a permission denied error. Send only the editable fields. Only restrict UPDATE and INSERT here: Supabase warns that column restrictions can break select * on the table, so do not revoke SELECT on columns unless you need to hide them.
  6. Alternative or extra guard, a trigger: a BEFORE UPDATE trigger can compare OLD and NEW, which a policy cannot. For example: create function public.protect_profile_columns() returns trigger language plpgsql as $$ begin if current_user in ('anon','authenticated') and (new.role is distinct from old.role or new.plan is distinct from old.plan) then raise exception 'role and plan can only be changed by the server'; end if; return new; end; $$; then create trigger protect_profile_columns before update on public.profiles for each row execute function public.protect_profile_columns(); Your service role key and the SQL editor still work.
  7. Create profile rows server-side with a trigger on auth.users (Supabase's handle_new_user pattern) that sets role and plan to safe defaults, so a user cannot insert their own row with role 'admin' either. If you keep a browser INSERT, add with check (role = 'user' and plan = 'free') to the insert policy.
  8. Never authorise from user_metadata. Policies or code that read auth.jwt() -> 'user_metadata' -> 'role' can be changed by the user with supabase.auth.updateUser(). Use raw_app_meta_data, a roles table, or a custom claim added by the Custom Access Token Auth Hook.
  9. Re-run the console test from step 1. It should now fail with an error, while editing the display name still works. Then check who already has role 'admin' or a paid plan without a matching Stripe payment, and fix those rows from the server.

Fix prompt (paste into Lovable, Bolt, Cursor, Replit or v0)

Security fix: any signed-in user can update their own role, is_admin, plan or credits column in my Supabase table <TABLE, e.g. profiles> because the UPDATE policy only checks (select auth.uid()) = id. Do not disable RLS and do not use the service_role key in the browser. 1) List every table and column in my migrations that controls permissions or billing (role, is_admin, plan, credits, subscription_status and similar) and every place the frontend writes to those tables. 2) Write a migration that runs revoke update on table public.<TABLE> from anon, authenticated; and grant update (<only the user-editable columns>) on table public.<TABLE> to authenticated; and the same for insert if the browser inserts rows. 3) Add a BEFORE UPDATE trigger that raises an exception when current_user is anon or authenticated and a protected column changes. 4) Make sure profile rows are created by a trigger on auth.users with safe defaults. 5) Change the frontend so updates only send editable fields. 6) Move any role checks that read user_metadata to app_metadata or a roles table written only by the server. Explain each change in one line and give me a console test that proves a normal user can no longer change their role.

Error codes this fixes

FAQ

Why can a user change their own role if RLS is on?

Because RLS works on rows, not columns. An UPDATE policy using ((select auth.uid()) = id) lets the owner change any column in their own row, including role or plan, and the authenticated role has a table-level UPDATE privilege on all columns by default.

Can WITH CHECK stop a user changing their role?

Not by itself in a simple way. Postgres checks WITH CHECK against the new row only, so it can block role = 'admin' but cannot tell whether the value changed or who is allowed to change it. Use column-level grants, a separate roles table, or a BEFORE UPDATE trigger that compares OLD and NEW.

How do I stop users updating one column in Supabase?

Revoke the table-level UPDATE privilege from authenticated and grant UPDATE only on the columns users may edit, for example revoke update on table public.profiles from authenticated; grant update (display_name, avatar_url) on table public.profiles to authenticated;. Revoking a single column while the table-level grant remains has no effect.

Where should I store user roles in Supabase?

Supabase recommends RLS plus a dedicated table for user roles, such as public.user_roles, that users cannot write, optionally exposed as a JWT claim through a Custom Access Token Auth Hook. raw_app_meta_data is also safe because users cannot change it, unlike raw_user_meta_data.

Will my server and Stripe webhook still be able to set the plan?

Yes. Column grants and the trigger only restrict the anon and authenticated roles. Code using the service role key on the server, and the SQL editor, can still update protected columns.

Sources

Based on the official documentation:

Related fixes

Want the full checklist for this area? Get a Fix Kit ($29)

Get one new fix a week

Free newsletter: the newest breakages in Lovable, Bolt, Cursor, Replit and v0 apps, with paste-in fixes.

We only use your email to send the newsletter. Unsubscribe any time. See privacy.