Case study: a Supabase function that trusted the tenant id it was given
#supabase#postgres#security-definer#multi-tenant#case-study
A multi-tenant app on Supabase saved products and prices through a SECURITY DEFINER function that took the tenant id as an argument and never checked that the caller belonged to that tenant. The function was executable by anon, so anyone with the public key could write products and prices into any tenant. A second function, unused by the app, could rewrite any user's score. The developer fixed both, closed several more functions with the same gap, and changed the project's defaults so new functions no longer get the API roles' direct grants.
We reported this privately in mid-November 2026, and the fix reached production within two weeks. The developer asked us not to name the project or link it, and to keep out anything that would help someone test other deployments of the same code. The function names and code below are illustrative, not the project's. The logic is the same.
What the app does
It's a multi-tenant SaaS. Each tenant has its own admins, its own catalogue of products and its own prices, and every table carries a tenant_id. RLS policies scope reads and writes to the tenant a user belongs to.
Creating a product touches several tables at once: the product, the content it unlocks, and one or more prices. To make that atomic, the app does it in one database function, called from a server action.
How did the bug work?
1. The tenant id came from the caller
The function looked roughly like this:
create or replace function public.save_listing(
p_tenant_id uuid,
p_author_id uuid,
p_listing jsonb,
p_prices jsonb
)
returns jsonb
language plpgsql security definer set search_path = public as $$
begin
insert into public.listings (tenant_id, author_id, title, ...)
values (p_tenant_id, p_author_id, p_listing->>'title', ...);
-- ...then the related rows and prices, all with p_tenant_id
end; $$;
SECURITY DEFINER runs the function as its owner, so the RLS policies that scope everything to the caller's tenant don't apply inside it. The only thing deciding which tenant gets written is p_tenant_id, and that is whatever the caller sends. The author id works the same way.
On the app's side this was fine. The server action worked out the tenant from the signed-in admin and passed it in. But the function was also reachable directly at /rest/v1/rpc/save_listing, and nobody had revoked it from the API roles. With the public anon key, anyone could call it with another tenant's id and create or change products and prices in that tenant's catalogue.
2. Why anon could call it at all
Nobody granted it to anon on purpose. On Supabase, new functions in public get EXECUTE for anon, authenticated and service_role through default privileges, so every function is callable through the API unless you take that away. The Kagelin case study walks through why revoke ... from public alone doesn't remove it.
3. A function nobody used
The second report was a function that overwrote a user's score with a new value and a note. The app had stopped calling it, but it still existed, it was still SECURITY DEFINER, it had no caller check, and it was still executable by the API roles. Unused functions don't show up when you test the app, so they're easy to miss.
What did it expose?
- Writes into any tenant's catalogue: new products, changed products and changed prices, from anyone with the public key.
- Any user's score, through the unused function.
We tested none of this against a live deployment. Both findings came from reading the SQL in the repository and tracing how the app calls it, and the developer confirmed both.
How was it fixed?
- Both functions became server-only. Execute was revoked from
public,anonandauthenticated, and granted toservice_role. The app already called the first one from the server with the service role, so nothing broke:
revoke execute on function public.save_listing(uuid, uuid, jsonb, jsonb)
from public, anon, authenticated;
grant execute on function public.save_listing(uuid, uuid, jsonb, jsonb)
to service_role;
- The developer swept for the same pattern and found several more functions with no caller check, all meant only for scheduled jobs. Scheduled jobs run as
postgres, so those were revoked from the API roles too. One report closed several more functions than it named. - The defaults changed too. After our review of the fix, the developer changed the project's default privileges so new functions in
publicno longer get Supabase's direct grants toanonandauthenticated. After a follow-up from us, they also revoked Postgres's own global grant toPUBLIC, and checkedpg_default_aclafterwards: new functions now start with no execute grant for the API roles.
That step matters most, because it fixes the cause instead of the instances. It takes two statements to make new functions start fully closed, and our first review only named the second. Postgres also grants EXECUTE on every new function to PUBLIC, which every role belongs to, through a global default that a revoke in schema public doesn't touch:
-- Postgres's own grant to PUBLIC. Must be global: no "in schema".
alter default privileges for role postgres
revoke execute on functions from public;
-- Supabase's direct grants in public.
alter default privileges for role postgres in schema public
revoke execute on functions from anon, authenticated;
With both, every RPC the browser calls needs an explicit grant execute ... to authenticated, which is a deliberate step and a good place to ask who should be allowed to call it. Supabase corrected its own docs to this two-statement form in October 2026; why the PUBLIC revoke must be global has the details.
The developer also looked for signs that the functions had been used. The data available couldn't fully answer that, which is common: when a row is overwritten, nothing records who overwrote it unless you built that in beforehand.
Does my app have the same problem?
Two checks in the SQL Editor.
List the SECURITY DEFINER functions the API roles can call:
select p.oid::regprocedure as function,
has_function_privilege('anon', p.oid, 'execute') as anon_can_run,
has_function_privilege('authenticated', p.oid, 'execute') as authenticated_can_run
from pg_proc p
join pg_namespace n on n.oid = p.pronamespace
where n.nspname = 'public' and p.prosecdef;
For each one that the API roles can run, read the arguments. If the function takes a tenant id, organisation id, user id or author id and uses it to decide what to write, check that the body compares it with the caller. That looks like auth.uid(), or a membership lookup such as exists (select 1 from tenant_members where tenant_id = p_tenant_id and user_id = auth.uid()). If it doesn't and the browser doesn't need the function, revoke it from anon and authenticated. If the browser does need it, derive the tenant inside the function from auth.uid() instead of accepting it as an argument.
Then check your defaults:
select pg_get_userbyid(defaclrole) as owner, defaclnamespace, defaclacl
from pg_default_acl
where defaclobjtype = 'f';
If anon and authenticated appear with X (execute), every new function you create is callable through the API until you revoke it. A row with defaclnamespace = 0 is a global default. If there's no such row for postgres, Postgres's built-in default still gives PUBLIC EXECUTE on every new function, even after you revoke the direct grants.
Does GuardLayer catch this?
Yes. The code scan flags a SECURITY DEFINER function that the API roles can execute and that never checks the caller (supabase/security-definer-no-caller-check), with the file, the line and the fix. It reads your migrations in order, so it follows grants and revokes across files, including default privileges. Live check reads the real grants from your Supabase project, so it also sees functions created outside your migrations.
FAQ
The server action passed the right tenant id. Why wasn't that enough?
Because the server action was only one way to reach the function. Anything the anon role can execute is reachable at /rest/v1/rpc/<name> with the public key, with whatever arguments the caller chooses. A SECURITY DEFINER function has to check the caller itself, or not be executable by the API roles.
Should a function ever take the tenant id as an argument?
Only if it checks it. The safer pattern is to look the tenant up inside the function from auth.uid(), so the caller can't choose it. For RLS on the tables themselves, see multi-tenant RLS in Supabase.
Is revoking the default privileges from anon and authenticated enough?
No. It removes Supabase's direct grants, but PUBLIC still gets EXECUTE on new functions through Postgres's global default. Also run alter default privileges for role postgres revoke execute on functions from public;, without in schema.
Does changing default privileges affect existing functions?
No. alter default privileges only applies to functions created afterwards. Existing functions keep their grants, so you still need to revoke those one by one.
Will revoking from authenticated break my app?
Only for functions the browser calls directly. Functions called from a server action, route handler or Edge Function with the service role key don't need the grant. Search your client code for .rpc( before you revoke.
See which of your Supabase tables anyone can read, free
Connect your Supabase project read-only. GuardLayer checks RLS, policies, grants and functions as they are right now, and gives you the SQL to fix each problem. It never reads your rows and can't change anything.
Check my Supabase projectPrefer to check code instead? Scan your repo, no signup.