Guides · Updated 2026-12-01
Supabase SECURITY DEFINER functions and rpc(): can anyone call my database function?
Short answer: Often yes. Postgres gives EXECUTE on every new function to PUBLIC, Supabase also grants it to anon and authenticated by default, and the Data API exposes functions in the public schema at /rest/v1/rpc/<name>. So anyone holding your public anon key can call them, logged in or not. For a normal (security invoker) function that's usually fine, because RLS still applies. A security definer function is different: it runs as its owner, usually postgres, which bypasses RLS. Prefer security invoker; if you need definer, set search_path = '', check auth.uid() inside, and revoke execute from public and anon (and authenticated if signed-in users shouldn't call it).
Why this shows up in AI-built apps
A common pattern in vibe-coded Supabase apps: the AI hits a permission error (for example a 42501 or an RLS policy that blocks a query) and "fixes" it by wrapping the query in a function declared security definer. The error goes away because the function now runs with its owner's rights instead of the user's. The side effect is that the function can now read or change rows that RLS was protecting, and by default anyone with the public anon key can call it.
The function doesn't need to be used anywhere in your frontend. The Data API accepts any function name the calling role is allowed to execute, so a helper written for an admin script is reachable too.
Invoker vs definer in one sentence each
- security invoker (the default): the function runs as whoever calls it, so RLS on the tables it touches still applies. A logged-out caller only gets what anon is allowed to see.
- security definer: the function runs as the role that owns it. Functions created in the Dashboard or by a migration are owned by postgres, which bypasses Row Level Security, so the function can return any row in any table.
Supabase's docs say it plainly: prefer security invoker, and if you use security definer you must set the search_path.
Who can call your functions by default
- PostgreSQL grants EXECUTE on new functions to PUBLIC (every role) by default.
- In default Supabase projects, anon and authenticated also get default-privilege grants for new functions, so revoking from anon alone usually leaves the function callable by every signed-up user.
- Functions in an exposed schema (public, by default) are callable through POST /rest/v1/rpc/<function_name>, and through GraphQL if pg_graphql is enabled and the return type is supported.
- An execute grant decides which roles may call the function. It does not limit which rows come back. Granting only to authenticated still lets any signed-in user get any user's row unless the function checks ownership itself.
How to check your project in 5 minutes
- Open Supabase Studio → Advisors → Security Advisor. Look for 0028_anon_security_definer_function_executable ("callable without signing in"), 0029_authenticated_security_definer_function_executable ("callable by signed-in users"), 0011_function_search_path_mutable, and 0031/0032 (functions that run a SQL string passed by the caller). The same checks run from the CLI with supabase db advisors.
- List your security definer functions and their owners with the SQL below.
- For each one, ask: should someone with no login be able to run this? Should every signed-in user? Does it read or change other people's rows?
select p.proname, pg_get_function_identity_arguments(p.oid) as args, pg_catalog.pg_get_userbyid(p.proowner) as owner, p.prosecdef as security_definer from pg_catalog.pg_proc p join pg_catalog.pg_namespace n on n.oid = p.pronamespace where n.nspname = 'public' and p.prosecdef order by p.proname;
How to lock it down
1. If the function doesn't really need to bypass RLS, switch it to invoker. RLS on the underlying tables then decides what the caller gets:
alter function public.my_function(int, text) security invoker;
2. If it must stay security definer, pin the search path to empty and fully qualify every table (public.profiles, not profiles), and check the caller inside the function:
create or replace function public.get_my_orders() returns setof public.orders language sql security definer set search_path = '' as $$ select * from public.orders where user_id = (select auth.uid()); $$;
3. Narrow who can execute it. Revoke from public and from each role that has a direct grant, then grant only to the role that needs it:
revoke execute on function public.get_my_orders() from public, anon; grant execute on function public.get_my_orders() to authenticated; -- an admin-only or script-only helper: nobody from the API revoke execute on function public.admin_cleanup() from public, anon, authenticated;
4. To stop new functions from being callable automatically, change the default privileges. Postgres' PUBLIC grant is global, so revoke it without "in schema"; then grant execute on each new function to the roles that call it, including helper functions your RLS policies use:
alter default privileges revoke execute on functions from public; alter default privileges in schema public revoke execute on functions from anon, authenticated;
5. Functions that should never be called from the app (admin jobs, triggers' helpers) can live in a schema that isn't exposed to the Data API instead of public.
Then test it like a stranger
From a logged-out browser or a plain request with only the anon key, call the function through supabase.rpc('function_name'). It should fail with a permission error, or return only public data. Then sign in as a second test user and call it again: it must not return the first user's rows.
A prompt for your AI tool
List every function in the public schema of my Supabase project, showing whether it is security definer, its owner, and which roles (public, anon, authenticated) can execute it. For each security definer function: switch it to security invoker if it doesn't need to bypass RLS; otherwise add set search_path = '', fully qualify every table, and filter rows by (select auth.uid()) inside the function. Revoke execute from public and anon on anything that shouldn't be callable without login, and from authenticated on admin-only helpers. Never fix a permission error by making a function security definer. Show me the SQL before running it.
What this guide doesn't cover
A passive outside scan can't list your database functions or their grants; the Security Advisor inside your project is the right tool for that. What a LeakyVibe scan does check, in about 15 seconds and without signup, is your published app from the outside: Supabase secret or service_role keys and other secrets shipped in your JavaScript, which would make every RLS rule and grant here pointless.
Sources
Checked 2026-12-01. Supabase, "Database functions" (https://supabase.com/docs/guides/database/functions): prefer security invoker, the default; security definer requires setting search_path; functions created in the Dashboard or by a migration are owned by postgres, which bypasses RLS; any role can call a function by default, including anon; an execute grant doesn't limit rows returned; revoke from public and from roles with direct grants; the PUBLIC default is global, so revoke it without in schema. Supabase, "Advisors" (https://supabase.com/docs/guides/database/database-advisors): lints 0028 and 0029 (security definer functions executable by anon / authenticated, reachable at /rest/v1/rpc/), 0011 (mutable search_path), 0031/0032 (dynamic SQL), and the owner query used above. PostgreSQL, "Privileges" (https://www.postgresql.org/docs/current/ddl-priv.html): EXECUTE on functions is granted to PUBLIC by default. PostgreSQL, "CREATE FUNCTION" (https://www.postgresql.org/docs/current/sql-createfunction.html), section Writing SECURITY DEFINER Functions Safely.