critical · M3 · MASVS-AUTH-1 · PTKD-BACKEND-SUPABASE-USER-METADATA-POLICY

    Supabase policy trusts user_metadata, which every user can edit

    A Row Level Security policy reads user_metadata (auth.jwt() -> 'user_metadata' or auth.users.raw_user_meta_data). Each signed-in user can set their own user_metadata with one call from the app (supabase.auth.updateUser({ data: { role: 'admin' } })), so a policy that grants access on it grants access to anyone who asks.

    How it is exploited

    The policy says `using ((auth.jwt() -> 'user_metadata' ->> 'role') = 'admin')`. A user opens the app, runs `supabase.auth.updateUser({ data: { role: 'admin' } })` from the console or a proxy, refreshes the session and now passes the admin policy on every row.

    Why it matters

    Any user becomes admin, another tenant or a paying customer, and reads or changes data the policy was meant to protect.

    How to fix it

    1. Move the field to app_metadata: `update auth.users set raw_app_meta_data = raw_app_meta_data || '{"role":"admin"}' where id = ...` from the server only.
    2. Change the policy to `(auth.jwt() -> 'app_metadata' ->> 'role') = 'admin'`, or join a roles table the user cannot write.
    3. Run the Supabase advisor (lint 0015) after the change.

    migration

    drop policy "admins read all" on public.orders;
    create policy "admins read all" on public.orders for select to authenticated
      using ((auth.jwt() -> 'app_metadata' ->> 'role') = 'admin');

    Does your app have this?

    PTKD checks your build or your source project for PTKD-BACKEND-SUPABASE-USER-METADATA-POLICY and every other check in this knowledge base.

    Scan your app

    References

    Related checks (M3)