Search Psychevus

12 results available.
↑ ↓ to navigate ↵ to open · esc to close

Supabase · PostgreSQL

Never use user_metadata in Supabase RLS policies

Any signed-in user can edit their own user_metadata from the browser. A policy that reads a role or tenant from it lets users make themselves admin. Use app_metadata or a table instead.

Short answerBecause users can change it themselves. supabase.auth.updateUser({ data: { role: 'admin' } }) works from any browser session and writes to user_metadata, which then appears in the user's JWT. A policy like (auth.jwt() -> 'user_metadata' ->> 'role') = 'admin' therefore lets anyone become an admin. Store authorization data in app_metadata (only the service role can set it) or in a table the user can't write.

Two kinds of metadata

  • user_metadata (raw_user_meta_data in auth.users): profile data the user controls, such as a display name. Writable by the signed-in user.
  • app_metadata (raw_app_meta_data): data your server controls, such as a plan or role. Only writable with the service role.
The escalation, from the browser console
await supabase.auth.updateUser({ data: { role: "admin", org_id: "<someone else's org>" } });
await supabase.auth.refreshSession();   // the new claims are now in the JWT

Policies to replace

Before → after
-- before: user-controlled
using ((auth.jwt() -> 'user_metadata' ->> 'role') = 'admin')

-- after: server-controlled claim
using ((auth.jwt() -> 'app_metadata' ->> 'role') = 'admin')

-- or: a table only your server writes
using (exists (select 1 from private.admins a where a.user_id = (select auth.uid())))

Set app_metadata from server code with the service role (auth.admin.updateUserById), or add claims with Supabase's custom access token hook. Remember that claims in a JWT only change when the token is refreshed, so for permissions that must take effect immediately, a table lookup is the safer source.

Check it yourself

Search your migrations for user_metadata and raw_user_meta_data. The RLS simulator flags any policy that reads them as critical.

Simulate your RLS policies (free, in your browser)

Questions

Is it fine to read user_metadata in the UI?

Yes, for display: a name or avatar. Just never base an access decision on it, in a policy, an Edge Function or a server action.

What about the email in the JWT?

Comparing auth.jwt() ->> 'email' is safer than metadata, but emails can change and may not be verified. Prefer the user id.

Updated 18 November 2026 · by

Reading about a flaw is the easy part. A Launch Check finds every one of these in your app, with evidence and a fix prompt for each, in two business days.

Get a fixed quote

Related

YOUR VISIT, YOUR CHOICE

Privacy, by intention.

You decide what stays on your device. You can change your choice at any time.

Essential

Saves your privacy choice. Administrator sign-in uses a secure session cookie.

Always on
Full privacy notice ↗