Supabase view returns every row even though RLS is on
Updated
By default a view runs with the permissions of its owner, which on Supabase is usually postgres, and that role bypasses RLS. So a view over a protected table can expose every row through the API. On Postgres 15 and newer, set security_invoker = true on the view so it applies the table’s policies to whoever queries it.
Check if you have the problem
AI tools like views for dashboards and leaderboards. Request one with the anon key and no sign-in:
curl 'https://<project-ref>.supabase.co/rest/v1/user_stats?select=*' \
-H "apikey: YOUR_ANON_KEY"
If rows come back that the same request to the underlying table doesn’t return, the view is leaking. The Security Advisor in the Supabase dashboard also flags such views.
The fix
alter view public.user_stats set (security_invoker = true);
For a new view:
create view public.user_stats
with (security_invoker = true)
as select user_id, count(*) as orders
from public.orders
group by user_id;
On Postgres older than 15, revoke access from anon and authenticated or move the view to a schema the API doesn’t expose.
Check that it worked
Repeat the request without signing in: empty result. Signed in as a user: only that user’s rows.