Supabase database
Supabase SECURITY DEFINER functions: when rpc() skips RLS
A security definer function runs with the rights of the role that created it, which in Supabase is usually a role that RLS doesn’t apply to. Postgres lets everyone execute a new function, Supabase also grants it to anon, and every function in public is served at /rest/v1/rpc/<name>. Put together, one helper written for a policy or an AI-generated migration can hand any visitor rows your policies were built to hide.
TL;DR
- Postgres grants
EXECUTEon new functions toPUBLIC, and Supabase also grants new functions inpublictoanonandauthenticated. - So
revoke … from anonalone leaves thePUBLICgrant, andrevoke … from publicalone leaves theanongrant. Revoke both. - Pin the search path:
set search_path = ''and schema-qualify every name inside. - Better still: switch to
security invoker, or move helpers to a schema the API doesn’t expose.
The problem in one example
An AI builder was asked to “show the seller’s email on the order page”. The auth.users table isn’t readable through the API, so it wrote a function that is:
create or replace function public.get_user_email(uid uuid)
returns text
language sql
security definer
as $$
select email from auth.users where id = uid;
$$;The order page works. So does this, from any browser, with the public key that ships in your frontend and no sign-in:
curl -X POST 'https://<project>.supabase.co/rest/v1/rpc/get_user_email' \
-H 'apikey: <your public anon key>' \
-H 'Content-Type: application/json' \
-d '{"uid": "<any user id>"}'User ids are not secrets: they appear in URLs, in other tables’ rows and in Realtime messages. Anyone who collects them can now turn each one into an email address.
Why it skips RLS and why anyone can call it
Three defaults line up:
- Definer rights. A
security definerfunction runs as its owner. Migrations and the SQL editor run aspostgres, which owns your tables and isn’t subject to their row level security, so the function sees every row. Supabase’s advisor puts it plainly: security definer bypasses RLS. - Everyone can execute it. Postgres grants
EXECUTEon every new function toPUBLIC, which means every role. Supabase additionally grants new functions inpublictoanon,authenticatedandservice_rolethrough default privileges. - The API serves it. Functions in an exposed schema (by default
public) are callable at/rest/v1/rpc/<name>, andsupabase.rpc('name', args)in your frontend is the same call.
A security invoker function (the default when you write neither) is fine on all three counts: it runs as the caller, so the caller’s policies apply to every table it reads.
The second problem: a search path anyone can shadow
Inside a function, an unqualified name like profiles is looked up along the search_path. The PostgreSQL manual warns that a definer function whose path includes a schema untrusted users can write to, notably the temporary schema, which is searched first by default, can be made to use a look-alike table or function. Supabase reports functions with no fixed path as lint 0011 function_search_path_mutable.
Find definer functions anyone can call
Run this in the SQL editor. It lists every definer function in public, who can execute it, and whether its search path is pinned:
select p.oid::regprocedure as function,
has_function_privilege('anon', p.oid, 'execute') as visitors_can_call,
has_function_privilege('authenticated', p.oid, 'execute') as signed_in_can_call,
coalesce(array_to_string(p.proconfig, ', '), 'not set') as settings,
p.prorettype = 'trigger'::regtype as is_trigger
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and p.prosecdef
order by visitors_can_call desc, signed_in_can_call desc;| What you see | What it means |
|---|---|
visitors_can_call true | Anyone with your public key can run it. Read the body: if it returns or changes data for an id passed in, that is a leak. High. |
signed_in_can_call true only | Anyone who signs up can run it. With open sign-ups that is nearly the same as anyone. Read the body the same way. |
settings has no search_path= | The path isn’t pinned. Fix it even if the grants are right. |
is_trigger true | A trigger function. The API can’t call it directly, so the grants don’t matter; still pin the path. |
Supabase’s Security Advisor reports the same functions as 0028 anon_security_definer_function_executable and 0029 authenticated_security_definer_function_executable. Then confirm what a visitor gets, inside a transaction that changes nothing:
begin;
set local role anon;
select public.get_user_email('00000000-0000-0000-0000-000000000000');
-- "permission denied for function get_user_email" means visitors are locked out.
-- A result (even null) means they can call it.
rollback;The fix
Ask first whether the function needs owner rights at all. Most AI-written definer functions don’t; they were made definer to get past a “permission denied” during development. In order of preference:
1. Make it run as the caller
alter function public.get_order_totals(uuid) security invoker;Now the function only sees rows the caller’s policies allow. If it then returns nothing for a legitimate user, the gap is in your policies, and that is where it should be fixed.
2. If it must be definer: narrow it to the caller, pin the path, grant it on purpose
Take the user from auth.uid() inside the function instead of trusting an id in the arguments, and set the grants in the same migration that creates it:
drop function if exists public.get_user_email(uuid);
create or replace function public.get_my_seller_email(order_id bigint)
returns text
language sql
security definer
set search_path = ''
as $$
select u.email
from public.orders o
join auth.users u on u.id = o.seller_id
where o.id = order_id
and o.buyer_id = (select auth.uid()); -- only the buyer of this order
$$;
revoke execute on function public.get_my_seller_email(bigint) from public, anon;
grant execute on function public.get_my_seller_email(bigint) to authenticated;Revoke from both. revoke … from anon alone leaves the PUBLIC grant, which anon inherits. revoke … from public alone leaves the direct grant Supabase gave anon. The call test above tells you which one you missed.
With search_path = '', every table and function in the body must be schema-qualified (public.orders, auth.users). Built-ins like now() still work, because pg_catalog is always searched.
3. Helpers used only by policies: move them out of the API
A definer helper such as is_admin() or has_role(), written to avoid recursive policies, doesn’t need to be callable through /rpc at all. Put it in a schema the API doesn’t expose:
create schema if not exists private;
create or replace function private.is_admin()
returns boolean
language sql
security definer
set search_path = ''
stable
as $$
select exists (
select 1 from public.user_roles
where user_id = (select auth.uid()) and role = 'admin'
);
$$;
revoke execute on function private.is_admin() from public, anon;
grant usage on schema private to authenticated;
grant execute on function private.is_admin() to authenticated;
-- Policies call it as (select private.is_admin()).Policies run as the caller, so the caller still needs execute on the helper and usage on the schema; what changes is that the API no longer serves it.
Stop the next one
New functions keep getting the default grants. If your app calls few functions from the browser, flip the default so each one is granted on purpose:
-- 1. Remove the PUBLIC default. It is global, so this line has no "in schema".
alter default privileges revoke execute on functions from public;
-- 2. Remove Supabase's schema-level grant to visitors.
alter default privileges in schema public revoke execute on functions from anon;You need both lines. The one-line version you will often see, alter default privileges in schema public revoke execute on functions from anon, public;, does not stop visitors. Postgres adds schema-level defaults on top of the global ones and can’t take a global grant away inside a schema, so new functions still reach anon through PUBLIC. We tested both forms on Postgres 18 on 13 Nov 2026: with the one-liner a new function was still callable as anon; with the two lines above it wasn’t.
Default privileges are per creating role: run them as the same role your migrations use (normally postgres). They don’t touch existing functions, so after it, grant the ones your frontend calls to authenticated (or anon where visitors genuinely need them), and test the app’s sign-in and logged-out flows.
If an AI builder writes your migrations, give it the rule up front:
For every Postgres function you create in Supabase:
- Use SECURITY INVOKER unless owner rights are essential; say why if not.
- Add SET search_path = '' and schema-qualify every table and function.
- Never take a user id as an argument to decide whose data to return;
use (select auth.uid()) inside the function.
- In the same migration, REVOKE EXECUTE ... FROM public, anon, then GRANT
EXECUTE only to the roles that call it.
- Helpers used only inside RLS policies go in a "private" schema.When a definer function is the right tool
Owner rights are the correct choice in a few common places, and they are safe when the path is pinned and the grants are narrow:
- a policy helper that returns only true or false about the caller, kept in a private schema as above;
- the trigger that creates a
profilesrow when someone signs up (trigger functions can’t be called through/rpc); - a deliberate public summary, such as a count of listings per city, that returns only data meant for everyone. Leave a comment in the migration saying so, because the advisor will keep flagging it and the next edit needs to know it was a decision.
Sources
- PostgreSQL: CREATE FUNCTION, Writing SECURITY DEFINER Functions Safely
- PostgreSQL: Privileges (default EXECUTE for PUBLIC)
- Supabase: database advisors (lints 0011, 0028, 0029)
- Supabase: database functions
- PostgreSQL: ALTER DEFAULT PRIVILEGES (per-schema defaults add to global ones)
Related findings
The checks behind this guide, each with example code, a self-check and the fix.
Free check
Check your app for this, free.
Paste your public repo. PatchLabs rebuilds your database from its migrations, tests it as a visitor, a stranger and the owner, reads the code for keys and risky patterns, and hands you the fix.
Written by Manraj Singh Dhillon. Last checked 13 Nov 2026. Code samples are synthetic and run against your own project; nothing here names a real app. Spotted a mistake? Tell us.