AleannLab

Do Supabase Views Bypass RLS? Yes, Unless You Set security_invoker

AleannLab team3 min read

Yes. By default a Postgres view runs with the rights of the role that created it, and in Supabase that role is usually postgres, which is not restricted by your RLS policies. A view over a well-protected table can hand every row to anyone with your public key. The fix is one line: alter view ... set (security_invoker = true).

On this page
  1. Why it happens
  2. Find the views that do this
  3. The fix
  4. Materialized views are different
  5. When you do want the owner's rights
  6. Test it

This is one of the quieter leaks we find in AI-built apps. The tables look right, RLS is on, the policies are correct, the two-account test passes on the tables. Then there is a view called user_stats or orders_with_customers that an assistant added to simplify a dashboard query, and it returns everything.

Why it happens

RLS policies are checked against the role running the query. When you select from a view, Postgres runs the view's underlying query as the view's owner, not as you. The owner created the tables, so the policies do not apply to it.

The result:

SQL
create view public.orders_with_customers as
select o.id, o.total, o.created_at, p.email, p.full_name
from public.orders o
join public.profiles p on p.id = o.user_id;

orders and profiles both have strict policies. This view ignores them. Any client can run:

JavaScript
const { data } = await supabase.from('orders_with_customers').select('*');

and get every order with every customer's email.

Find the views that do this

SQL
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(
    (select option_value::boolean
     from pg_options_to_table(c.reloptions)
     where option_name = 'security_invoker'),
    false
  );

Every view in the result runs with its owner's rights. The Supabase Security Advisor reports the same thing as "Security Definer View".

Then check who can reach them:

SQL
select table_name, grantee, privilege_type
from information_schema.role_table_grants
where table_schema = 'public'
  and table_name in (select viewname from pg_views where schemaname = 'public')
  and grantee in ('anon', 'authenticated');

The fix

Postgres 15 and later support security_invoker. With it, the view runs as the caller, and the policies on the underlying tables apply again:

SQL
alter view public.orders_with_customers set (security_invoker = true);

For new views, set it at creation:

SQL
create view public.orders_with_customers
with (security_invoker = true) as
select o.id, o.total, o.created_at, p.email, p.full_name
from public.orders o
join public.profiles p on p.id = o.user_id;

After the change, test the screens that use the view. If a dashboard goes empty, the view was relying on bypassing RLS to work, and the underlying policies need a rule for that case. For example, an admin dashboard needs an admin policy on orders. Do not switch security_invoker back off to get the data back.

Materialized views are different

A materialized view stores its result as a table-like object. It has no security_invoker option and you cannot put RLS policies on it. Whoever can select from it sees all of it.

Two safe patterns:

  1. Keep it out of the API. Create it in a schema the Data API does not expose (for example private), or revoke access:
SQL
revoke all on public.daily_revenue from anon, authenticated;
  1. Serve it through a function or a regular security_invoker view that filters by auth.uid().

When you do want the owner's rights

Sometimes a view exists to show a safe subset of a private table: public profile fields from a table that also holds emails and phone numbers. That is a legitimate use. Make it deliberate:

  • List columns by name. Never select *, because a column added later becomes public too.
  • Grant only select, and only to the roles that need it.
  • Write a comment on the view saying it is public by design, so the next person (or the next agent session) does not "fix" or extend it blindly.

Test it

Use two accounts, the same way as for tables. Log in as user A and query every view from the browser console:

JavaScript
const { data } = await supabase.from('orders_with_customers').select('*');
console.log(data.length); // only A's rows

Then log out and repeat with no session. An anonymous visitor should get an empty result or a permission error.

Views are easy to miss because most RLS guides and most scanners look at tables. The broader list of things we check is in our post on Supabase RLS mistakes and in the security checklist for vibe-coded apps. If you would like a person to go through your schema, see our app rescue and security audit.

All articles →

Ready to discuss your project with us?

idea
*
*
*
I'm interested in...
Web Development
App Development
UX/UI Design
DevOps
Artificial Intelligence And Machine Learning
QA Testing
Mobile Development
Consultation
OutSource
Project budget (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Upload file