Supabase: Public Can Execute SECURITY DEFINER Function
#supabase#postgres#security-definer#security-advisor#grants
The warning means anon can call a SECURITY DEFINER function through /rest/v1/rpc/<name>, and revoking from anon alone won't clear it, because Postgres grants EXECUTE on every new function to PUBLIC. Revoke from public, anon, authenticated in the migration that creates the function, then grant back only the roles that should call it. To make new functions start closed, you need two default-privilege changes, and the one for PUBLIC must not say in schema.
Supabase's linter added two WARN-level lints for this in April 2026, one per API role: 0028_anon_security_definer_function_executable (Public Can Execute SECURITY DEFINER Function) and 0029_authenticated_security_definer_function_executable (Signed-In Users Can Execute SECURITY DEFINER Function). Fix them together: a revoke aimed at one role leaves the other firing.
For the other warnings the Security Advisor shows next to these two, see every Security Advisor warning explained.
It's a common gap: in our study of 844 public Supabase projects, 19% had at least one definer function the API roles could call that never checked who was calling.
What does "Public Can Execute SECURITY DEFINER Function" mean?
It means a function in an API-exposed schema runs with its owner's privileges, and anon holds EXECUTE on it. The lint's SQL checks prosecdef and has_function_privilege('anon', p.oid, 'EXECUTE') in every schema listed in pgrst.db_schemas (public by default), minus Supabase's internal schemas.
"Public" means "without signing in". Supabase's database functions guide warns that a definer function created by a migration is owned by postgres, which bypasses RLS, and that any role can call it by default, "including anon". Its advice since October 2026 has two parts: check ownership in the function body, because a grant decides who can call it but not which rows they get back, and narrow the execute privilege by revoking it "from public and from each role holding a direct grant". Anyone with your publishable key can call supabase.rpc('get_order_total', …) and read data RLS would hide. 0029 runs the same check against authenticated, which under open signup means anyone with an email address.
Why does the warning come back after I revoked from anon?
Because anon still gets EXECUTE through PUBLIC. Postgres grants EXECUTE on every new function to PUBLIC, which every role belongs to, and the REVOKE docs are explicit that revoking from one role doesn't stop it using a privilege PUBLIC still has. Separately, Supabase's Securing your API page says that on existing projects, functions created in public also receive EXECUTE for anon, authenticated and service_role directly.
The usual first attempt:
-- Security Advisor: "Public Can Execute SECURITY DEFINER Function".
-- Revoked from anon so logged-out visitors can't call it.
create or replace function public.get_order_total(p_order_id uuid)
returns numeric
language sql
security definer
as $$
select total from public.orders where id = p_order_id;
$$;
revoke execute on function public.get_order_total(uuid) from anon;
- Warningsupabase/migrations/20261013120000_order_total_rpc.sql:3
SECURITY DEFINER function without a fixed search_path
Pin the search_path on the function: add `set search_path = ''` to the definition (or `ALTER FUNCTION <name> SET search_path = '';`) and fully qualify every object reference inside it, e.g. public.profiles instead of profiles.
That revoke only touches anon's own grant. PUBLIC still holds EXECUTE, so has_function_privilege('anon', …) stays true and 0028 fires again; authenticated was never touched, so 0029 fires too. The body also returns the total for any order id, with no ownership check.
The scan above is real GuardLayer output for this file. It catches a different defect, the unpinned search_path (lint 0011, covered in the mutable search_path fix). The incomplete revoke doesn't produce a finding here, because GuardLayer only reports a callable definer function when it changes data, touches auth tables or secrets, or acts on a user or account id the caller passes in, and this one is a read-only lookup that returns one number. As Supabase's guide puts it, "Pinning the search_path doesn't change who can call the function."
"[B]y default, execute privilege is granted to PUBLIC for newly created functions … you must revoke the default PUBLIC privileges and then grant execute privilege selectively." — PostgreSQL docs, Writing SECURITY DEFINER Functions Safely
Triage: which findings are real
List every definer function in public with its owner, return type and effective callers, then sort each row into one of four buckets:
select p.oid::regprocedure as function,
pg_get_userbyid(p.proowner) as owner,
p.prorettype::regtype as returns,
p.proacl, -- null = default ACL: PUBLIC can execute
has_function_privilege('public', p.oid, 'EXECUTE') as public_exec,
has_function_privilege('anon', p.oid, 'EXECUTE') as anon_exec,
has_function_privilege('authenticated', p.oid, 'EXECUTE') as authed_exec
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosecdef and n.nspname = 'public'
order by p.prorettype = 'trigger'::regtype, p.proname;
1. It returns trigger. Usually handle_new_user. Supabase's managing user data guide creates it as a definer function in public with no revoke, so any project that copied it verbatim gets both warnings. It isn't reachable as an RPC: PostgREST's schema cache drops functions returning trigger (t.oid <> 'trigger'::regtype in SchemaCache.hs). Revoke anyway to clear the noise.
2. It doesn't need to bypass RLS. If the tables it reads have policies for the caller, switch it with alter function … security invoker. Both lints ignore invoker functions. The reporter of splinter#172 cut 44 warnings to 33 this way.
3. It must bypass RLS, and only your server calls it. Cron jobs, webhooks, admin scripts. Revoke from public, anon and authenticated and grant service_role explicitly, or move the function to a schema the Data API doesn't expose, which the lints don't check.
Before you revoke, check that no RLS policy calls it. Policies run as the caller, so a helper like is_admin() used inside a policy needs EXECUTE for authenticated. Revoke it and every query on that table fails with permission denied for function is_admin. Such a helper belongs in case 4, or in a private schema with EXECUTE granted to authenticated there, which keeps it off /rest/v1/rpc. To find them: select tablename, policyname from pg_policies where qual ilike '%is_admin%' or with_check ilike '%is_admin%';
4. It must bypass RLS, and users call it. Revoke from all three roles, grant authenticated back, and compare auth.uid() to the row inside the body, as in the RPC-only RLS design. 0029 keeps firing, and the splinter docs treat that case as intentional: validate the input, then suppress. All 33 functions left in #172 were this case, per the reporter's audit.
The dangerous findings take caller-chosen arguments and write where the caller can't. One variant posted on 18 Oct 2023 in the programmatically create users thread defines public.create_user(email text, password text) as SECURITY DEFINER, inserting into auth.users with email_confirmed_at set to now(). With default grants, any caller could POST /rest/v1/rpc/create_user and run that insert as the function owner. Whether postgres can still write to auth.users today is untested here; the grant is the bug either way.
The fix
For the sample, assuming orders has an owner-scoped SELECT policy (bucket 2):
create or replace function public.get_order_total(p_order_id uuid)
returns numeric
language sql
security invoker
set search_path = ''
as $$
select o.total from public.orders o where o.id = p_order_id;
$$;
revoke execute on function public.get_order_total(uuid) from public, anon, authenticated;
grant execute on function public.get_order_total(uuid) to authenticated;
A server-only definer function, and the signup trigger:
revoke execute on function public.rebuild_leaderboard() from public, anon, authenticated;
grant execute on function public.rebuild_leaderboard() to service_role;
revoke execute on function public.handle_new_user() from public, anon, authenticated;
CREATE TRIGGER requires EXECUTE on the function, and in Postgres's trigger.c that is the only check: ExecCallTriggerFunc, which fires the trigger, has none. Tested on plain Postgres, not on Supabase: apply the revoke on a branch or local stack and sign up a test user. If that fails with Database error saving new user, grant EXECUTE on the function to supabase_auth_admin, the role Supabase Auth connects as.
How do I make new functions start closed?
Put the revoke in the same migration as every create function. To stop new functions being callable at all, change the default privileges for both sources of the grant:
-- 1. The PUBLIC grant Postgres gives every new function. No "in schema" here.
alter default privileges for role postgres
revoke execute on functions from public;
-- 2. The direct grants Supabase gives anon and authenticated in public.
alter default privileges for role postgres in schema public
revoke execute on functions from anon, authenticated;
You need both. Postgres grants EXECUTE to PUBLIC through a global default, and the ALTER DEFAULT PRIVILEGES docs say a per-schema revoke "has no effect, unless it is undoing a matching GRANT". So the second statement alone leaves anon able to call every new function through PUBLIC, and the first alone leaves Supabase's direct grants.
Supabase's own snippet used to get this wrong. Until October 2026, its "Revoke default privileges" example on the Securing your API page revoked from public with in schema public, which does nothing. In discussion #45329 a user showed that anon could still execute a new function with it applied. Supabase corrected the docs on October 20, and both the functions guide and the Securing your API page now use the global form. If you copied the old snippet, run the first statement above.
Three things to know before you apply it:
- It only affects functions created afterwards, and only those created by the role you name (
postgres, which runs your migrations and the SQL editor). Existing functions keep their grants; revoke those one by one or withrevoke execute on all functions in schema public from public, anon, authenticated. - The global form covers every schema, not just
public. That's usually what you want. - Every new function now needs an explicit grant, including helpers your RLS policies call, like
is_admin(). Forget one and queries on that table fail withpermission denied for function.
The October 30, 2026 platform change doesn't do this for you. Supabase now applies the "Automatically expose new tables and functions" off setting to every existing project (how the Data API grant change works), but PUBLIC keeps EXECUTE on new functions regardless, so new definer functions are still callable until you revoke.
A 60-second self-check
# Migrations that create definer functions
grep -rliE "security[[:space:]]+definer" supabase/migrations/
# One-line function revokes that never name PUBLIC (half-fixes)
grep -rniE "revoke (execute|all)[^;]*on function" supabase/migrations/ \
| grep -viE "from[^;]*public"
Each file from the first command needs a revoke naming public; each line from the second is a half-fix. Then rerun the triage query: public_exec should be false on every row.
FAQ
Is "Public Can Execute SECURITY DEFINER Function" always a vulnerability? No. It's real when the function touches data RLS would block, using arguments the caller chooses. The splinter docs list two false positives: a deliberate public endpoint (a contact form, an anonymous vote) and a low-privilege owner.
Why is handle_new_user flagged?
It's SECURITY DEFINER in public with the default grant, and the lint doesn't filter on return type. It can't be called over RPC: PostgREST skips trigger functions, and PL/pgSQL refuses to run one outside a trigger. Revoke anyway, then test a signup.
Does CREATE OR REPLACE undo my revoke?
No. Replacing a function keeps its ownership and permissions. Dropping and recreating it doesn't: the new function starts with default privileges, PUBLIC included.
Does alter default privileges in schema public revoke execute on functions from anon, authenticated close new functions?
Not on its own. It removes Supabase's direct grants, but PUBLIC still gets EXECUTE through Postgres's global default, and anon belongs to PUBLIC. Add alter default privileges for role postgres revoke execute on functions from public;, without in schema.
Can GuardLayer detect this?
Partly. It flags SECURITY DEFINER functions in committed SQL that don't pin search_path, and it follows function grants and revokes across your migrations to flag definer functions the API roles can still call when they change data, touch auth tables or secrets, or act on an id the caller chooses, without checking auth.uid(). A read-only lookup like the sample isn't flagged. The triage query covers every function.
See which of your Supabase tables anyone can read, free
Connect your Supabase project read-only. GuardLayer checks RLS, policies, grants and functions as they are right now, and gives you the SQL to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.