VibeCPR

← All fixes

Supabase · RLS · RPC functions

Fix a Supabase SECURITY DEFINER function anyone can call through /rest/v1/rpc

Updated · Applies to: Supabase, Lovable, Bolt, v0, Cursor, Replit, Claude Code

TL;DR fix: A function declared security definer runs with its owner's rights, so it reads and writes past your RLS policies. Postgres gives EXECUTE on every new function to PUBLIC, and Supabase also grants it to anon and authenticated, so anyone with your public key can call it at POST /rest/v1/rpc/function_name. Fix: revoke execute on function public.f(...) from anon, authenticated, public;, or switch it to security invoker if it doesn't need to skip RLS. To stop new functions getting the grant, run alter default privileges revoke execute on functions from public; without in schema. The per-schema version alone does not remove the PUBLIC grant (we tested it).

Steps

  1. List every SECURITY DEFINER function and who can call it. Run the audit query below in the SQL editor. Any row with anon_can_execute = true is callable by anyone with your public key at /rest/v1/rpc/<name>. Any row with authed_can_execute = true is callable by any signed-up user, which on an app with open signup is still the whole internet.
  2. Decide what each function is for. Admin helpers, cron jobs, webhooks and backfills should never be callable from the browser. A small number of functions are meant to be public, like a contact form or a vote counter, and those need input validation and a narrow job.
  3. Revoke EXECUTE from anon, authenticated and PUBLIC. For functions the browser should not call, revoke execute from anon, authenticated and public. You must include public, because that is where Postgres keeps its own default grant. Call admin functions from a server or Edge Function with the service_role key instead.
  4. Switch to SECURITY INVOKER where it doesn't need to skip RLS. If a function was marked security definer by habit and only touches rows the caller is already allowed to see, alter function ... security invoker. RLS on the underlying tables then applies, and you can grant execute to authenticated safely.
  5. Stop new functions getting the grant. Run alter default privileges revoke execute on functions from public (global, no 'in schema') plus the per-schema revoke from anon and authenticated. Then every new function needs a deliberate grant.
  6. Retest with only the anon key, then re-run Security Advisor. POST to /rest/v1/rpc/<name> with only your anon key. It should now return a permission error. Re-run Security Advisor and clear lints 0028 and 0029, or accept them knowingly for functions that are meant to be public.

Why this happens

AI coding tools love Postgres functions. Ask for “an admin button that resets the demo data” or “a leaderboard that counts everyone’s points” and you often get a function marked security definer, because that’s the quickest way past an RLS error. Two defaults then line up against you:

  1. SECURITY DEFINER skips RLS. The function runs as its owner, normally postgres, so your policies don’t apply inside it.
  2. Everyone can call it. Postgres grants EXECUTE on new functions to PUBLIC, and Supabase’s own docs say projects also grant default privileges on new functions to anon, authenticated and service_role. PostgREST serves any function the caller can execute at POST /rest/v1/rpc/<name>. It doesn’t need to be called anywhere in your frontend.

So a helper you meant for yourself becomes a public endpoint. Supabase’s Security Advisor now flags it as “This SECURITY DEFINER function is callable without signing in” (lint 0028) and “callable by signed-in users” (lint 0029).

Reproduce it in under a minute

Run this on a throwaway project, never production. We ran the same steps on Postgres 18 to check every result shown.

create table public.orders (
  id serial primary key,
  user_id uuid not null,
  total numeric,
  email text
);
alter table public.orders enable row level security;   -- no policies: anon sees nothing
insert into public.orders (user_id, total, email) values
  (gen_random_uuid(), 10, '[email protected]'),
  (gen_random_uuid(), 20, '[email protected]'),
  (gen_random_uuid(), 30, '[email protected]');

-- The "admin helpers" an AI tool might write
create function public.get_all_orders() returns setof public.orders
  language sql security definer set search_path = ''
  as $$ select * from public.orders $$;

create function public.reset_orders() returns void
  language sql security definer set search_path = ''
  as $$ delete from public.orders $$;

Now act as someone holding only your public key:

set role anon;
select count(*) from public.orders;            -- 0  (RLS works on the table)
select count(*) from public.get_all_orders();  -- 3  (the function skips RLS)
select public.reset_orders();                  -- runs: every order is deleted
reset role;

Over HTTP that last line is POST /rest/v1/rpc/reset_orders with the anon key.

Audit every function in your project

select
  p.proname as function_name,
  pg_get_function_identity_arguments(p.oid) as args,
  pg_get_userbyid(p.proowner) as owner,
  p.prosecdef as security_definer,
  has_function_privilege('anon', p.oid, 'execute') as anon_can_execute,
  has_function_privilege('authenticated', p.oid, 'execute') as authed_can_execute
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
order by security_definer desc, function_name;

Any row with security_definer = true and anon_can_execute = true is the urgent one. security_definer = true with authed_can_execute = true is next, because anyone can sign up.

The fix, copy-paste

Use the exact argument list from the audit (args). Put it in a migration so it’s reviewable.

-- 1. The browser should never call admin helpers
revoke execute on function public.reset_orders() from anon, authenticated, public;
revoke execute on function public.get_all_orders() from anon, authenticated, public;

Re-run the attack:

set role anon;
select count(*) from public.get_all_orders();  -- ERROR: permission denied for function get_all_orders
reset role;

Revoking only from anon is not enough. In our test authenticated could still call the function until we revoked from it too, and the Postgres default sits on public.

If the function doesn’t actually need to skip RLS, make it run as the caller and grant it back to signed-in users:

alter function public.get_all_orders() security invoker;
grant execute on function public.get_all_orders() to authenticated;
-- A signed-in user now gets only the rows their RLS policies allow (0 here, since there are no policies)

If the frontend really needs a privileged function (a public vote counter, a contact form), keep it security definer, set search_path = '', check every argument, have it do one narrow thing, and accept the Advisor finding knowingly.

Stop new functions getting the grant

This is the part almost every guide gets slightly wrong. The per-schema statement looks right but doesn’t remove the built-in PUBLIC grant:

-- Not enough on its own: new functions are still executable by anon via PUBLIC
alter default privileges in schema public revoke execute on functions from anon, authenticated, public;

PostgreSQL’s docs explain why: per-schema default privileges can only add to the global defaults. Run the global form as well (in the SQL editor you’re the postgres role, which owns the functions you create there):

alter default privileges in schema public revoke execute on functions from anon, authenticated;
alter default privileges revoke execute on functions from public;

In our Postgres 18 test, a new function created after these two lines was not executable by anon or authenticated, and service_role kept its grant. From then on, every function needs a deliberate grant execute ... to authenticated.

Added a helper to fix infinite recursion detected in policy? Keep it in a schema the API doesn’t expose, with no arguments and no anon grant: Fix “infinite recursion detected in policy for relation” (42P17).

Related holes that skip RLS the same way: views that bypass RLS and policies with USING (true). For the table grant change enforced from Oct 30, 2026, see Supabase’s Oct 30, 2026 change explained.

Prompt for Cursor, Lovable, Bolt or Claude Code

Audit every function in the public schema of my Supabase project. Assume the anon key
is public and anyone can sign up. For each function report: owner, SECURITY DEFINER or
INVOKER, whether anon / authenticated can EXECUTE it, and which part of my frontend calls it.
Then in one migration: revoke EXECUTE from anon, authenticated and public on every function
the browser doesn't call; switch functions that don't need to bypass RLS to SECURITY INVOKER
and grant them to authenticated only; set search_path = '' on any remaining SECURITY DEFINER
function. Also add: alter default privileges revoke execute on functions from public;
Show me the audit table and the SQL before running anything.

If a function was already called by someone else: check your Postgres and API logs for /rest/v1/rpc/ requests, restore from a backup or point-in-time recovery if data changed, and rotate any keys the function could read.

When to get help

Get help if an AI tool generated dozens of RPC functions and you don't know which ones your frontend calls, if a function was already called by someone else and you need to check what changed, or if you want a full review before launch.

FAQ

Why can the anon key call my Supabase function?

Postgres grants EXECUTE on every new function to PUBLIC, a group every role belongs to, and Supabase projects also grant default privileges on new functions to anon, authenticated and service_role. PostgREST exposes any function in an exposed schema at /rest/v1/rpc/<name>, so the public anon key can call it unless you revoke EXECUTE. Supabase Security Advisor flags this as lint 0028 (anon) and 0029 (authenticated).

What does SECURITY DEFINER do in a Supabase function?

It makes the function run with the privileges of the role that owns it, usually postgres, instead of the caller. That owner is not limited by your RLS policies, so a SECURITY DEFINER function can read or delete every row in a protected table. That is useful for controlled admin jobs and dangerous when anyone can call it.

Does revoking EXECUTE from anon fix it?

Not on its own. The Postgres default grant sits on PUBLIC, and authenticated usually has its own grant too. Revoke from anon, authenticated and public together: revoke execute on function public.your_function(args) from anon, authenticated, public. Then grant it back only to the roles that really need it.

Why do new functions still get EXECUTE after I ran alter default privileges in schema public?

PostgreSQL docs say per-schema default privileges can only add to the global defaults, not remove them. The built-in PUBLIC execute grant is global, so alter default privileges in schema public revoke execute on functions from public leaves it in place. Run the global form, alter default privileges revoke execute on functions from public, with no in schema clause. We checked this on Postgres 18: only the global form stopped anon from executing a new function.

Should I use SECURITY INVOKER instead?

Usually yes. A SECURITY INVOKER function runs as the caller, so RLS still applies to every table it touches and you can grant it to authenticated without handing out extra access. Keep SECURITY DEFINER only for functions that must do something the caller cannot, set search_path = empty on them, and validate every input.

Does the Oct 30, 2026 Supabase change fix this?

No. The October 30, 2026 change is about grants on new tables in the public schema for projects created before May 30, 2026. It does not change function EXECUTE grants, and functions you already created keep the grants they have.

Sources

Related fixes