Guides · VibeVigil
Supabase "View Bypasses Row-Level Security" (security_definer_view): What It Means and How to Fix It
Why a Supabase view can show every user's rows even with RLS turned on, how to find risky views and SECURITY DEFINER functions with the Security Advisor or one SQL query, the one-line fix (security_invoker), and a ready-to-paste prompt for Lovable, Bolt or Cursor.
Not sure your app is safe? Run a free scan first.
VibeVigil checks your live app for exposed keys, Supabase tables a stranger can read, missing security headers, SSL problems and downtime. Every issue comes with a ready-to-paste prompt for your coding agent.
Scan my app for free → (only scan apps you own)
You turned on RLS. You wrote policies. Your tables look safe.
Then the Supabase Security Advisor shows a red error: "View bypasses row-level security" (its code is security_definer_view). Or your AI agent made a view to "make the query easier," and now every user can see every row.
This guide explains why that happens and how to fix it in a few minutes. Only test apps you own.
The short answer
- A normal Postgres view ignores RLS by default. It runs with the rights of the person who created it, and that is usually an admin role.
- Views in the
publicschema are part of your API. So anyone with your public (anon) key may be able to read them. - The fix is one setting:
security_invoker = on. Then the view uses the rights of the person asking, and your RLS policies apply again. - Check functions too. A function marked
SECURITY DEFINERalso skips RLS. If anyone can call it, it can leak data. - Materialized views can't use RLS at all. Keep them out of the API.
Why this happens
Supabase's own docs say it plainly: Postgres views default to "security definer." That means they "use the permissions of the view's creator, rather than the permissions of the querying user." Supabase calls this "an unintuitive default" kept for older versions (Supabase: Database Advisors, lint 0010).
Here is the problem in plain words:
- Your
orderstable has RLS. User A can only see user A's orders. Good. - Your AI agent creates a view called
order_summarythat reads fromorders. - The view runs as the admin who made it. Admins skip RLS.
- So
order_summaryshows everyone's orders to anyone who can reach it.
AI coding tools make views often. They are handy for dashboards, joins and "stats" pages. The tool rarely adds the safety setting.
Step 1: Look at the Security Advisor
In your Supabase dashboard, open Advisors → Security Advisor. Look for these:
| Advisor warning | What it means |
|---|---|
View bypasses row-level security (security_definer_view) | A view in public ignores your RLS policies |
User data exposed through a view (auth_users_exposed) | A view shows data from auth.users, like emails |
Materialized view exposed in API (materialized_view_in_api) | A materialized view anyone with API access can read in full |
| SECURITY DEFINER function callable without signing in | A function that skips RLS and anyone can call |
| SECURITY DEFINER function callable by signed-in users | Same, but any signed-up user can call it |
If you use the Supabase CLI, supabase db advisors runs the same checks.
Step 2: Find risky views with one query
Open the SQL Editor and run this. It lists views in public that do not have security_invoker turned on:
select c.relname as view_name
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname = 'public'
and c.relkind = 'v'
and not coalesce(
'security_invoker=true' = any(c.reloptions)
or 'security_invoker=on' = any(c.reloptions),
false
);No rows means no risky views. Each row is a view to fix or remove.
Step 3: Fix the view
The easy fix keeps the view and turns on the safe setting:
alter view public.order_summary set (security_invoker = on);Or, when you create a new view:
create view public.order_summary
with (security_invoker = on)
as
select id, user_id, total, created_at
from public.orders;Now the view follows the RLS policies on orders. User A sees only user A's rows.
Two things to check after:
- The table under the view needs RLS and policies.
security_invokeronly passes your rules through. If the table has no RLS, the view is still open. - A view over
auth.usersneeds extra care. Supabase suggests aprofilestable filled by a trigger, with its own RLS, instead of exposingauth.users(same Supabase page, lint 0002).
If nobody needs the view from the app, you can just drop it or move it to a schema that is not exposed.
Step 4: Check SECURITY DEFINER functions
Functions can have the same problem. A function marked SECURITY DEFINER runs with its owner's rights. In Supabase, any function in public is callable at /rest/v1/rpc/<name> by default for both logged-out and logged-in users, unless you remove that access.
List them:
select p.proname as function_name,
pg_get_function_identity_arguments(p.oid) as args
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and p.prosecdef;For each one, ask: "Should a stranger be able to run this?" If not, pick one:
-- Option A: nobody outside can call it
revoke execute on function public.my_function(int) from anon, authenticated, public;
-- Option B: it doesn't need admin rights, so let RLS apply
alter function public.my_function(int) security invoker;Some functions are meant to be public, like a "submit contact form" function. That's fine. Make sure it checks its input and does only one small job.
Step 5: Keep materialized views out of the API
Materialized views can't use RLS. If they live in public, anyone with API access may read every row. Remove access like this:
revoke select on public.my_stats from public, anon, authenticated;Step 6: Test it like a stranger
Don't just trust the setting. Test it:
- Sign in as user A and load the page that uses the view. Note what you see.
- Sign in as user B. You should not see user A's rows.
- Log out and call the view with only the public anon key. It should return nothing private.
Our guide on testing whether users can see each other's data walks through this with two test accounts.
Copy-paste prompt for your coding agent
Paste this into Lovable, Bolt, Cursor or your agent of choice:
Please fix Supabase views and functions that bypass Row Level Security.
1. Find every view in the public schema. For each one, make it respect RLS with
security_invoker = on (ALTER VIEW ... SET (security_invoker = on)), unless it
is not used by the app, in which case drop it.
2. Make sure every table those views read from has RLS enabled and policies that
limit rows to the signed-in user (or their team/organization).
3. If any view reads from auth.users, replace it with a public.profiles table
filled by a trigger, with RLS, exposing only the columns the app needs.
4. List every SECURITY DEFINER function in public. For each one, either switch it
to SECURITY INVOKER, or revoke EXECUTE from anon, authenticated and public if
the app does not call it. Keep it only if it must bypass RLS, and then make it
check auth.uid() and validate its inputs.
5. Revoke SELECT from anon, authenticated and public on any materialized view in
public, or move it to a non-exposed schema.
6. Do not change any other features. When done, list what you changed and tell me
how to test with two users that user B cannot see user A's rows through any view.After the agent finishes, run the Security Advisor again. The errors above should be gone.
FAQ
I turned on RLS. Why can people still see data through my view? Because a default view runs as its creator, not as the person asking. Turn on security_invoker for the view.
Is security_invoker available on Supabase? Yes. It was added in Postgres 15, and Supabase's docs recommend it for views that should respect RLS.
Will this break my app? It can, if your app relied on the view showing more rows than the user should see. That's the leak you are fixing. Test with two accounts after the change.
Does the free scan find this? The free scan checks what a stranger can read from the outside, like open tables and leaked keys. It can't see your view settings from outside, so run the query in Step 2 too.
Sources
- Supabase: Database Advisors (lints 0002, 0010, 0016, 0028, 0029)
- Supabase: Row Level Security
- PostgreSQL: CREATE VIEW (security_invoker)
Want a second set of eyes on the outside of your app? Run a free VibeVigil scan to check for exposed keys, open Supabase tables, headers and SSL. Only scan apps you own.
Scan your app free. It checks the public page of a site you own. No account.