What the warning says
Supabase's Security Advisor flags every function whose search_path is not set in its definition. Without it, the function resolves unqualified names like profiles or lower() using the search path of whoever calls it. For a normal function that is a reliability problem. For a security definer function, which runs with its owner's rights (usually postgres, which bypasses RLS on its own tables), it is a security problem.
The risk, concretely
The PostgreSQL manual (“Writing SECURITY DEFINER Functions Safely”) explains it: if the search path includes a schema an untrusted user can write to, that user can create a table, function or operator with the same name as one the function uses, and the privileged function will use theirs. The temporary schema is searched first by default and is writable by anyone who can create temp tables.
On Supabase, API users can't create objects directly, so the more common real-world problem is the second advisor warning:
Function `public.delete_user_data(uuid)` can be executed by the `anon` role as a
`SECURITY DEFINER` function via `/rest/v1/rpc/delete_user_data`.New functions are executable by everyone unless you say otherwise: Postgres grants EXECUTE to PUBLIC by default, and Supabase projects have also granted it to anon and authenticated directly. A security definer function in an exposed schema is an RLS bypass that anyone with your publishable key can call over HTTP, with whatever arguments they like.
Fix 1: pin the search path and qualify names
create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
insert into public.profiles (id, email)
values (new.id, new.email);
return new;
end;
$$;With an empty search path nothing is looked up implicitly, so every table and non-built-in function must carry its schema (public.profiles, extensions.gen_random_bytes()). For an existing function you can also run alter function public.handle_new_user() set search_path = '';, then test it, because unqualified names inside will now fail with “relation does not exist”.
Fix 2: decide who may call it
Revoking from public alone is often not enough on Supabase, because anon and authenticated can hold direct grants. Revoke from all three, then grant back only what is needed:
revoke execute on function public.delete_user_data(uuid) from public, anon, authenticated;
grant execute on function public.delete_user_data(uuid) to service_role;Trigger functions such as handle_new_user don't need EXECUTE for API roles at all; the trigger fires them. Helper functions used only inside RLS policies belong in a non-exposed schema (private), so they can't be called over HTTP.
Fix 3: ask whether it needs to be definer at all
If the function only reads or writes rows the caller is allowed to touch anyway, make it security invoker (the default). It then runs under the caller's RLS, and the bypass question disappears. Keep security definer for the few cases that truly need to cross RLS, and inside them check auth.uid() or the caller's role yourself before doing anything.
How to verify
select p.oid::regprocedure as function,
p.prosecdef as security_definer,
p.proconfig as settings,
has_function_privilege('anon', p.oid, 'execute') as anon_can_execute
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
order by security_definer desc;Every security_definer = true row should show search_path= in settings, and anon_can_execute should be false unless signed-out visitors are really meant to call it. Rerun the Security Advisor: both warnings are gone.
Related guides
- Supabase “infinite recursion detected in policy for relation” – cause and fix
- “relation does not exist” in Postgres and Supabase – schema, quotes, search_path, cache
- Supabase rpc “Could not find the function … in the schema cache” and “function does not exist”
- Postgres “new row for relation … violates check constraint” – find the rule and fix it
More in Postgres errors and performance · all guides