Mendlark / Guides

Supabase view bypasses RLS: how to lock it down

You turned on row level security (RLS), wrote policies, and your tables look safe. Then you added a view, for example a public_profiles view so users can see each other's names. By default that view can skip your RLS policies completely. If the public anon role can also write through it, anyone with your anon key (which ships in your frontend by design) can read, edit or delete rows through the view. This week a widely shared incident wiped tens of thousands of user profiles exactly this way. Here is how to check your own project in about ten minutes.

Stuck on this? Get it fixed for €49.

Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.

Get it fixed for €49

Why a view can bypass RLS

In Postgres, a view normally runs with the permissions of the role that owns it, not the person querying it. Views you create in the Supabase SQL editor are usually owned by postgres, which bypasses RLS. So the view sees every row, and your policies on the underlying table never get a say. On top of that, simple views (one table, no grouping) are automatically updatable, so UPDATE and DELETE through the view hit the real table.

1. List the views in your public schema

Run this in the Supabase SQL editor:

select c.relname as view_name, c.reloptions
from pg_class c
where c.relkind = 'v'
  and c.relnamespace = 'public'::regnamespace;

Any view whose reloptions does not include security_invoker=true runs with its owner's rights. Supabase's Security Advisor (in the dashboard under Advisors) flags these as Security Definer View. Treat every one of those warnings as urgent.

2. Make the view respect RLS

Supabase runs Postgres 15 or later, which supports security_invoker. With it on, the view runs as the person querying it, so the table's RLS policies apply:

alter view public.public_profiles set (security_invoker = true);

For new views, set it when you create them:

create view public.public_profiles
with (security_invoker = true) as
  select id, display_name, avatar_url from public.profiles;

Heads up: if the view was there because users couldn't see each other's rows, it may now return less. That's correct. Add a narrow select policy on the table for the columns you meant to share, or keep the shared columns in a separate table with its own policy.

3. Take write access away from the view

A read-only view should be read-only for everyone outside your server:

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

While you're there, check what anon can do on your real tables. Signed-out visitors rarely need delete on anything:

select table_name, privilege_type
from information_schema.role_table_grants
where grantee = 'anon' and table_schema = 'public'
order by table_name;

4. Tighten the table policies too

Make sure every table in public has RLS enabled, and that update and delete policies check ownership, for example:

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

A policy like using (true) on delete or update means anyone, which is almost never what you want.

5. Test it like an attacker would

Use only the anon key, the same one in your frontend, and try to read the view while signed out:

curl 'https://YOUR-PROJECT.supabase.co/rest/v1/public_profiles?select=*' \
  -H "apikey: YOUR_ANON_KEY"

If you get back data a signed-out visitor shouldn't see, it's still open. Test writes on a branch or a copy of the project, never on live data. Then create two test accounts and confirm one can't edit or delete the other's rows.

6. Have a way back

Check you have backups you can actually restore. Supabase paid plans include daily backups, and point-in-time recovery is available as an add-on. Know where the restore button is before you need it.

Quick checklist

  • Every view in public has security_invoker = true
  • No Security Definer View warnings left in Security Advisor
  • insert, update and delete revoked on views from anon and authenticated
  • RLS on for every table, with ownership checks on update and delete
  • Tested signed out with only the anon key
  • Backups confirmed and restorable

Want a second pair of eyes?

Mendlark checks AI-built apps for exactly this kind of hole. Start with a free scan and we'll tell you what's exposed and what a fix costs, or get a fix for €49 (launch price, normally €79) and we'll lock it down, test it and send it as a pull request. Related: Supabase auth redirect loop on Vercel.

Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.