Skip to content

Guides · VibeVigil

Can Anyone Call My Supabase Database Function? How to Check and Lock Down RPC Functions

Your Lovable, Bolt or Cursor app calls Supabase functions with .rpc(). By default, anyone with your public key can call them too. Here's how to list your functions, spot the risky 'security definer' ones, run the stranger test, and lock them down with a few lines of SQL, plus a prompt for your coding agent.

Not sure your app is safe? Run a free scan first.
VibeVigil checks your live app for exposed keys, Supabase tables a stranger can read, missing security headers, SSL problems and downtime. Every issue comes with a ready-to-paste prompt for your coding agent.
Scan my app for free → (only scan apps you own)

Can Anyone Call My Supabase Database Function?

Your AI tool hit a wall with Row Level Security (RLS). Maybe an update failed, or a count came back wrong. So it wrote a database function and called it from the app with supabase.rpc('something'). It worked.

Here is the question to ask next: who else can call that function?

The short answer surprises a lot of people: by default, anyone can. That includes visitors who never logged in. This guide shows how to check your functions in about 10 minutes, and how to fix the risky ones.

The short answer

  1. Every function in your public schema is part of your API. Supabase lets apps call them at /rest/v1/rpc/<function_name>. Your public key is enough.
  2. By default, any role can run a function. That includes anon, the role for logged-out visitors (Supabase: Database Functions).
  3. "Security definer" functions skip RLS. A function made in the dashboard or by a migration is usually owned by postgres. If it runs as security definer, it runs with that owner's power, and RLS does not stop it.
  4. So a definer function anyone can call is a back door. It can read or change rows that your RLS policies were supposed to protect.
  5. The fix is small: check the user inside the function, pin its search_path, and only let the right roles call it.

Why AI tools write these functions

AI coding tools reach for functions for good reasons:

  • To do several steps at once (for example, "create the order and lower the stock").
  • To give logged-out users one narrow action, like "upload a proof with this link" instead of full table access.
  • To work around an RLS error.

That last one is the risky one. If RLS blocks something, the quick "fix" is a security definer function. It works because it ignores RLS. If the function doesn't check who is calling, it now ignores RLS for everyone.

Invoker vs definer, in plain words

  • Security invoker (the default): the function runs as the person calling it. Your RLS policies still apply. This is the safe choice for most functions.
  • Security definer: the function runs as the person who created it. If that's postgres, RLS doesn't apply inside it.

Supabase's own advice: prefer security invoker, and when you do use security definer, set the search_path (Supabase: Database Functions).

Step 1: Check the Security Advisor

In the Supabase dashboard, open Advisors → Security Advisor. Look for these warnings (Supabase: Database Advisors):

  • 0028 anon_security_definer_function_executable: a security definer function that can be called without signing in.
  • 0029 authenticated_security_definer_function_executable: the same, for any signed-in user. If anyone can sign up, that's almost anyone.
  • 0011 function_search_path_mutable: a function without a fixed search_path.

Supabase notes that anon and authenticated start with the same default grants. So if you only fix the first warning, the second one usually still applies. Fix them together.

Step 2: List your functions and who can call them

Open the SQL Editor and run this. It changes nothing. It only reads.

select
  p.proname as function_name,
  pg_get_function_identity_arguments(p.oid) as args,
  case when p.prosecdef then 'definer' else 'invoker' end as runs_as,
  has_function_privilege('anon', p.oid, 'execute') as logged_out_can_call,
  has_function_privilege('authenticated', p.oid, 'execute') as signed_in_can_call
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public'
order by runs_as, function_name;

Now read the results:

  • definer + logged_out_can_call = true: look at this one first. Ask, "Should a stranger be able to run this?"
  • definer + signed_in_can_call = true: does the function check that the user owns the rows it touches?
  • invoker: lower risk, because RLS still applies. Still make sure the tables it uses have RLS on.

You can usually skip functions that return trigger. They run when a row changes, not from your app.

Step 3: Read what each risky function does

Open each definer function (Database → Functions, or ask your coding agent to show the code). For each one, answer three questions:

  1. Does it read or change data that belongs to a user? (orders, profiles, messages, files)
  2. Does it check who is calling? Look for auth.uid() in a where clause, like where user_id = (select auth.uid()).
  3. Does it trust an ID passed in by the caller? A function like get_profile(user_id uuid) that returns any profile you ask for is a leak, even if only signed-in users can call it.

Supabase says this plainly: a grant decides who can call a function. It does not limit which rows come back. Granting to authenticated still returns any user's row to every signed-in caller, unless the function checks ownership.

Step 4: The stranger test

Only do this on your own app. In a terminal, call the function with only your public key, no login:

curl -X POST "https://YOUR-PROJECT.supabase.co/rest/v1/rpc/FUNCTION_NAME"   -H "apikey: YOUR_PUBLIC_KEY"   -H "Content-Type: application/json"   -d '{}'

Add the function's arguments inside -d '{}' if it needs them.

  • You get data back, or it changes something: a stranger can do that too. Fix it now.
  • You get a "permission denied" error: good. Logged-out visitors can't call it.

Then repeat while signed in as a test user, and try IDs that belong to a second test user. If user A can read or change user B's data, the function needs an ownership check.

Step 5: Fix it

Option A: Make it an invoker function (best for most cases)

If the function doesn't truly need to skip RLS, switch it to invoker. Then your RLS policies protect it like any query:

alter function public.get_my_orders() security invoker;

Option B: Keep definer, but check the user and pin the search path

Some functions do need extra power, like the ones that update a table users can't write to directly. Write them like this:

create or replace function public.mark_order_shipped(order_id bigint)
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
  update public.orders
  set status = 'shipped'
  where id = order_id
    and vendor_id = (select auth.uid());  -- only the owner's own order

  if not found then
    raise exception 'not allowed';
  end if;
end;
$$;

Three things matter here:

  • The ownership check (vendor_id = (select auth.uid())). Without it, any caller can ship any order.
  • set search_path = ''. This stops the function from being tricked into using a look-alike table or function. With an empty path, write the schema on every table, like public.orders.
  • Only the columns you mean to change. Don't take a whole row from the caller and save it.

Option C: Limit who can call it

Take away the default "anyone can run it," then give it back only to the roles that need it (Supabase: Function privileges):

revoke execute on function public.mark_order_shipped(bigint) from public;
revoke execute on function public.mark_order_shipped(bigint) from anon;
grant execute on function public.mark_order_shipped(bigint) to authenticated;

You need both revokes. Revoking from anon alone isn't enough, because Postgres also gives every function to public (which means "every role").

If a function should only ever run from your server or an Edge Function, revoke it from authenticated too, and call it with the service role key on the server only.

Option D: Move it out of the API

Helper functions that your app never calls directly can live in a schema that isn't exposed, like private. Supabase's advisor lists this as a fix too.

Lock down future functions (optional)

To stop new functions from being open by default:

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

Careful: after this, you must grant execute on each new function to the roles that need it. That includes helper functions your RLS policies call (like an is_admin() check). If you forget, those policies will start failing with permission errors.

A special case: links for people who don't log in

Some apps give customers a link with a token instead of an account (for example, "upload your delivery proof here"). Moving that to a function is a good idea. Make sure the function:

  • Looks up the row by the token inside the function, and only touches that one row.
  • Uses long, random tokens (not order numbers or short codes).
  • Stops working when the token expires or the job is done.
  • Only changes the one or two columns it needs to.

If files are involved, also check the storage bucket. A public bucket lets anyone with a file's link open it (our storage guide).

Copy-paste prompt for Lovable, Bolt, v0 or Cursor

Review every database function in the Supabase public schema.
1. List each function, whether it is SECURITY DEFINER or SECURITY INVOKER, and whether the anon and authenticated roles can execute it.
2. Switch any function that does not need to bypass Row Level Security to SECURITY INVOKER.
3. For each function that must stay SECURITY DEFINER: add set search_path = '', use schema-qualified names (public.table), and add a check so it only reads or changes rows owned by auth.uid(). Never trust a user ID passed in as an argument.
4. Revoke EXECUTE from public and anon on every function logged-out users should not call, and grant it only to the roles that need it. Revoke from authenticated too for functions that should only run on the server.
5. Keep EXECUTE for any helper functions used inside RLS policies.
6. Show me the SQL migration before running it, and tell me which app features call each function so nothing breaks.

Quick checklist

  • [ ] Security Advisor shows no 0028, 0029 or 0011 warnings (or each one is on purpose)
  • [ ] Functions that don't need to skip RLS are security invoker
  • [ ] Every security definer function checks auth.uid() before touching user data
  • [ ] Every security definer function has set search_path = ''
  • [ ] EXECUTE is revoked from public and anon where strangers shouldn't call it
  • [ ] The stranger test returns "permission denied" for private functions
  • [ ] User A can't read or change user B's data through any function

Related guides

Sources

  • Supabase docs, Database Functions (security invoker vs definer, search_path, function privileges): https://supabase.com/docs/guides/database/functions
  • Supabase docs, Database Advisors (lints 0011, 0028, 0029): https://supabase.com/docs/guides/database/database-advisors
  • Supabase docs, Row Level Security: https://supabase.com/docs/guides/database/postgres/row-level-security
  • PostgreSQL docs, CREATE FUNCTION (SECURITY DEFINER and search_path): https://www.postgresql.org/docs/current/sql-createfunction.html

Want a check on the public side of your app? VibeVigil looks for exposed keys, open Supabase tables, headers, SSL and uptime, and gives you a fix prompt for each issue. Run a free scan → (only scan apps you own)

Scan your app free. It checks the public page of a site you own. No account.

Can Anyone Call My Supabase Database Function? How to Check and Lock Down RPC Functions · VibeVigil