Deutsch
Fix libraryCriticalSupabase · Database functions

SECURITY DEFINER function without an auth check or search_path: lock it down

A SECURITY DEFINER function runs with its owner’s rights and skips RLS. If anyone can call it and it trusts its arguments, it is an open admin endpoint.

By Tim GeithnerPublished 3 min read

ShipRely's Database & Supabase audit checks for this today.

Short answer

A function marked security definer runs with the rights of its owner, which in Supabase usually bypasses row level security, and every role can call new functions by default. If such a function takes a user ID or an amount from its caller and never checks who is calling, any visitor can use it to change any account. Revoke execute from the public roles, check the caller inside the function, set search_path to an empty string and qualify every name.

Ask an AI about this page:ChatGPTClaude

On this page

A function marked security definer runs with the rights of the role that created it, not of the caller. In Supabase that owner usually bypasses row level security, and any role may call a new function by default, including signed-out visitors. If the function takes an account ID or an amount from its caller and never checks who is calling, it is an admin endpoint open to the internet. It is rated critical because one rpc() call can change any user’s data, balance or plan.

Why it matters

This is the function from one of our test apps, a report service that sells credit packs. After checkout, the browser lands on a success page that calls it:

VulnerableThe vulnerable function
create function public.add_credits(_user uuid, _amount int)
returns integer
language plpgsql
security definer
as $$
declare
  _balance integer;
begin
  insert into credit_accounts (user_id, balance)
  values (_user, _amount)
  on conflict (user_id) do update
    set balance = credit_accounts.balance + excluded.balance,
        updated_at = now()
  returning balance into _balance;

  return _balance;
end;
$$;

grant execute on function public.add_credits(uuid, integer) to authenticated;

The credit_accounts table itself is locked down well: users can only read their own balance. But the function writes with its owner’s rights, so RLS never sees the change. It trusts _user and _amount completely and never asks whether a payment happened. Any signed-in user can call supabase.rpc("add_credits", { _user: theirId, _amount: 1000000 }), and because PostgreSQL grants execute on new functions to PUBLIC, so can visitors who never signed in. Our audit engine found this in our test apps and reported it as a critical finding, pointing at the function and at the success page that triggers it from the browser.

The second weakness is smaller but common: the function has no fixed search_path and writes to credit_accounts without a schema. PostgreSQL’s docs warn that a definer function should only search schemas that untrusted users cannot write to, so that nobody can slip in a look-alike table or function (CWE-426 (opens in a new tab)).

How to find it in your app

The Security Advisor flags these as lint 0011 (function_search_path_mutable), 0028 (definer function callable without signing in) and 0029 (callable by signed-in users). To see all of them at once, run this in the SQL Editor:

SECURITY DEFINER functions and who can call them
select p.proname as function,
       pg_get_function_identity_arguments(p.oid) as args,
       p.proconfig as settings,
       has_function_privilege('anon', p.oid, 'execute') as visitors_can_call,
       has_function_privilege('authenticated', p.oid, 'execute') as users_can_call
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where p.prosecdef
  and n.nspname = 'public'
order by 1;

Every function with true in a “can call” column is reachable through supabase.rpc(). Open each one and look for the line that ties its arguments to auth.uid(). If there is none and it takes an ID, an amount, a role or a plan as an argument, it needs the fix below. settings should contain search_path="".

How to fix it

Credits after a payment should be added by your payment webhook, never by the browser. So the function becomes server-only: only the service role may call it, it checks its input, and it uses fully qualified names with an empty search_path:

SafeFix: server-only, validated, fixed search_path
create or replace function public.add_credits(_user uuid, _amount int)
returns integer
language plpgsql
security definer
set search_path = ''
as $$
declare
  _balance integer;
begin
  if _amount is null or _amount <= 0 then
    raise exception 'amount must be positive';
  end if;

  insert into public.credit_accounts as a (user_id, balance)
  values (_user, _amount)
  on conflict (user_id) do update
    set balance = a.balance + excluded.balance,
        updated_at = now()
  returning a.balance into _balance;

  return _balance;
end;
$$;

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

Revoking from public matters: that is where PostgreSQL’s default grant lives, so revoking only from authenticated would leave it callable. Then remove the rpc("add_credits", …) call from the success page and make your Stripe webhook call it with the secret key after verifying the signature (see Stripe webhook without signature check).

If a definer function is meant to be called by users, the rule is different: take the user from (select auth.uid()) inside the function instead of from an argument, raise an error when it is null, and check that the caller may touch the row before changing it. The SECURITY DEFINER guide walks through that pattern, and the credits fix covers the tables around it.

Lock down a SECURITY DEFINER function

The function public.add_credits(_user uuid, _amount integer) is SECURITY DEFINER, callable by anon and authenticated, and adds any amount to any account without checking the caller or a payment. Add a new Supabase migration that recreates it with set search_path = ‘’, schema-qualified table names and a check that rejects null or non-positive amounts; revokes execute on it from public, anon and authenticated; and grants execute only to service_role.

Remove the call to add_credits from the browser (the payment success page). Make the Stripe webhook edge function call it with the secret key after verifying the Stripe signature, using the user and amount from the verified event. List every other SECURITY DEFINER function in the public schema that anon or authenticated can execute, and say for each whether it checks auth.uid().

Check that the fix works

Act as a signed-in user and try to add credits:

Check as a user
begin;
set local role authenticated;
set local request.jwt.claims = '{"sub": "22222222-2222-2222-2222-222222222222", "role": "authenticated"}';
select public.add_credits('22222222-2222-2222-2222-222222222222', 1000000);
rollback;

With a real user ID, this must fail with permission denied for function add_credits. Replace authenticated with anon in the second line and remove the third: the same error. Run the check query again: add_credits must show false in both “can call” columns and search_path="" in settings. Finally, buy a credit pack in Stripe’s test mode and confirm that the webhook, not the success page, adds the credits.

Frequently asked questions

Why would a function use security definer at all?

Because it lets the function work regardless of the caller’s RLS, which is handy when a policy blocks something. That is also the danger: the function then decides on its own who may do what, and if it doesn’t check, nobody does.

Is setting search_path enough to make a definer function safe?

No. It protects the function from look-alike tables and functions, but it does not change who can call it or what it does for them. The caller check and the execute grants matter more.

Can anonymous visitors really call my function?

By default, yes. PostgreSQL grants execute on new functions to PUBLIC, and Supabase’s docs say any role can call a new function, including anon. Functions in the public schema are reachable through the Data API with supabase.rpc().

Should I switch the function to security invoker instead?

If the caller’s own RLS policies already allow what the function does, yes: invoker is the default and the safer choice. Keep definer only for work the caller must not do directly, and then check the caller inside the function.

Sources

  1. 1.Database functions — Supabase Docs
  2. 2.Performance and Security Advisors — Supabase Docs
  3. 3.CREATE FUNCTION: Writing SECURITY DEFINER Functions Safely — PostgreSQL Documentation
  4. 4.Privileges — PostgreSQL Documentation
  5. 5.CWE-862: Missing Authorization — MITRE
  6. 6.CWE-426: Untrusted Search Path — MITRE

Find this in your own app

ShipRely reads your repository (read-only), proves each finding with file and line, and gives you a fix prompt to paste into your AI builder.

Run a demo audit
  • Read-only access to your repository
  • Every finding with file and line
  • Fix prompts for Lovable, Cursor, Claude Code and Bolt