Your first check is free. No card needed.Run a free security check

Supabase views and functions that skip row level security, and how to find them in your project

A view in Supabase ignores the row level security on its tables unless it was created with security_invoker = true, because a view runs with its owner's privileges and the owner is usually postgres, which bypasses RLS. A SECURITY DEFINER function does the same, and any function in the public schema can be called through /rest/v1/rpc by default, even by signed-out visitors. List both with the two queries below, set security_invoker on each view, and for each security definer function either switch it to security invoker, revoke execute from anon and public, or move it to a schema the API does not expose and check the caller inside it.

Imagine your invoices table has careful policies: each organisation sees only its own rows, and you tested it with two accounts. Later you ask your AI coding tool for a revenue chart. It writes a monthly_revenue view, or a get_dashboard_stats() function, and the chart works on the first try.

Nothing in the Table Editor changed. The table still shows RLS as enabled. But the chart's data now comes from an object that reads invoices as the postgres role, and anyone who copies that request from the browser can call it for every organisation, signed in or not.

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/rpc accepts any function name the role has EXECUTE on, so a helper written for an admin script is callable the same way (lint 0028).
  • Granting only to authenticated is 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_path is 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 foundWho can reach itFix
View without security_invokerAnyone with select on the viewalter view ... set (security_invoker = true)
Materialized view in an exposed schemaAnyone with select, every rowRevoke select from public, anon, authenticated
SECURITY DEFINER function, no caller checkEvery role with execute, anon by defaultSwitch to security invoker, or revoke execute
SECURITY DEFINER helper used in policiesShould be nobody directlyKeep it in a private schema, grant execute narrowly
SECURITY INVOKER functionThe caller, under table policiesNothing, 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.

Where does Saqr fit?

If you want this checked from the outside on every release, a Saqr task calls the views and functions your published app uses, signed out and as a second test account, and shows the request and response for anything that returns another organisation's rows, with a fix prompt for your AI coding tool. Your first check is free, no card needed.

Saqr (usesaqr.com) is AI security for web apps. Its AI agents attack your live app from the outside, the way a real attacker would, and show you exactly what to fix. From $19 a month, cancel any time. Tests only apps you have shown are yours.

Questions

Does security_invoker work on every Supabase project?

It needs Postgres 15 or above. Supabase says that on older versions you should protect views by revoking access from the anon and authenticated roles, or by putting them in a schema that is not exposed.

Why does my view return nothing after I set security_invoker?

The view now runs as the caller, so the caller needs select on the tables underneath and a policy that allows the rows. Check the grants first; a missing grant gives a 42501 error before any policy runs.

Is a SECURITY DEFINER function always a security problem?

No. Supabase recommends them as helpers inside policies, for example to read a membership table. They become a problem when they sit in an exposed schema, can be called by anon or authenticated, and do not check who is calling.

Does the Supabase Security Advisor catch these?

It flags views without security_invoker, materialized views in the API, and security definer functions that anon or authenticated can execute. It cannot tell whether a function checks its caller correctly, so read each flagged function and test it.

Can I hide a function by not calling it from my app?

No. The REST endpoint accepts any function name the role is allowed to execute, whether or not your app's code ever calls it. Revoke execute or move it out of the exposed schema.