Short answer: every function in your Supabase public schema can be called by anyone through POST /rest/v1/rpc/<name> with your public anon key, unless you revoke EXECUTE from anon and public. A function marked security definer runs with the privileges of its owner (usually postgres), so it skips row level security entirely. Keep definer functions out of exposed schemas where you can, check auth.uid() inside them, set search_path = '', and never trust a user_id passed in as an argument.
RLS gets most of the attention in Supabase security reviews, and for good reason. But AI coding tools like Lovable, Bolt, v0, Cursor and Replit often "fix" a permission error by writing a Postgres function with security definer. The error goes away because the function no longer goes through RLS at all. This guide explains what that means and how to check your project.
How supabase.rpc() reaches your functions
Supabase exposes your database through PostgREST. Tables in the public schema become REST endpoints, and so do functions:
// browser code, using the public anon key
const { data, error } = await supabase.rpc("get_team_invoices", { team_id: "..." });
That call is just an HTTP request:
curl -X POST "https://YOUR_PROJECT.supabase.co/rest/v1/rpc/get_team_invoices" \
-H "apikey: YOUR_ANON_KEY" \
-H "Content-Type: application/json" \
-d '{"team_id": "any-team-id-i-want"}'
Your anon key ships in your frontend bundle, so anyone can make this request, logged in or not, with whatever arguments they like. Whether it succeeds depends on two things: who has EXECUTE on the function, and whose privileges the function runs with.
By default, Postgres grants EXECUTE on new functions to PUBLIC, and Supabase's default privileges also grant it to the anon, authenticated and service_role roles. So a new function in public is callable by everyone unless you change that.
Security invoker vs security definer
security invoker (default) |
security definer |
|
|---|---|---|
| Runs as | The caller (anon or authenticated) |
The function's owner, often postgres |
| RLS applies | Yes, the caller's policies | No, in practice (owner bypasses it) |
| Safe to expose | Usually, RLS still protects data | Only with checks inside the function |
An invoker function is mostly as safe as the tables it touches. If RLS is on and your policies are right, the function can't read more than the caller could read directly.
A definer function is different. It's meant for cases where you deliberately need to do something the caller can't, like creating a profile row when a user signs up or counting rows across all users. Inside it, RLS doesn't protect you. The function body is your only access control.
The vulnerable pattern AI tools write
Here's a typical example. The app gets "permission denied" or "new row violates row-level security policy", and the generated fix looks like this:
-- vulnerable
create or replace function public.delete_post(post_id uuid)
returns void
language sql
security definer
as $$
delete from posts where id = post_id;
$$;
Any visitor can now delete any post by calling rpc("delete_post", { post_id }) with someone else's id. Nothing checks who owns the post.
An even more common variant takes the user id as an argument:
-- vulnerable: trusts the caller to say who they are
create or replace function public.get_my_orders(p_user_id uuid)
returns setof orders
language sql
security definer
as $$
select * from orders where user_id = p_user_id;
$$;
The frontend passes the logged in user's id, but an attacker can pass anyone's id and read their orders.
The fixed version
create or replace function public.delete_post(post_id uuid)
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
if auth.uid() is null then
raise exception 'not authenticated';
end if;
delete from public.posts
where id = post_id
and author_id = auth.uid();
end;
$$;
revoke execute on function public.delete_post(uuid) from public, anon;
grant execute on function public.delete_post(uuid) to authenticated;
What changed:
auth.uid()instead of an argument.auth.uid()reads the user id from the verified JWT on the request, so the caller can't fake it. Dropp_user_idstyle parameters completely.- An ownership condition in the query itself. The
whereclause limits the delete to the caller's own rows. set search_path = ''. A definer function with a mutable search path can be tricked into running objects the caller created with the same name. With an empty search path, you write fully qualified names likepublic.posts. Supabase's Security Advisor flags this as "Function Search Path Mutable".- Explicit grants. Revoke from both
publicandanon(revoking only fromanonisn't enough whilePUBLICstill has it), then grant only to the roles that need it.
Better still, ask whether the function needs to be a definer at all. For delete_post, a plain security invoker function plus an RLS delete policy (using (author_id = auth.uid())) does the same job with less risk. See the Supabase RLS checklist for policy examples.
Keep internal functions out of the API
Trigger functions and helpers that only the database itself should call don't need to be reachable over HTTP. Put them in a schema that isn't exposed through the API:
create schema if not exists private;
create or replace function private.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;
$$;
create trigger on_auth_user_created
after insert on auth.users
for each row execute function private.handle_new_user();
Only the schemas listed under your project's API settings ("Exposed schemas", public and graphql_public by default) are served by PostgREST, so a function in private can't be called with rpc().
Views bypass RLS too
The same problem shows up with views. By default a Postgres view runs with the privileges of its owner, so a view over a protected table can expose every row:
-- returns every user's data to anyone who can select from the view
create view public.order_summary as
select user_id, sum(total) as total from public.orders group by user_id;
On Postgres 15 and later (current Supabase projects), make the view respect the caller's RLS:
alter view public.order_summary set (security_invoker = true);
Supabase's Security Advisor reports these as "Security Definer View". Fix every one it lists unless you're sure the view should be public.
Audit your project in five minutes
Run these in the Supabase SQL editor.
List security definer functions in exposed schemas and who can run them:
select
n.nspname as schema,
p.proname as function,
pg_get_function_identity_arguments(p.oid) as args,
has_function_privilege('anon', p.oid, 'execute') as anon_can_execute,
p.proconfig as settings
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosecdef
and n.nspname in ('public', 'graphql_public')
order by 1, 2;
For each row, ask:
- Does it need to be
security definer? If not,alter function ... security invoker;. - Should
anonbe able to run it? Ifanon_can_executeis true and the answer is no, revoke it. - Does
settingsincludesearch_path=""? If not, addset search_path = ''and qualify table names. - Does the body check
auth.uid(), and does it take any user id, team id or role as a parameter it trusts?
List every function anon can call:
select n.nspname, p.proname
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
and has_function_privilege('anon', p.oid, 'execute');
Then open the Security Advisor (under Advisors in the Supabase dashboard) and work through anything about functions and views.
Checklist
- No
security definerfunction trusts a user id, email or role from its arguments - Every definer function checks
auth.uid()(and role, if it's an admin action) itself - Every definer function sets
search_path = '' -
EXECUTErevoked frompublicandanonwhere logged out users shouldn't call it - Trigger and helper functions live in a non-exposed schema
- Views over protected tables use
security_invoker = true - Security Advisor shows no function or view warnings
If you've been seeing RLS errors and the suggested fix was a definer function, read how to fix "new row violates row-level security policy" first. The right fix is usually a policy, not a bypass. And if server code decides who the user is, make sure it uses getUser() or getClaims(), not getSession().
Get an automated check
PreflightPR reviews AI-built apps for Supabase functions and views that skip RLS, tables without policies, leaked service role keys and other launch blockers. Run a free scan on a public repo for a launch score and a prioritized fix list, or install the PreflightPR GitHub App to catch risky migrations in every pull request before they merge. For the full list of things to check before you ship, see the pre-launch security checklist for AI-built apps.