Why does a view ignore my table's policies?
Because of how PostgreSQL checks views. Unless a view has the security_invoker option, the tables underneath it are checked against the privileges of the view's owner, and the owner's row level security applies, not the caller's (PostgreSQL, CREATE VIEW). Supabase spells out the result: "Views bypass RLS by default because they are usually created with the postgres user" (Supabase, row level security).
Supabase's own database linter rates this an error and calls the default "unintuitive", kept for backwards compatibility with older Postgres versions (lint 0010, security definer view).
A developer who writes as Pon on dev.to described exactly this. A public_profiles view meant to show usernames and avatars also selected email, and "every email address in my users table was readable with just the anon key". It stayed that way for about three weeks, and it was found by calling the view's address with only the public key (Pon, dev.to).
Materialized views are worse in one way: they cannot be made to respect the policies of their tables at all, so every row is visible to any role that can select from them (lint 0016).
Why does a function skip them too?
SECURITY DEFINER means the function "is to be executed with the privileges of the user that owns it" (PostgreSQL, CREATE FUNCTION). Supabase's functions guide is blunt about what that means in a project: a function created in the Dashboard or by a migration is owned by postgres, which bypasses row level security, and "any role can call it by default, including anon" (Supabase, database functions).
Three details make this easy to miss:
- It does not need to be in your app. The linter notes that
/rest/v1/rpcaccepts any function name the role hasEXECUTEon, so a helper written for an admin script is callable the same way (lint 0028). - Granting only to
authenticatedis not a fix. Supabase points out that an execute grant controls who may call the function, not which rows come back, so it "still returns any user's row to every signed-in caller" (database functions). - A pinned
search_pathis about something else. It stops a caller swapping in their own objects. It "doesn't change who can call the function" (same page).
The author of a dev.to post titled "I built a Supabase security tool, then found 14 critical leaks in my own production CRM" found six security definer functions that signed-out visitors could execute, among them get_dashboard_stats, next to tables whose policies were USING (true). Their summary: "The dashboard 'RLS enabled' checkmark is misleading." The fix included REVOKE EXECUTE ... FROM anon, public, and the count went from 14 to 0 (Perufitlife, dev.to).
Not every security definer function is a mistake. Supabase recommends them inside policies, to read a membership table without recursion, but in a schema the API does not expose (row level security). The question is where the function lives and who can call it.
How do I list the views that skip RLS in my project?
Run this in the SQL Editor. Add every schema listed under Exposed schemas in your API settings to the in (...) list.
select c.relname as view_name,
case c.relkind when 'm' then 'materialized' else 'view' end as kind,
pg_get_userbyid(c.relowner) as owner,
has_table_privilege('anon', c.oid, 'select') as signed_out_can_read,
has_table_privilege('authenticated', c.oid, 'select') as signed_in_can_read
from pg_class c
join pg_namespace n on n.oid = c.relnamespace
where n.nspname in ('public')
and c.relkind in ('v', 'm')
and not exists (
select 1 from unnest(coalesce(c.reloptions, '{}')) o
where o in ('security_invoker=true', 'security_invoker=on',
'security_invoker=1', 'security_invoker=yes')
)
order by 1;
Every row is a view that reads its tables as its owner. If signed_out_can_read is true, a visitor with your publishable key can select from it. We ran both queries in this article against a local PostgreSQL 17 database set up with Supabase's anon and authenticated roles.
How do I list the functions that skip RLS?
select p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')' as function_name,
pg_get_userbyid(p.proowner) as owner,
has_function_privilege('anon', p.oid, 'execute') as signed_out_can_call,
has_function_privilege('authenticated', p.oid, 'execute') as signed_in_can_call,
p.proconfig as settings
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname in ('public')
and p.prosecdef
and p.prorettype <> 'trigger'::regtype
order by 1;
has_function_privilege counts grants made to public as well, which is where PostgreSQL's default execute grant lives (securing your API). The Security Advisor in the dashboard reports the same problems as lints 0010, 0016, 0028 and 0029 (Supabase, advisors), so it is worth opening after any schema change.
How do I see what a stranger gets back?
Use your own project and your publishable key, the one already in your app's scripts. Supabase asks for that key in the apikey header (API keys).
The first command reads a view from the first query; the second calls a function from the second query, with its arguments as JSON.
curl "https://YOUR-PROJECT.supabase.co/rest/v1/monthly_revenue?select=*&limit=5" \
-H "apikey: YOUR_PUBLISHABLE_KEY"
curl -X POST "https://YOUR-PROJECT.supabase.co/rest/v1/rpc/get_dashboard_stats" \
-H "apikey: YOUR_PUBLISHABLE_KEY" \
-H "Content-Type: application/json" -d '{}'
Rows that only a member should see are a leak. An empty list, or a 42501 permission error, is what you want. Repeat it signed in as a test user from a second organisation by copying a request from the browser with its Authorization header.
How do I fix a view?
On Postgres 15 and above, make it run as the caller:
alter view public.monthly_revenue set (security_invoker = true);
Now the caller's policies apply, and the caller also needs select on the tables underneath (PostgreSQL, CREATE VIEW). Then look at the columns. Pon's view would still have shown emails to anyone the users policy allowed, so drop columns the page does not display.
For a materialized view, or a view that must not be public, take the API roles away:
revoke select on public.monthly_revenue_mv from public, anon, authenticated;
Then serve its numbers through server code that checks the caller, or move it to a schema the API does not expose.
How do I fix a function?
Pick one of three, per function.
It never needed to bypass RLS. Most dashboard helpers do not. Switch it, and the table policies decide what each caller sees:
alter function public.get_dashboard_stats() security invoker;
It is only for your server or the SQL Editor. Revoke it from the API roles. Revoke from public too, or the default grant remains:
revoke execute on function public.get_dashboard_stats() from public, anon, authenticated;
It must bypass RLS for signed-in users. Then the function itself has to check the caller, because nothing else will:
create or replace function public.org_revenue(p_org uuid)
returns numeric
language plpgsql stable
security definer
set search_path = ''
as $$
begin
if not exists (
select 1 from public.org_members m
where m.organization_id = p_org
and m.user_id = (select auth.uid())
) then
raise exception 'not a member of this organisation' using errcode = '42501';
end if;
return (select coalesce(sum(i.amount), 0)
from public.invoices i where i.org_id = p_org);
end $$;
revoke execute on function public.org_revenue(uuid) from public, anon;
grant execute on function public.org_revenue(uuid) to authenticated;
Called for an organisation the user does not belong to, it raises an error; called signed out, it is refused before it runs.
To stop new functions being callable by everyone, Supabase documents revoking the default privileges, including revoke execute on functions from public. It warns that this also blocks helper functions your policies call until you grant them back (securing your API).
A quick reference
| You found | Who can reach it | Fix |
|---|---|---|
View without security_invoker | Anyone with select on the view | alter view ... set (security_invoker = true) |
| Materialized view in an exposed schema | Anyone with select, every row | Revoke select from public, anon, authenticated |
SECURITY DEFINER function, no caller check | Every role with execute, anon by default | Switch to security invoker, or revoke execute |
SECURITY DEFINER helper used in policies | Should be nobody directly | Keep it in a private schema, grant execute narrowly |
SECURITY INVOKER function | The caller, under table policies | Nothing, if the tables have correct policies |
Run the two queries after every migration your AI coding tool writes, not only once. A new chart or export is the usual moment a new view or function appears.