Supabase views bypass RLS: fix them with security_invoker
You turned on RLS, wrote a policy so people only see their own orders, and tested it. Then your AI tool added a handy order_summaries view for the dashboard, and that view hands every customer's orders to anyone with your public key. In Supabase, a view in the public schema skips RLS by default.
postgres user" (Row Level Security docs). Postgres runs a normal view with its owner's permissions, and on Supabase that owner can bypass RLS. Fix: on Postgres 15+, set security_invoker = true on every view the API can reach, give the calling role only the grants it needs, never expose auth.users through a view, and keep materialized views away from anon and authenticated.Here's the kind of migration AI tools write all the time:
-- orders has RLS: users can only select their own rows
create view public.order_summaries as
select o.id, o.user_id, o.total, o.status, p.email
from public.orders o
join public.profiles p on p.id = o.user_id;
Because the view lives in public, the Data API exposes it like a table. A logged-out visitor can call /rest/v1/order_summaries with your anon key, and since the view checks the underlying tables as its owner, the orders policy never filters anything. Every order and email comes back. Supabase's Security Advisor flags exactly this as an error: check 0010, "View bypasses row-level security" (Advisors docs).
Step 1: Find every view the API can reach
Open Advisors → Security Advisor in the Supabase dashboard and look for security_definer_view, auth_users_exposed and materialized_view_in_api. To see everything yourself, run this in the SQL Editor:
select n.nspname as schema,
c.relname as name,
case c.relkind when 'v' then 'view' else 'materialized view' end as kind,
c.reloptions as options
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind in ('v', 'm')
and n.nspname = 'public' -- add any other schema your API exposes
order by kind, name;
A normal view is safe from this problem only if options includes security_invoker=true (or =on). An empty options means the view runs as its owner. Every materialized view in the list needs step 5.
Step 2: Switch normal views to security_invoker
Check your Postgres version with select version();. On Postgres 15 and newer, fix existing views in place:
alter view public.order_summaries set (security_invoker = true);
And write new views (and every create or replace view in later migrations) with the option built in, so a future edit can't quietly drop it:
create or replace view public.order_summaries
with (security_invoker = true)
as
select o.id, o.user_id, o.total, o.status
from public.orders o;
Per the PostgreSQL docs, with security_invoker the underlying tables are checked with the permissions and RLS policies of the person running the query, as if they had queried the tables directly. On Postgres versions older than 15, Supabase recommends revoking access to the view from anon and authenticated or moving it to a schema the API doesn't expose.
Step 3: Fix the grants and policies the view now needs
Once a view runs as the caller, the caller needs its own SELECT grant on every table the view reads, and those tables' RLS policies decide which rows come back. If the dashboard now shows "permission denied for table" (42501), grant just that table and operation to the role that should have it:
alter table public.orders enable row level security;
grant select on public.orders to authenticated;
create policy "Users read their own orders"
on public.orders for select
to authenticated
using ( (select auth.uid()) = user_id );
Don't fix it with grant all on all tables; that's how private tables end up writable. Our 42501 guide walks through per-table grants. Simple views can also accept inserts, updates and deletes; with security_invoker those writes are checked against the caller's own privileges and policies too.
Step 4: Remove views over auth.users
A view in public that selects from auth.users (often added to "show the user's email") can expose every account's email and sign-in details. Supabase flags it as error 0002, "User data exposed through a view". Drop the view and use a public.profiles table that holds only the fields your app needs, filled by a trigger on new signups and protected by RLS. Keep emails out of any table that's readable by everyone (see RLS filters rows, not columns).
Step 5: Lock down materialized views
Materialized views can't use RLS and can't be set to security_invoker, so if the API roles can select from one, every row is public. Revoke access and read them only from server code:
revoke select on public.daily_stats from public, anon, authenticated;
-- should return false
select pg_catalog.has_table_privilege('anon', 'public.daily_stats', 'select');
Step 6: Test with the anon key and two users
Test the view the way a stranger would, with only your public anon key:
curl -s "https://YOUR-PROJECT.supabase.co/rest/v1/order_summaries?select=*&limit=5" \
-H "apikey: YOUR_PUBLIC_ANON_KEY" \
-H "Authorization: Bearer YOUR_PUBLIC_ANON_KEY"
# expected: [] or a permission error, never real rows
Then sign in as two test users and confirm user B gets only B's rows from every view. Re-run Security Advisor and these tests after each AI edit that touches SQL, because a regenerated view can lose the setting.
Copy-paste prompt for your AI coding tool
Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:
Audit the Postgres views in my Supabase project. 1) List every view and materialized view in the public schema (and any other schema exposed by the Data API), what tables it reads, and whether it has security_invoker = true. 2) For every normal view, write a migration that runs alter view ... set (security_invoker = true), and make sure every create or replace view in my migrations includes with (security_invoker = true). 3) For each table those views read, confirm RLS is enabled and that anon/authenticated have only the specific grants and policies the app needs; add narrow grants (no GRANT ALL) where the view now returns permission denied. 4) If any public view selects from auth.users, replace it with a public.profiles table that holds only the fields the app needs, filled by a trigger, with RLS. 5) For materialized views, revoke select from public, anon and authenticated or move them to a private schema, and read them only from server code. 6) Give me curl commands using only the anon key to confirm each view returns no other users' rows, and a test with two signed-in users. Do not use the service_role key in browser code.
Check your live site for exposed keys in about a minute
Enter your app's URL on KeySnare for a free check of the page and its same-site JavaScript for a leaked service_role key and other secret keys, or paste a public GitHub repo for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.
Run a free scan →FAQ
Do Supabase views respect Row Level Security?
Not by default. Supabase's docs say views bypass RLS by default because they are usually created by the postgres role and Postgres runs them with the view owner's permissions. On Postgres 15 and newer, set security_invoker = true so the view uses the RLS policies and grants of the person calling it.
What does security_invoker = true do?
It makes Postgres check the underlying tables with the permissions and RLS policies of the user running the query instead of the view owner, as if they had queried the tables directly. It is available on Postgres 15 and newer, and you can add it to an existing view with ALTER VIEW ... SET (security_invoker = true).
Why did my app break with "permission denied" after I set security_invoker?
Because the caller now needs its own SELECT grant on every table the view reads. Grant only what that role needs on that table (for example grant select on public.orders to authenticated) and keep RLS on. Do not fix it with GRANT ALL on all tables.
Can I put RLS on a materialized view?
No. Materialized views cannot use RLS and cannot be set to security_invoker, so if anon or authenticated can select from one, they see every row. Revoke select from public, anon and authenticated or move it to a schema the API does not expose.
Is a security definer view ever OK?
Sometimes, for data that is meant to be public, like a leaderboard of display names and scores. Then the view shows every row it selects to anyone with your public key, so select only public columns. Many teams prefer security_invoker plus an explicit public select policy so the rule lives in one place.
What is the Supabase "security_definer_view" warning?
It is Security Advisor check 0010. It flags a view in an exposed schema that runs with its creator's permissions and ignores RLS. The documented fix is to recreate or alter the view with security_invoker = on.
Does KeySnare check my Supabase views?
No. KeySnare doesn't connect to your database, so it can't see your views, grants or policies. The free live-website check finds a Supabase service_role key and other secret keys in your site's JavaScript, and the GitHub repo scan flags tables created without RLS in SQL migrations. Run the checks in this guide for views.