</>RescueMyRepoFree scan

// guide · Supabase · RLS · database functions · Nov 27, 2026

Your Supabase function skips RLS and anyone can call it: lock down SECURITY DEFINER rpc

Short answer

  • A security definer function runs as its owner, postgres, which bypasses Row Level Security.
  • By default any role can execute it, including anon, at POST /rest/v1/rpc/<name>. Your RLS policies don't matter.
  • Revoking from anon alone isn't enough: revoke from public too, and think about authenticated.
  • Fix: switch to security invoker if you can; otherwise narrow EXECUTE and check auth.uid() inside, or move helpers to a private schema.

Why this happens

When an AI tool hits “RLS is blocking this query”, a common shortcut is a database function marked security definer. It works, because it ignores RLS:

-- What an AI tool writes when RLS "gets in the way"
create or replace function public.get_all_orders()
returns setof public.orders
language sql
security definer            -- runs as postgres: RLS is skipped
set search_path = ''
as $$
  select * from public.orders;
$$;
-- no revoke: PUBLIC, anon and authenticated can all execute it

The Supabase database functions docs spell out the problem: a definer function “can return rows the caller isn't allowed to read”; 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.” Two defaults combine here. The PostgreSQL CREATE FUNCTION docs say “execute privilege is granted to PUBLIC for newly created functions”, and Supabase's Security Advisor lint 0028 adds that Supabase also grants new functions to anon, authenticated, service_role.

The Data API does the rest. Per the same lint, PostgREST exposes the function at /rest/v1/rpc/ automatically, and the function “does not need to appear anywhere in the documented API for the call to work.” Your anon key is in your frontend by design, so:

// Anyone who opens your site has the anon key, so anyone can do this:
const { data } = await supabase.rpc("get_all_orders");

# or without your app at all (test this on your own project only)
curl -X POST "https://<project-ref>.supabase.co/rest/v1/rpc/get_all_orders" \
  -H "apikey: <anon or publishable key>" \
  -H "Content-Type: application/json" -d '{}'

Step 1: find them (1 minute)

-- SECURITY DEFINER functions in public, and who can call them
select p.proname as function,
       pg_get_function_identity_arguments(p.oid) as args,
       pg_get_userbyid(p.proowner) as owner,
       has_function_privilege('anon', p.oid, 'execute') as anon_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 n.nspname = 'public'
  and p.prosecdef
  and p.prorettype <> 'trigger'::regtype
order by 1;

Any row with anon_can_call = true is a public endpoint that runs with RLS switched off. Supabase's Security Advisor flags the same functions as anon_security_definer_function_executable (0028) and authenticated_security_definer_function_executable (0029). Trigger functions are filtered out because they can only run as triggers.

Step 2: test one with the anon key

On your own project only, run the curl above with just the anon (or publishable) key and no user session. If you get rows back, or the function changed something, a logged-out visitor can do the same. Then repeat it signed in as a brand-new test user: that's what anyone who signs up can do.

Step 3: pick the right fix

If the function only used definer rights out of habit, give RLS its job back:

-- Option 1: it never needed to bypass RLS
alter function public.get_all_orders() security invoker;

If it really must bypass RLS, narrow who can call it and make the body check who's asking. Supabase's docs: revoke “from public and from each role holding a direct grant, such as anon”, then grant to the roles that need it. PostgreSQL recommends doing the create and the revoke in one transaction so there's no window where it is open to everyone.

-- Option 2: keep definer rights, but only for the right callers
begin;
revoke execute on function public.get_my_orders() from public, anon;
-- also revoke from authenticated unless every signed-in user should call it
grant execute on function public.get_my_orders() to authenticated;
commit;

-- and make the body check who is asking
create or replace function public.get_my_orders()
returns setof public.orders
language sql
security definer
set search_path = ''
as $$
  select * from public.orders
  where user_id = (select auth.uid());
$$;

The execute grant controls who can call it, not which rows they get back. Supabase warns that granting to authenticated “still returns any user's row to every signed-in caller” unless the body checks ownership.

If it's a helper your RLS policies call (has_role, is_admin), keep it working for policies but off the API:

-- Option 3: RLS helpers don't belong on the API
create schema if not exists private;
alter function public.has_role(uuid, text) set schema private;
grant usage on schema private to authenticated;
grant execute on function private.has_role(uuid, text) to authenticated;
-- existing policies follow the moved function; write new ones as private.has_role(...)

Watch out

Revoking EXECUTE from authenticated on a helper that a policy calls makes those queries fail with a permission error. And create or replace functionkeeps the function's existing permissions (PostgreSQL docs), so re-running the definition doesn't undo a revoke, but drop + create does: it gets the defaults again.

Step 4: change the default for new functions

-- Stop future functions from being callable by default (Supabase docs)
alter default privileges revoke execute on functions from public;
alter default privileges in schema public revoke execute on functions from anon, authenticated;
-- after this, grant execute on each new function to the roles that need it,
-- including helpers your RLS policies call

Supabase's docs explain why there are two statements: Postgres grants execute to PUBLIC through a global default, and “a revoke limited to one schema has no effect on a global default.” After this, every new function is closed until you grant it.

Prompt to paste into Lovable, Cursor or Claude Code

Audit my Supabase database functions without breaking anything:
1. List every function in my SQL migrations that is SECURITY DEFINER and lives in the public schema (skip functions that return trigger). For each, show who can execute it after all later migrations: PUBLIC, anon, authenticated.
2. For each one, say whether it actually needs to bypass RLS. If not, write a migration with alter function ... security invoker.
3. If it does, write a migration that revokes execute from public and anon (and authenticated unless every signed-in user should call it), grants execute only to the roles that need it, adds set search_path = '' and checks (select auth.uid()) inside the body.
4. If it is only used by RLS policies, move it to a private schema and grant usage + execute to authenticated, and use private.<name> in policies from now on.
5. Show every place my app calls supabase.rpc() for these functions so I can confirm nothing breaks.
Do NOT disable RLS, drop functions, or use the service_role key in the browser. Show me the before/after SQL.

Check it automatically

RescueMyRepo's free scan replays your Supabase migrations in order and flags SECURITY DEFINER functions in public that anon, or every signed-in user, can still execute, with a fix prompt for each one. The same scan checks tables without RLS, views that bypass RLS, write grants to anon, users who can edit their own role, Stripe webhooks, secret keys in the frontend and unauthenticated AI routes.

Scan my repo freePublic GitHub repos · no signup · about a minute

FAQ

Can anyone really call my database function?

By default, yes. PostgreSQL grants EXECUTE on every new function to PUBLIC, and Supabase also grants it to anon and authenticated through default privileges. Supabase's docs say 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 Data API exposes it at POST /rest/v1/rpc/<name>, even if your app never calls it.

Does RLS protect the tables my function reads?

Not if the function is SECURITY DEFINER. It runs with its owner's privileges, and the owner (postgres) bypasses RLS. A SECURITY INVOKER function, which is the default, runs as the caller, so RLS still applies.

I revoked execute from anon. Am I done?

Usually not. The default grant also lives on PUBLIC, which every role inherits, and Supabase's docs tell you to revoke from both public and the role you're restricting. Supabase's advisor notes that anon and authenticated start with identical grants, so revoking from anon alone still leaves the function callable by every signed-up user. In open-signup apps that's anyone with a throwaway account.

Does set search_path = '' fix this?

No. Supabase requires it for SECURITY DEFINER functions, and it stops search-path hijacking, but its docs say pinning the search_path "doesn't change who can call the function." You still need to narrow EXECUTE or check ownership inside the body.

What about my handle_new_user trigger and has_role helper?

Trigger functions (returns trigger) can't be called directly by Postgres or the API, so they are not exposed this way. Helpers that RLS policies call, like has_role or is_admin, need EXECUTE for the roles whose queries evaluate those policies, so don't blindly revoke from authenticated. Supabase's docs say to grant EXECUTE on each new function to the roles that call it, including helpers your policies use. To keep one off the API, move it to a schema the Data API doesn't expose (for example private) and grant usage on that schema plus execute to authenticated.

Does RescueMyRepo check for this?

Yes, since Nov 27, 2026. The free scan reads your SQL migrations in order (creates, drops, security invoker changes, grants, revokes and default privileges) and flags SECURITY DEFINER functions in public that anon, or every signed-in user, can still execute. High if the function writes data or returns rows with no auth.uid() or auth.jwt() check, medium if only signed-in users can call it, and low for single-value helpers, auth-checked functions and functions you explicitly granted to anon. Trigger functions are skipped.

Sources

Behavior per the linked docs, read Nov 27, 2026.