LatchDog

← Guides

Supabase SECURITY DEFINER function exposed: how to find and fix it

Short answer: a SECURITY DEFINER function runs with its owner's privileges, not the caller's. In Supabase the owner is usually postgres, which bypasses Row Level Security. Postgres also grants EXECUTE on new functions to PUBLIC, and many projects still grant EXECUTE to anon and authenticated in public. If that function sits in an exposed schema, anyone with your public anon or publishable key can call it at POST /rest/v1/rpc/<name>. Prefer SECURITY INVOKER. If the browser does not need to call it, revoke EXECUTE from PUBLIC, anon and authenticated, or move it to a schema the Data API does not expose. If it must stay definer, pin search_path = '', schema-qualify every name, and check (select auth.uid()) inside the function so one caller cannot read another user's rows. Leave functions that are meant to be public callable.

Why a definer function in public is callable

Postgres describes the two modes plainly: "SECURITY INVOKER indicates that the function is to be executed with the privileges of the user that calls it. That is the default. SECURITY DEFINER specifies that the function is to be executed with the privileges of the user that owns it." (PostgreSQL: CREATE FUNCTION, read Nov 17, 2026.)

Supabase's Database functions guide states the risk for a project like yours: "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, the role that serves requests carrying no user session." (Supabase: Database functions, read Nov 17, 2026.)

Two default grants make that reachable from the Data API. Postgres: "the default privileges granted to PUBLIC are as follows: ... EXECUTE privilege for functions and procedures" (PostgreSQL: Privileges, read Nov 17, 2026.). On existing Supabase projects, new functions in public also receive EXECUTE for anon, authenticated and service_role. "Postgres grants execute to PUBLIC through a global default, and a revoke limited to one schema can't remove it." (Supabase: Securing your API, read Nov 17, 2026.)

Supabase Security Advisor puts those facts together: "PostgREST exposes it at /rest/v1/rpc/ automatically." "The function does not need to appear anywhere in the documented API for the call to work." "/rest/v1/rpc accepts any function name the role has EXECUTE on." Your client calls the same endpoint with supabase.rpc('your_function'). The RLS guide's caution is direct: "A security definer function in an exposed schema is callable over the Data API with the creator's privileges. Never create one in a schema listed under "Exposed schemas" in your API settings." (Supabase: Advisors and Supabase: Row Level Security, read Nov 17, 2026.)

This is a different trap from a view that bypasses RLS. Views are covered in Supabase views bypass RLS. Functions are callable at /rpc/, and RLS on the tables they touch does not apply while the function runs as postgres.

What Security Advisor flags

Supabase Security Advisor includes these function lints (verified in the Advisors docs on Nov 17, 2026):

LintLevelSupabase's summary
0028_anon_security_definer_function_executableWARNThis SECURITY DEFINER function is callable without signing in.
0029_authenticated_security_definer_function_executableWARNThis SECURITY DEFINER function is callable by signed-in users.
0011_function_search_path_mutableWARNUnsecured function search path

Lint 0028 fires when anon has EXECUTE on a definer function in an exposed schema. Lint 0029 is the same check for authenticated. The docs say to address both together: "In default Supabase projects anon and authenticated start with identical default-privilege grants (and the Postgres default for new functions is EXECUTE to PUBLIC), so revoking from anon alone usually leaves the same function callable by every signed-up user." In a project with open sign-up, lint 0029 can mean any throwaway account. Lint 0011 is a related warning: a function without a pinned search_path can be pointed at a caller's object. It applies to invoker and definer functions. The Advisor guide on function_search_path_mutable is also summarized in rls_disabled_in_public and related Advisor errors.

Advisor also says some findings may be intentional. A public contact-form or "count of public posts" RPC can be a definer function on purpose. Do not revoke those. Confirm they validate input and limit what they return or write, then leave them callable.

List the functions (SQL)

Run this in the SQL editor. It lists SECURITY DEFINER functions in public (add any other schema from your Data API "Exposed schemas" setting), whether anon or authenticated can EXECUTE them, the owner, and proconfig (where a pinned search_path appears). It extends the official owner query from lint 0028 with privilege and config columns.

select
  n.nspname as schema_name,
  p.proname as function_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 in ('public') -- add every exposed schema
  and p.prosecdef
order by 1, 2;

A row with anon_can_execute = true is callable with your public key and no sign-in. authenticated_can_execute = true means any signed-in user. If config does not contain search_path, lint 0011 will also flag that function. Use the args column in revoke and alter function statements; Postgres identifies a function by its argument types.

Fix 1: use SECURITY INVOKER where possible

Supabase's functions guide: "Prefer security invoker, which is also the default. When you use security definer, you must set the search_path." Advisor's fix for a function that does not need to bypass RLS:

alter function public.your_function(integer, text) security invoker;

The function still runs, but it now executes as the caller, so RLS on the tables it reads or writes applies. This is the right fix when an AI tool marked the function SECURITY DEFINER out of habit. After the change, the caller also needs the table grants and policies the function relies on, or the call fails closed with 42501.

Fix 2: revoke EXECUTE from roles that should not call it

If the browser should not call the function (admin helpers, internal jobs, one-off backfills), revoke EXECUTE. Postgres says: "by default, execute privilege is granted to PUBLIC for newly created functions... you must revoke the default PUBLIC privileges and then grant execute privilege selectively." (PostgreSQL: CREATE FUNCTION, Writing SECURITY DEFINER Functions Safely.) Supabase shows the same two-step revoke, from both PUBLIC and the role you are restricting:

revoke execute on function public.your_function(integer, text) from public;
revoke execute on function public.your_function(integer, text) from anon, authenticated;

Revoking from anon alone is not enough, which is why lints 0028 and 0029 are paired. Re-run the list query; both anon_can_execute and authenticated_can_execute should be false for a function the Data API should not serve. If a trusted server role should still call it, grant EXECUTE to that role only.

Fix 3: pin search_path = '' and schema-qualify names

Pinning search_path does not change who can call the function. It stops a caller from pointing an unqualified name at their own object and running it with the owner's privileges. Supabase: "Set search_path = '' on every security definer function and schema-qualify the names inside it." Advisor's 0011 fix is the same, and it warns: "once you set the search_path = '' all references to tables/functions/views/etc in your function's body must be qualified with a schema name." Change from orders to from public.orders, or the function fails with a missing-relation error.

create or replace function public.your_function(integer, text)
returns boolean
language plpgsql
security invoker
set search_path = ''
as $$
begin
  return exists (
    select 1 from public.orders
    where id = $1
  );
end;
$$;

If the function must remain security definer, keep set search_path = '' and the schema-qualified names, then apply the execute and ownership checks in the next section. Test the function after you pin the path. The empty path is the arrangement Supabase documents; Postgres's own safe-definer example uses a trusted schema followed by pg_temp. Either pins the path. Follow the empty-path form on Supabase so it matches lint 0011.

Fix 4: move it out of the exposed schema

The Data API only serves schemas listed under Exposed schemas (normally public). A function in a private schema is not reachable at /rest/v1/rpc/. This is the pattern Supabase uses in the RLS guide for a membership helper:

create schema if not exists private;

create function private.has_good_role()
returns boolean
language plpgsql
security definer -- will run as the creator
set search_path = '' -- every name inside must be schema-qualified
as $$
begin
  return exists (
    select 1 from public.roles_table
    where (select auth.uid()) = user_id and role = 'good_role'
  );
end;
$$;

Client code that called supabase.rpc('has_good_role') will stop working; call it from a policy, a trigger, or server-side code instead. Grant EXECUTE to the roles that should run it (including roles whose RLS policies call the helper). Do not expose the private schema in your API settings.

When a definer function is intentional

Some apps need a definer function the browser can call: a contact form that inserts into a table anon cannot otherwise write, or a public RPC that returns a count of public posts. Advisor treats those as intentional findings you can suppress after you review them. Keep them, and tighten what they do:

create or replace function public.my_order(order_id uuid)
returns setof public.orders
language sql
security definer
set search_path = ''
as $$
  select * from public.orders
  where id = order_id
    and user_id = (select auth.uid());
$$;

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

Optional: tighten default privileges, then grant back public functions

If you want new functions to start closed, Supabase documents a default-privilege change. Because EXECUTE for PUBLIC is a global default, the first statement has no in schema:

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

This is not a blanket ban. After it, grant EXECUTE on each function the app should expose, including "helper functions that your Row Level Security policies call." You can also revoke on every existing function in a schema and then grant back the public ones, but do that as a reviewed migration, not as a one-line wipe. Intentionally public RPCs should stay callable.

Test it from the outside with your anon key

Only test your own project. Use the anon or publishable key your site already ships (not a secret key):

curl -X POST "https://YOUR-PROJECT-REF.supabase.co/rest/v1/rpc/your_function"   -H "apikey: YOUR_ANON_OR_PUBLISHABLE_KEY"   -H "content-type: application/json"   -d '{}'

Add parameters in the JSON body if the function takes any. A result that includes rows or a successful write you expected to be private means the function is callable as anon with the owner's rights. After a revoke or a schema move, the Data API should refuse the call. After a switch to SECURITY INVOKER, the function still runs, but it is limited by the caller's grants and RLS, so private rows should not come back to a signed-out request. Sign in as two test users before you conclude a remaining authenticated grant is safe: an execute grant does not by itself keep one user out of another user's rows.

Paste-in fix prompt for Lovable, Bolt or Cursor

My Supabase project may have SECURITY DEFINER functions in an exposed schema
(public, or any schema listed under Exposed schemas) that anon or authenticated
can EXECUTE. Those run with the owner's privileges (usually postgres, which
bypasses RLS) and are callable at /rest/v1/rpc/<name> with the public key.
Please:
1. List every function in each exposed schema, and for each one say whether it
   is security definer, whether its search_path is pinned, and whether anon or
   authenticated can execute it (has_function_privilege).
2. For functions the browser should call and that do not need to bypass RLS:
   "alter function ... security invoker;" and "set search_path = ''", with every
   name inside schema-qualified.
3. For functions the browser does not need: "revoke execute on function ...
   from public, anon, authenticated;" or move them to a private schema that is
   not exposed, and grant execute only to the roles that should call them
   (including helpers that RLS policies call).
4. For definer functions that must stay callable: keep search_path = '',
   schema-qualify names, revoke from roles that should not call them, and check
   (select auth.uid()) inside the function for per-user data. Do not revoke
   functions that are meant to be public (for example a public contact form).
5. Do not disable RLS, do not grant all privileges to anon, and do not put a
   service_role or sb_secret_ key in client code to make anything work.
Show me the SQL migration first and explain which role can call what afterwards.
Do not apply it until I approve.

Review what the AI changes, re-run the list query, and repeat the outside call with your anon key. Related reading: is the Supabase anon key safe to expose?, Supabase RLS for vibe-coded apps, how to test Supabase RLS policies, and LatchDog vs Supabase Security Advisor. A short mention of this trap also appears in is my Lovable app secure?.

What LatchDog does here (and doesn't)

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Sources

Keep watching your app, not just checking it once

LatchDog keeps watching your live app after launch. Starter ($19/month, up to 3 apps) and Pro ($39/month, up to 10 apps) re-scan it automatically every day and after every deploy (triggered by a deploy hook from your host or CI), and email you only when something changes. Nothing to install: you verify your domain once.

Start with the free check (headers, HTTPS, leaked keys in your JavaScript, source maps, .env/.git). Open-table checks run after you verify your domain. See plans

Free, read-only public check: no sign-up, no card. Headers, HTTPS, leaked keys in your JavaScript (always masked), public source maps and exposed .env/.git files. Supabase table checks run after you verify you own the domain.

Want the open-table check too? Get my free deep scan: sign in with your email (no password) and verify you own the domain. Your first deep scan is free and adds CORS, more of your JavaScript and, if your app uses Supabase, whether anyone can read its tables and views with your public key.