Deutsch
GuideSupabase · Database

Supabase views bypass RLS: security_invoker explained

Why a Postgres view in Supabase can show every row even when the table has RLS, how security_invoker fixes it and how to find leaky views, with tested SQL.

By Tim GeithnerPublished 6 min read

Short answer

A normal Postgres view runs with the rights of its owner, which in Supabase is usually the postgres role, so it ignores the row level security of the tables underneath and shows every row to anyone who may read the view. On Postgres 15 and later, create views with security_invoker = true so they check the caller’s policies instead, and keep that option when you recreate a view. Materialized views can’t use RLS at all: revoke API access to them.

Ask an AI about this page:ChatGPTClaude

On this page

Key takeaways

  • A view checks the tables underneath with the owner’s rights. In Supabase the owner is usually postgres, so RLS on those tables is skipped.
  • On Postgres 15+, add with (security_invoker = true) and the view checks the caller’s policies.
  • create or replace view without that option switches it off again. AI tools recreate views often; check after every migration.
  • Materialized views have no RLS. Revoke select from anon and authenticated or keep them out of the API.
  • One query finds every view in public that still runs with owner rights.

Why a view can leak a protected table

Suppose your documents table has perfect row level security: each user reads only their own rows. Then someone asks the AI builder for “a list of documents with their titles”, and it adds a view:

Vulnerablesupabase/migrations/…_document_list.sql
create view public.document_list as
select id, user_id, title, created_at
from public.documents;

In the app nothing changes, because the frontend still filters by user ID. But the view is in the public schema, so the Data API serves it, and Supabase’s default grants let anon and authenticated read it. Anyone with your public key can now ask for document_list and get every user’s documents.

The reason is in the Postgres documentation for CREATE VIEW: by default, access to the tables a view reads is checked with the permissions of the view owner, and if those tables have RLS, the policies of the view owner apply. Supabase’s docs put it plainly: “Views bypass RLS by default because they are usually created with the postgres user.” That role owns your tables, and row level security does not apply to a table’s owner.

So the policies you wrote for authenticated never run. The view asks as postgres, and postgres sees everything.

The fix: security_invoker

Since Postgres 15, a view can run with the rights of the person who queries it. Set the option on the existing view:

SafeFix an existing view
alter view public.document_list set (security_invoker = true);

Or create it that way from the start:

SafeCreate the view with security_invoker
create or replace view public.document_list
with (security_invoker = true)
as
select id, user_id, title, created_at
from public.documents;

Now a query on the view behaves exactly like a query on the table. In our test, Alice saw only her document through the view, Bob only his, a visitor with the public key saw nothing and the service role still saw all rows.

There is one side effect to know: with security_invoker, the caller also needs a grant on the table. If you revoked all rights on documents from anon, a visitor querying the view now gets permission denied for table documents instead of an empty list. That is the correct result. It only surprises you if the view was meant to be public, and then it should read from a table whose policies say so.

How do I check one view by hand?

The query further down finds views without the option. To see what a single user really gets through a view, ask as that user. Copy a user ID from Authentication → Users, then run:

Query the view as one user
begin;
set local role authenticated;
set local request.jwt.claim.sub = '11111111-1111-1111-1111-111111111111';
select count(*) from public.document_list;   -- expect: this user's rows only
rollback;

Compare the number with the same query on the table itself. If the view returns more, it bypasses RLS. The transaction ends with rollback, so nothing is changed. Our guide on testing RLS policies shows the same technique for visitors, second users and writes.

The trap: recreating a view switches the fix off

This one surprised us while testing. create or replace view without a with (…) clause does not keep the old options. It resets them:

VulnerableLooks harmless, reopens the leak
create or replace view public.document_list as
select id, user_id, title, created_at
from public.documents;

After this statement the view runs with owner rights again and Alice sees every document. AI builders regenerate views whenever you add a column, so a leak you fixed last week can come back with the next “add the status to the document list” prompt. Always write the option into the create statement itself, not only into a separate alter view, and re-run the check below after migrations.

Find every view that still bypasses RLS

The Security Advisor in your dashboard flags these views as lint 0010 (security_definer_view). You can also list them yourself in the SQL Editor:

Views in public without security_invoker
select c.relname as view_name
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'v'
  and n.nspname = 'public'
  and not coalesce(
    array_to_string(c.reloptions, ',') ~* 'security_invoker=(true|on|yes|1)',
    false
  );

Every row is a view that checks its tables as the owner. For each one, decide:

  • Should users see only their own rows through it? Add security_invoker = true.
  • Is it meant to be public, for example a list of published posts? Then it should still respect RLS: make it security_invoker and give the table a clear public policy, so the rule lives in one place.
  • Is it only for admin dashboards or server jobs? Move it to a schema the API doesn’t expose, or revoke access from anon and authenticated.

If your project runs on Postgres older than 15, security_invoker is not available. Supabase’s advice is to revoke access to such views from anon and authenticated. To see your version, run select version(); in the SQL Editor.

Materialized views: no RLS at all

A materialized view stores a copy of the query result. It is fast for statistics and dashboards, and it has no row level security and no security_invoker option. In the public schema it is served by the API like any table:

VulnerableA per-user statistic, readable by everyone
create materialized view public.document_stats as
select user_id, count(*) as documents
from public.documents
group by user_id;

In our test every signed-in user and every visitor could read the counts of all users. Supabase’s lint 0016 (materialized_view_in_api) recommends revoking select from the API roles. Include public, because every role inherits from it:

SafeClose a materialized view to the API
revoke select on public.document_stats from public, anon, authenticated;

Your server code with the secret key can still read it. To check all of them at once:

Materialized views and who can read them
select c.relname as materialized_view,
       has_table_privilege('anon', c.oid, 'select') as anon_can_read,
       has_table_privilege('authenticated', c.oid, 'select') as users_can_read
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'm'
  and n.nspname = 'public';

Both columns should be false unless the data in it is meant for everyone.

Views are not the only side door

The same “runs as the owner” logic applies to functions declared security definer. A function in public that reads a table with owner rights can return rows that RLS would hide. Our guide on SECURITY DEFINER functions covers how to write them safely, and the fix for definer functions without checks shows the pattern we flag most often. For the basics of policies themselves, start with the Supabase row level security guide.

A fix prompt for your AI builder

Make every view respect RLS

Add a new Supabase migration. For every view in the public schema, recreate it with the same query and with (security_invoker = true) in the create statement itself, so it checks the row level security policies of the user who queries it. Do not change the columns or the query.

For every materialized view in the public schema, run revoke select on it from public, anon and authenticated, and tell me which frontend code reads it so I can move that read to a server function.

Afterwards, list every view and materialized view you changed, and remind me to keep with (security_invoker = true) in any future create or replace view.

Frequently asked questions

Do Supabase views respect row level security?

Not by default. A view checks the underlying tables with the rights of its owner, and in Supabase that is usually the postgres role, which is not subject to the tables’ RLS. Create the view with security_invoker = true (Postgres 15 and later) and it checks the policies of whoever queries it.

What does the Security Advisor warning security_definer_view mean?

It is lint 0010. It flags views in an exposed schema that run with their creator’s rights and can therefore bypass RLS. The fix Supabase recommends is to set security_invoker on the view.

My Postgres version is older than 15. What can I do?

security_invoker does not exist before Postgres 15. Supabase’s advice for older versions is to revoke access to the view from the anon and authenticated roles, so the API can’t serve it, or to upgrade the project.

Can a materialized view have RLS or security_invoker?

No. A materialized view is a stored copy of the data and has no row level security. If it lives in an exposed schema, revoke select from public, anon and authenticated, which is what Supabase’s lint 0016 recommends.

Sources

  1. 1.Row Level Security — Supabase Docs
  2. 2.Performance and Security Advisors (lints 0010 and 0016) — Supabase Docs
  3. 3.CREATE VIEW — PostgreSQL Documentation
  4. 4.CWE-863: Incorrect Authorization — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt