RLS is on, but a user can still change their own credits, plan or is_admin

A signed-in user can PATCH score, credits, plan or is_admin on their own row even though Row Level Security is enabled

What you see

You turned on Row Level Security, added a policy so users can update only their own profile, and the 42501 insert error is gone. The app looks locked down. Then you (or anyone who can sign in) send a request like:

await supabase.from('profiles').update({ is_admin: true, credits: 999999, plan: 'enterprise' }).eq('user_id', user.id);

It succeeds. The same pattern works for score, role, balance or any other privileged column that lives on that row.

Why it happens

Supabase’s column-level security guide puts it plainly: Row Level Security gives you control over who can access rows, but it does not control which columns they can access within those rows. Its own example policy, using ((select auth.uid()) = user_id), “gives the post owner full access to update the row, including all of the columns.”

Postgres CREATE POLICY makes the same split: USING is checked against the existing row, WITH CHECK against the new row. Supabase’s RLS guide says the using clause decides which existing rows can be updated, and with check decides what the resulting row is allowed to look like (for example, that user_id was not reassigned). Neither expression sees “this column changed.” A typical owner policy therefore allows update profiles set credits = 999999 as long as the row still belongs to the signed-in user.

AI-generated apps often put display_name next to is_admin, plan and credits on one profiles table, then add a single update policy. That is enough to open this hole.

Fix 1: grant UPDATE only on the columns users may edit

This is the privilege system Postgres documents for columns. There are two kinds of grant: table-level (every column) and column-level (named columns). If both exist, a table-level UPDATE still wins. The PostgreSQL GRANT docs warn that granting a privilege at the table level and then revoking it for one column “will not do what one might wish: the table-level grant is unaffected by a column-level operation.”

On a new Supabase table, authenticated usually already has table-level UPDATE. Revoke that first, then grant back only the safe columns:

revoke update on table public.profiles from anon, authenticated;

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

A signed-in user can then change display_name. Setting credits, plan or is_admin fails with Postgres error 42501, permission denied for table profiles. The same happens if they smuggle a protected column in the same update as a safe one.

Leave service_role’s table-level UPDATE in place so your webhook or other server code can still write those fields. Keep the secret key off the client; service_role bypasses RLS.

If users can also insert their own profile, revoke table-level INSERT and grant INSERT only on the columns they may set, or they can create a row with is_admin = true. Revoke DELETE if a delete-and-insert would rebuild the row and bypass an update-only protection.

Supabase also warns that a role with restricted select column privileges cannot use select * (or the API equivalent). Restricting only UPDATE does not have that effect.

Fix 2: a WITH CHECK clause, only for conditions on the new row

You can tighten the update policy with with check. This is the right tool for an invariant on the resulting row, such as “the client must not turn is_admin on”:

create policy "Users can update their own profile"
on public.profiles for update
to authenticated
using ( (select auth.uid()) = user_id )
with check (
  (select auth.uid()) = user_id
  and is_admin = false
);

A patch that sets is_admin to true then fails with new row violates row-level security policy (42501). That does not stop credits = 999999 or plan = 'enterprise', because those new values don’t violate the check. A clause such as credits = 0 would also reject a paying user who only edits their name, because WITH CHECK cannot say “leave this column unchanged.” For fields that already hold a legitimate value, use Fix 1, a trigger, or a separate table.

Fix 3: a BEFORE UPDATE trigger that compares OLD and NEW

A trigger function can see OLD and NEW. That is the first place you can reject “this column changed” while still allowing a no-op (credits = credits) and a change to display_name. Keep the function in a schema that is not exposed through the Data API, and only reject client roles so service_role can still grant credits:

create schema if not exists private;

create or replace function private.protect_profile_columns()
returns trigger
language plpgsql
set search_path = ''
as $$
begin
  if current_user in ('anon', 'authenticated') and (
       new.score    is distinct from old.score
    or new.credits  is distinct from old.credits
    or new.plan     is distinct from old.plan
    or new.is_admin is distinct from old.is_admin
  ) then
    raise exception 'score, credits, plan and is_admin are read only'
      using errcode = '42501';
  end if;
  return new;
end;
$$;

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

is distinct from treats a change to or from null as a change. The trigger fires even for service_role (bypassing RLS does not skip triggers), which is why the current_user check is there.

This list of columns lives in code: a new is_pro column is writable until you add it. Treat the trigger as extra defence, not as a reason to skip grants or a separate table.

Fix 4: move privileged fields off the client-writable table

Supabase’s column-level security page recommends this for most projects: use RLS with a dedicated table for roles, rather than column privileges alone.

create table public.profile_entitlements (
  user_id uuid primary key references auth.users (id),
  plan text not null default 'free',
  credits integer not null default 0,
  is_admin boolean not null default false
);

alter table public.profile_entitlements enable row level security;

create policy "Users can view their own entitlements"
on public.profile_entitlements for select
to authenticated
using ( (select auth.uid()) = user_id );

revoke insert, update, delete
  on table public.profile_entitlements
  from anon, authenticated;

Users can read their plan and credits. They cannot write them. Your server updates the table with the secret key (service_role), or through a security definer function that you do not expose to the browser. A definer function created in the Dashboard is owned by postgres, which bypasses RLS, and any role can call it by default. If you use one, set search_path = '', schema-qualify every name, put it in a schema that is not exposed by the Data API, revoke execute from public, anon and authenticated, and check in the body that the caller is allowed to change that row.

Don’t do this

  • Don’t turn RLS off to “simplify” updates. A table in an exposed schema without RLS can be read and written by any role with a grant on it, and the publishable key is in the browser. See the 42501 guide.
  • Don’t put the service_role secret in front-end code so the client can “legitimately” edit credits. It bypasses RLS completely.
  • Don’t only revoke update (credits) while authenticated still has table-level UPDATE. Postgres will ignore the column revoke.

Check it worked

In the SQL editor, test as the authenticated role (the default postgres role is the owner and typically bypasses RLS, so it proves nothing). Sign in as a user and:

  1. Update display_name (or another safe column): it should succeed.
  2. Update credits, plan or is_admin on your own row: it should fail with 42501.
  3. From a server path that uses the secret key, grant a plan or credits: that write should still succeed.

Supabase recommends locking policies in with supabase test db (pgTAP) for each table. Add cases for a protected-column update by the owner, a safe-column update by the owner, and a write by another user.