AleannLab

Can Anyone Call Your Supabase Functions? Checking RPC Access for anon

AleannLab team2 min read

Probably yes. Postgres grants execute on every new function to everyone by default, and Supabase exposes functions in the public schema as HTTP endpoints at /rest/v1/rpc/<name>. If a function is marked security definer, it also skips your RLS policies. Anyone with your public key can call it without logging in, unless you revoked that right.

On this page
  1. What a call looks like
  2. Two things make a function dangerous
  3. List what anon can call
  4. Lock it down
  5. Write the function so access does not depend on arguments
  6. Keep helpers out of the API
  7. Test it

Tables get most of the attention in Supabase security guides. Functions are the back door. AI tools create them freely: increment_credits, get_user_stats, make_admin, delete_account. Each is a public API endpoint the moment the migration runs.

What a call looks like

No SDK is needed. This works from any terminal with the key that is already in your JavaScript bundle:

bash
curl -X POST 'https://<project>.supabase.co/rest/v1/rpc/increment_credits' \
  -H "apikey: <publishable-or-anon-key>" \
  -H "Content-Type: application/json" \
  -d '{"target_user": "8f1c...", "amount": 100000}'

If the function trusts its arguments, the caller just gave someone 100,000 credits.

Two things make a function dangerous

1. Who may execute it. Unless you change it, the answer is everyone, including the anon role used for visitors who are not logged in.

2. Whose rights it runs with. A function is security invoker by default: it runs as the caller and RLS applies. A security definer function runs as its owner and RLS does not apply. Assistants reach for security definer whenever a policy gets in the way, because it makes the error go away.

A security definer function that anon can execute, and that takes a user ID as an argument, is an unauthenticated admin API.

List what anon can call

SQL
select p.proname as function_name,
       pg_get_function_identity_arguments(p.oid) as arguments,
       p.prosecdef as security_definer
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')
order by p.prosecdef desc, p.proname;

Rows with security_definer = true come first. Read each of those line by line.

Lock it down

Revoke from both public (the Postgres word for "every role") and anon, then grant to the roles that need it:

SQL
revoke execute on function public.increment_credits(uuid, integer) from public, anon;
grant execute on function public.increment_credits(uuid, integer) to service_role;

Revoking from anon alone does nothing while public still has the right.

For functions you create later, change the defaults once:

SQL
alter default privileges for role postgres
  revoke execute on functions from public;

alter default privileges for role postgres in schema public
  revoke execute on functions from anon;

The first statement has no in schema on purpose. Postgres only lets you remove the built-in "everyone can execute" default globally.

After that, every new function needs an explicit grant execute. The same idea as Supabase's change for tables, described in our post on the 42501 "permission denied" error.

Write the function so access does not depend on arguments

Never accept "which user" as a parameter for an action on the caller's own data. Read it from the session:

SQL
create or replace function public.redeem_coupon(coupon_code text)
returns void
language plpgsql
security definer
set search_path = ''
as $$
declare
  caller uuid := (select auth.uid());
begin
  if caller is null then
    raise exception 'not authenticated';
  end if;

  update public.coupons
  set redeemed_by = caller, redeemed_at = now()
  where code = coupon_code and redeemed_by is null;

  if not found then
    raise exception 'invalid coupon';
  end if;
end;
$$;

revoke execute on function public.redeem_coupon(text) from public, anon;
grant execute on function public.redeem_coupon(text) to authenticated;

Three details matter:

  • auth.uid() identifies the caller. The client cannot fake it.
  • set search_path = '' stops the function from resolving names through a schema an attacker can write to. With an empty path, every table is written with its schema, as above.
  • The function does one narrow thing. A general-purpose run_admin_action(action, payload) is hard to make safe.

Keep helpers out of the API

Trigger functions and internal helpers never need to be called over HTTP. Put them in a schema that is not exposed:

SQL
create schema if not exists private;
alter function public.recalculate_totals() set schema private;

Functions in private have no RPC endpoint at all.

Test it

Call each remaining function three ways: with no session, as user A acting on user A's data, and as user A passing user B's IDs. Only the second should succeed.

Function access is part of check 2 in our security checklist for vibe-coded apps. If you want the full pass done for you, with fixes, see our app rescue and security audit.

All articles →

Gotowy, aby omówić swój projekt z nami?

idea
*
*
*
Interesuje mnie...
Tworzenie stron internetowych
Tworzenie aplikacji
Projektowanie UX/UI
DevOps
Sztuczna inteligencja i uczenie maszynowe
Testowanie QA
Tworzenie aplikacji mobilnych
Konsultacja
Outsourcing
Budżet projektu (USD)
≤ $5K
$10K-$20K
$30K-$40K
$50K-$60K
>$100K
Prześlij plik