RLS was on for my projects table. The anon key got 401. A signed-in client saw only their own company's 3 projects.
Then I put a plain view on top of the same table and granted select on it. Same anon key, same API:
GET /rest/v1/projects → 401 (42501)
GET /rest/v1/probe_projects_v → 200 6 rows
Every project from both companies. RLS was still on. The view just never asked.
This is a local test. My production database has no views in the exposed schemas. But a view is an easy thing to add for a dashboard, and the grant is often copied from the table next to it.
Why it happens
A view runs with the privileges of its owner, not the caller. In Supabase, a view you create in the SQL Editor or a migration is owned by postgres. And postgres has BYPASSRLS:
select rolname, rolsuper, rolbypassrls from pg_roles where rolname = 'postgres';
-- postgres | f | t
So the view reads the table as postgres, and the policies are skipped.
A reader on my post about default privileges asked exactly this, which is why I tested it.
What I tested
The view I used:
create view public.probe_projects_v as
select id, organization_id, name, status, amount, payment_status
from public.projects;
Then I compared it with the table, inside transactions that rolled back. Two companies, 3 projects each:
| Client in company A | anon | |
|---|---|---|
projects table |
3 rows | 42501 |
| view, no grant | 42501 permission denied for view |
42501 |
view, granted to authenticated and anon
|
6 rows | 6 rows |
same view with security_invoker = true
|
3 rows | 42501 |
I repeated the last two rows through the real REST API (/rest/v1) with the client's JWT and with the anon key only. Same results: 200 with 6 rows, then 200 with 3 rows and 401 for anon.
The fix for views: security_invoker
Since PostgreSQL 15, a view can run as the caller instead of the owner:
create view public.probe_projects_v
with (security_invoker = true) as
select id, organization_id, name, status, amount, payment_status
from public.projects;
-- or, for an existing view
alter view public.probe_projects_v set (security_invoker = true);
Now the table's policies apply to whoever calls the view.
Materialized views can't do this
create materialized view public.probe_projects_mv
with (security_invoker = true) as ...
-- ERROR 22023: unrecognized parameter "security_invoker"
A materialized view stores the rows it read when it was refreshed. There is no caller to check later. Once anon or authenticated can select from it, everyone sees everything in it.
The only protection is not to grant it. Keep it out of the exposed schemas, or revoke select from anon, authenticated and PUBLIC.
Do closed default privileges help?
Partly. In the previous post I closed the default privileges for objects that postgres creates in public. ON TABLES also covers views. So a new view gets no grants for anon, authenticated or service_role, and reading it fails with 42501.
But that only holds until someone writes the grant. The leak above started with one line I typed myself.
And I found two places it doesn't cover:
- Views that
supabase_admincreates inpublic - Views that
postgrescreates instorage(not exposed through the API in my project)
Both get full privileges for anon and authenticated the moment they're created.
Guard: let CI find them
I added two checks to the access-check script that runs on every pull request.
Guard 1: every view in the exposed schemas uses security_invoker. The value in reloptions is stored as you wrote it (on, 1, yes, true), so compare it as a boolean, not as text:
select n.nspname, c.relname
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'v'
and n.nspname in ('public', 'graphql_public')
and not coalesce((
select o.option_value::boolean
from pg_options_to_table(c.reloptions) o
where o.option_name = 'security_invoker'
), false);
-- expect 0 rows
Guard 2: no materialized view in those schemas is readable by anon, authenticated or PUBLIC.
select n.nspname, c.relname
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where c.relkind = 'm'
and n.nspname in ('public', 'graphql_public')
and (has_table_privilege('anon', c.oid, 'select')
or has_table_privilege('authenticated', c.oid, 'select'));
-- expect 0 rows
These are simplified. What they don't do:
- My script also skips views owned by an extension (
pg_dependwithdeptype = 'e'). These versions will flag them. - Neither version catches a column-level grant like
grant select (id) on ... to anon.has_any_column_privilegewould. - The schema list is typed by hand. If you expose another schema, update it here too.
To make sure the guards actually fire, I left a plain view and a granted materialized view in my local database and ran the script: 515 pass, 3 fail. After dropping them: 518 pass, 0 fail.
Quick checklist
- List views and materialized views in every schema the Data API exposes.
- Views: set
security_invoker = true, then test as a signed-in user and as anon. - Materialized views: no
selectforanon,authenticatedorPUBLIC. - Before you write
grant select on <view>, ask whose privileges it will run with. - Put the check in CI. A view added next month won't be reviewed by hand.
The pull request with the guards and the tests: https://github.com/kuros-works/client-portal-demo/pull/26
RLS being on for every table doesn't mean every path to the data goes through it. Views, materialized views and functions are the paths that are easy to miss, because nothing breaks when they leak. If you'd like a second pair of eyes before launch, I review Supabase RLS policies and privileges as a fixed-price job on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-security-review-for-your-lovable-bolt-or-cursor-app-2126067179511646761
Top comments (0)