Skip to content

Can anyone call your database functions? How to check SECURITY DEFINER RPCs in a Lovable or Supabase app (and fix them)

LaunchGuard (https://getlaunchguard.app) is a weekly health check for the app you shipped from Lovable, Cursor, Bolt, or v0. The first scan is free (no card; you prove you own the app) and shows your 1–2 most severe findings, with a fix prompt for each. Paid plans add the full report, a fix prompt for every finding, a weekly rescan and an emailed report. Everything below you can do yourself in the Supabase dashboard and a terminal, for free. Only test an app you own.

Scan your first app free — create an account, add the published URL of an app you own, and prove ownership with a file at /.well-known, a meta tag, or a DNS TXT record. Then run the first scan.

TL;DR

  1. A Postgres function marked SECURITY DEFINER runs as its owner, not as the caller. On Supabase the owner is usually postgres, which bypasses Row Level Security, so the function can read or change rows the caller could not touch with a normal query. (Supabase: Row Level Security; Supabase: Database functions)
  2. New functions are callable by everyone by default. Postgres grants EXECUTE to PUBLIC on new functions, and Supabase also grants it to anon, authenticated and service_role. PostgREST then exposes the function at POST /rest/v1/rpc/<name> — it does not have to appear in your app's UI. (PostgreSQL: Privileges; PostgreSQL: CREATE FUNCTION; Supabase: Advisors, lint 0028)
  3. Check Security Advisor for lints 0028 and 0029, list your definer functions in the SQL Editor, and try calling each one signed out with only the publishable (anon) key. Then revoke EXECUTE, switch the function to SECURITY INVOKER, or move it out of an exposed schema — and pin search_path = '' on every definer function you keep.

Why this happens

Row Level Security protects tables when a client queries them with the publishable or anon key. A SECURITY DEFINER function is a side door: it runs with the privileges of the user that owns it, which is the default SECURITY INVOKER behaviour turned off. (PostgreSQL: CREATE FUNCTION)

Supabase's docs are blunt about the result: "A security definer function can return rows the caller isn't allowed to read. It runs with its creator's privileges. A function created in the Dashboard or by a migration is owned by postgres, which bypasses Row Level Security. Any role can call it by default, including anon." (Supabase: Database functions)

That default comes from Postgres itself: "the default privileges granted to PUBLIC are as follows: … EXECUTE privilege for functions and procedures". (PostgreSQL: Privileges) Postgres tells you to revoke that default and grant EXECUTE only to the roles that need it, in the same transaction that creates a definer function, so there is no window where everyone can call it. (PostgreSQL: CREATE FUNCTION)

AI builders often add SECURITY DEFINER to "fix" a permission error. The error goes away because RLS is no longer applied. The function is then reachable at /rest/v1/rpc/<name> with the public anon key that already ships in your frontend.

There is a second trap: search_path. If it is not pinned, a caller can put an unqualified name on an object they control and have the definer function run it with the owner's privileges. Supabase and Postgres both say to set search_path and to schema-qualify every name inside the function. (Supabase: Row Level Security; Supabase: Advisors, lint 0011; PostgreSQL: CREATE FUNCTION)

Step 1 — Read Security Advisor

In the Supabase dashboard, open Advisors → Security Advisor (or run supabase db advisors). Look for:

  • 0028_anon_security_definer_function_executable — "This SECURITY DEFINER function is callable without signing in."
  • 0029_authenticated_security_definer_function_executable — the same, but for any signed-in user. If signup is open, that is effectively anyone who can create an account.
  • 0011_function_search_path_mutable — the function's search_path is not pinned.

Treat 0028 and 0029 as a pair. Revoking EXECUTE from anon alone usually leaves the function callable by every signed-up user, because both roles start with the same default grants. (Supabase: Advisors, lint 0028 and 0029)

Step 2 — List the definer functions yourself

Run this in the SQL Editor. The owner query follows Supabase's own example for lint 0029; the privilege columns use Postgres's has_function_privilege.

select
  n.nspname as schema,
  p.proname as name,
  pg_get_function_identity_arguments(p.oid) as args,
  pg_catalog.pg_get_userbyid(p.proowner) as owner,
  p.prosecdef as security_definer,
  p.proconfig as config,
  has_function_privilege('anon', p.oid, 'execute') as anon_can_execute,
  has_function_privilege('authenticated', p.oid, 'execute') as authenticated_can_execute
from pg_catalog.pg_proc p
join pg_catalog.pg_namespace n on n.oid = p.pronamespace
where n.nspname not in ('pg_catalog', 'information_schema')
  and p.prosecdef
order by n.nspname, p.proname;

(Supabase: Advisors, lint 0029, "Identifying the Owner")

Red flags:

  • security_definer is true, the schema is public (or another schema exposed in your API settings), and anon_can_execute or authenticated_can_execute is true.
  • The owner is postgres or another privileged role. That is typical on Supabase, and it means the function can read and write what that role can. (Supabase: Advisors, lint 0029)
  • config does not include search_path=.

Also check Project Settings → Data API → Exposed schemas. A definer function in an exposed schema is callable over the Data API with the creator's privileges. Supabase's RLS guide says: "Never create one in a schema listed under 'Exposed schemas' in your API settings." (Supabase: Row Level Security)

Helper functions used only inside policies belong in a private, unexposed schema, with search_path = '' and schema-qualified names. (Supabase: Row Level Security)

Step 3 — Try calling one signed out (your project only)

Do this only against a project you own. Use the publishable or anon key, never the secret or service_role key.

# Replace the project URL, function name and key.
# If the function takes arguments, put them in the JSON body as
# { "arg_name": "value" }.
curl -sS -D - -o /tmp/rpc-body.txt \
  -X POST "https://<project_id>.supabase.co/rest/v1/rpc/<function_name>" \
  -H "apikey: <publishable-or-anon-key>" \
  -H "Authorization: Bearer <publishable-or-anon-key>" \
  -H "Content-Type: application/json" \
  -d '{}'
echo "---- body ----"
cat /tmp/rpc-body.txt

The client libraries call the same endpoint with supabase.rpc('function_name'). (Supabase: Database functions)

If a signed-out request returns rows, a 200, or any successful write, anyone with your public anon key can run that function. A permission error or HTTP 401/403 is what you want for a function that should not be public.

Then sign in as a normal test user (not an admin) and call it again. Lint 0029 is about that case: a throwaway account should not be able to run an admin-rights function.

Step 4 — Fix it

Decide, for each function, whether it should be callable at all, and by whom. Supabase's advisor describes three paths. (Supabase: Advisors, lint 0028 and 0029)

A. Revoke EXECUTE (the usual fix). Revoke from PUBLIC as well as from anon and authenticated, because the Postgres default grant lives on PUBLIC. Include the argument types in the name, exactly as Step 2 showed them.

revoke execute on function public.my_priv_op(int, text)
  from anon, authenticated, public;

To stop the same default applying to functions you create later:

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

The first of those two statements is not limited to one schema, because Postgres grants EXECUTE to PUBLIC through a global default. (Supabase: Database functions) Then grant EXECUTE only on the functions that should be callable:

grant execute on function public.hello_world() to authenticated;

B. Keep the function in the API, but run it as the caller. Use this when the function does not actually need to bypass RLS.

alter function public.my_priv_op(int, text) security invoker;

RLS on the underlying tables then applies again. (Supabase: Advisors, lint 0029)

C. It is meant to be a public (or signed-in) RPC. Examples: a contact form, an anonymous vote, or "create my profile" that inserts a row the user cannot otherwise insert. Leave SECURITY DEFINER and the grant in place only if the function validates its inputs and limits what it does. A definer function exposed to authenticated is a public API for anyone who can sign up. (Supabase: Advisors, lint 0028 and 0029) Still pin search_path, and check inside the function that the caller may see or change the rows it returns — an EXECUTE grant does not limit which rows a permitted role gets back. (Supabase: Database functions)

D. Pin search_path on every definer function you keep, and schema-qualify every name in the body (public.profiles, not profiles):

create or replace function public.my_priv_op(int, text)
returns /* same return type as before */
language plpgsql
security definer
set search_path = ''
as $$
begin
  -- use public.table_name, not table_name
end;
$$;

(Supabase: Row Level Security; Supabase: Advisors, lint 0011; PostgreSQL: CREATE FUNCTION)

If you use CREATE OR REPLACE FUNCTION, ownership and permissions do not change automatically. A replace is not a substitute for the REVOKE in A. (PostgreSQL: CREATE FUNCTION)

Then repeat Step 1 and Step 3.

Copy-paste fix prompt for Lovable or Cursor

Audit every Postgres function in the exposed API schemas of this
Supabase project (usually public).

1. List each function: schema, name, arguments, owner, whether it is
   SECURITY DEFINER or SECURITY INVOKER, its search_path / proconfig,
   and whether anon and authenticated have EXECUTE.
2. Flag:
   - SECURITY DEFINER functions that anon or authenticated can call,
   - definer functions whose owner is postgres or another privileged
     role,
   - any function (definer or not) with no pinned search_path,
   - definer functions that live in an exposed schema but are only
     meant as helpers for RLS policies.
3. Propose a plan, function by function: revoke EXECUTE from
   anon, authenticated and public; or switch the function to
   SECURITY INVOKER; or move a policy helper to an unexposed
   private schema. For any definer function we keep, set
   search_path = '' and schema-qualify every name in the body.
   Also revoke default EXECUTE on future functions, then grant it
   only where the app needs it.
4. Write the changes as a database migration. Show me the plan
   before you apply it, and tell me how to re-test each function
   signed out with only the publishable (anon) key against
   POST /rest/v1/rpc/<name>.

Do not put the service_role or secret key in the browser.

FAQ

I already turned on RLS. Does that protect these functions? Not if they are SECURITY DEFINER and owned by postgres. RLS applies to the table when the caller hits it. A definer function owned by a role with bypassrls does not run those policies. (Supabase: Row Level Security; Supabase: Database functions)

Security Advisor is quiet, but I have functions in public. Lint 0028 and 0029 only flag definer functions. A SECURITY INVOKER function still runs as the caller; if the tables it touches have RLS off, that is a table problem, covered by the table lints and by the RLS guide linked below.

I revoked EXECUTE from anon and the advisor still warns. Also revoke from PUBLIC and, for lint 0029, from authenticated. (Supabase: Advisors, lint 0028)

Should I drop pg_graphql? Only if your app does not use the GraphQL endpoint. Dropping it closes /graphql/v1, but the function is still callable at /rest/v1/rpc/<name>, so it is not a fix for 0028 or 0029. (Supabase: Advisors, lint 0028)

Is a public RPC always a mistake? No. A carefully limited contact form or vote can be an intentional definer function. The mistake is an admin helper, a "get all users" function, or anything that returns other people's rows, left executable by anon or by every signed-in user.

Scan your first app free

Scan your first app free. Sign up, add the published URL of an app you own, prove ownership with a file, a meta tag or a DNS TXT record, and the first scan shows your 1–2 most severe findings with a fix prompt. No card. Solo is $9 per month for 1 app; Pro is $24 per month for up to 5 apps. LaunchGuard scans only apps you own and verify.

Sources

Last checked against the sources above: 26 November 2026.

Newsletter

We send a confirmation link first. You are not subscribed until you open it, and every issue has a one-click unsubscribe.

Can anyone call your database functions? How to check SECURITY DEFINER RPCs in a Lovable or Supabase app (and fix them)