Short answer
SECURITY DEFINER makes a Postgres function run with the privileges of the role that owns it instead of the caller. In Supabase that owner is usually postgres, so the function skips row level security. Prefer the default, SECURITY INVOKER. If you need definer, pin search_path, check auth.uid() inside, keep it out of exposed schemas and revoke execute from anon and authenticated, not just from public.
Ask an AI about this page:ChatGPTClaude
On this page
SECURITY DEFINER is a Postgres function option that makes the function run with the privileges of the role that owns it, not of the role that calls it. The default is SECURITY INVOKER, which runs as the caller. Postgres’s own docs warn that a definer function needs care so it “cannot be misused”, because whoever can call it borrows its owner’s rights.
SECURITY DEFINER in an AI-built app
In Supabase, functions created in the dashboard or by a migration belong to postgres, a role that bypasses row level security. Any function in the public schema can also be called from the browser as supabase.rpc("name"). Together that makes a definer function a door around your policies.
AI builders reach for security definer when a policy check needs to read another protected table, or when a query “doesn’t return anything” under RLS. A typical result:
create function public.get_invoices(p_user uuid)
returns setof public.invoices
language sql
security definer
as $$
select * from public.invoices where user_id = p_user;
$$;It skips RLS, trusts the user ID the caller passes, has no fixed search_path, and sits in public, so a visitor with the anon key can call it with someone else’s ID. In our test, anon read another user’s invoices this way.
How to check your app
The Security Advisor flags functions without a fixed search path (lint 0011) and definer functions that anon or signed-in users can execute (lints 0028 and 0029). To see them in one list:
select p.proname as function_name,
p.proconfig as settings,
has_function_privilege('anon', p.oid, 'execute') as anon_can_call
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and p.prosecdef
order by p.proname;settings without search_path=… and anon_can_call = true are the two red flags.
How to fix or prevent it
Most of the time you don’t need definer at all. If the function only returns the caller’s own rows, let RLS do the work:
drop function public.get_invoices(uuid);
create function public.get_my_invoices()
returns setof public.invoices
language sql
security invoker
set search_path = ''
stable
as $$
select * from public.invoices
where user_id = (select auth.uid());
$$;When definer is really needed, for example a membership helper used inside policies, follow four rules: set search_path = '' with fully qualified names, an auth.uid() check inside, a schema that is not exposed through the API, and execute granted only to the roles that need it. The long version is SECURITY DEFINER functions done right; the fix for an existing function is on SECURITY DEFINER function without checks.
Frequently asked questions
Does a SECURITY DEFINER function respect RLS?
It runs as its owner. Functions created in the Supabase dashboard or by a migration are owned by postgres, which bypasses RLS, so the queries inside see every row unless the function filters them itself.
Can visitors call my function through the API?
If it lives in an exposed schema such as public and anon has execute, yes: through /rest/v1/rpc. In Supabase, anon and authenticated get execute on new functions in public by default, directly, not only through public.
Why does search_path matter?
Unqualified names inside the function are looked up along search_path. If a caller can put a look-alike table or function earlier on that path, the privileged function uses theirs. Setting search_path = ‘’ and writing public.table closes that.
Sources
- 1.CREATE FUNCTION (Writing SECURITY DEFINER Functions Safely) — PostgreSQL Documentation
- 2.Database Functions — Supabase Docs
- 3.Row Level Security — Supabase Docs
- 4.Performance and Security Advisors — Supabase Docs
- 5.CWE-250: Execution with Unnecessary Privileges — MITRE