Your Supabase RLS helper functions are callable through the API. Moving them to a private schema (no USAGE needed)
Most Supabase RLS setups have a helper like is_admin(). It's a SECURITY DEFINER function that policies call to avoid infinite recursion. Mine lived in the public schema, next to my tables. Everything in public is expose
Most Supabase RLS setups have a helper like is_admin(). It's a SECURITY DEFINER function that policies call to avoid infinite recursion. Mine lived in the public schema, next to my tables.
Everything in public is exposed through the Data API. So any signed-in user could call it directly:
POST /rest/v1/rpc/is_admin → 200 true / false
That's not a leak. The function only answers about the caller. But it's an endpoint nobody needs, and a reader on my post about the recursion error suggested moving it to a schema the API doesn't expose. I replied that authenticated would then need USAGE on that schema. I tested it, and I was wrong.
Step 1: find out who uses the helpers
Before moving anything, I listed everything that depends on the three helpers (is_admin(), is_org_member(uuid), is_org_owner(uuid)):
- 16 policies (14 in
public, 2 onstorage.objects), all forauthenticated - 0 other functions or views
- 0 calls from the app code. It only calls two other RPCs.
-
is_org_ownerwas used by nothing at all. It was only reachable as an RPC.
If your app calls a helper with supabase.rpc(...), moving it will break that call. Check first.
Step 2: move them
ALTER FUNCTION ... SET SCHEMA keeps the function's OID. Policies point to functions by OID, so the 16 policies didn't need to be recreated. Their text just changes from is_admin() to private.is_admin().
create schema if not exists private;
-- no role gets USAGE (or CREATE) on the schema
revoke all on schema private from public, anon, authenticated, service_role;
alter function public.is_admin() set schema private;
alter function public.is_org_member(uuid) set schema private;
alter function public.is_org_owner(uuid) set schema private;
-- EXECUTE only for authenticated (the policies need it)
revoke execute on function private.is_admin() from public, anon, service_role;
revoke execute on function private.is_org_member(uuid) from public, anon, service_role;
revoke execute on function private.is_org_owner(uuid) from public, anon, service_role;
grant execute on function private.is_admin() to authenticated;
grant execute on function private.is_org_member(uuid) to authenticated;
grant execute on function private.is_org_owner(uuid) to authenticated;
private must stay out of the Data API's exposed schemas. In my hosted project, that setting is under Integrations → Data API → Settings.
The surprise: no USAGE needed
I tested three states inside transactions that always rolled back, signed in as an admin, two users in company A and a user in company B. I counted rows in 8 tables and storage.objects:
| Schema USAGE | Function EXECUTE | Row counts | Calling private.is_admin() by name |
|---|---|---|---|
| granted | granted | same as before | works |
| none | granted | same as before | 42501 permission denied for schema private |
| none | none | every read fails | 42501 permission denied for function is_admin |
The policies kept working without USAGE. They already hold the function's OID, and schema USAGE is checked when a name is looked up. EXECUTE is still checked, so it has to stay.
Leaving USAGE off is a small extra layer: nobody can call the helpers by name from SQL either.
What the API returns now
-
POST /rest/v1/rpc/is_admin→404,PGRST202(the function isn't in the schema cache) - The same call with
Content-Profile: private→406,PGRST106("Invalid schema: private") -
select public.is_admin()→42883, the function no longer exists inpublic
Side check: does FORCE ROW LEVEL SECURITY break the helper?
I wondered whether FORCE ROW LEVEL SECURITY on profiles would make is_admin() return false, since the function's owner would then be subject to RLS. It didn't. In both my local and hosted projects, postgres isn't a superuser but has BYPASSRLS, and that wins over FORCE. The admin still got true, and row counts didn't change. So I didn't add it.
Things that bit me (or almost did)
-
Old scripts. My earlier verify and rollback SQL called
public.is_admin()by name. After the move they fail with42883. -
Default privileges. I had closed default privileges for
publiconly (previous post). A new function inprivatestill getsEXECUTEforPUBLICby default, so the migration revokes it per function. -
Local vs hosted again. Locally, the helpers also had
EXECUTEforservice_role. In production they didn't. The end state is the same, but the rollback has two versions.
How I checked it
- Locally: the verify SQL ran before and after, with a rollback and a re-apply in between.
- My access-check script grew from 482 to 510 checks, and all pass on every pull request. Rolling the migration back makes 47 of them fail, so they really test it.
- In production: a read-only query first, then the migration in the SQL Editor. Six fingerprints (table, column and other function grants, policies, RLS flags) were the same before and after. A probe that always rolls back confirmed the
42501and42883results, and that signed-in reads still work.
Quick checklist
- List what uses your helpers: policies, functions, views and
rpc()calls in your app. - Move them with
ALTER FUNCTION ... SET SCHEMA private. Don't recreate the policies. - Give no role
USAGEonprivate. KeepEXECUTEfor the role your policies run as. - Make sure
privateisn't in the Data API's exposed schemas. - Check that
/rpc/your_helpernow returns 404, and that signed-in users still see the same rows.
The pull request with the migration, verify and rollback SQL: https://github.com/kuros-works/client-portal-demo/pull/25
Want someone to check things like this in your Supabase project? I review RLS policies and privileges as a fixed-price job on Upwork: https://www.upwork.com/services/product/development-it-a-supabase-rls-security-review-for-your-lovable-bolt-or-cursor-app-2126067179511646761
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.