All posts

Supabase

Why hostname routing can't isolate tenants in a Supabase app

By Vivek Bhange2 min read

I recently worked on taking a construction ERP from single-tenant to multi-tenant SaaS. The app is a React SPA that talks straight to Supabase: around 110 tables and views and 56 RPC functions, with no application server in between. That last detail ended up shaping every decision.

The tempting design

Each customer gets its own domain, so the obvious move is to read the hostname in Next.js middleware, work out the tenant, and scope everything to it.

The problem is that data requests never pass through that middleware. The SPA calls PostgREST directly with the user’s JWT, and anyone can open devtools and replay the same request with a different filter. Hostname routing is fine for choosing which logo to show. It is not a security boundary.

Where the boundary actually is

The only thing Postgres can trust is a signed JWT. So the tenant lives in the user’s app_metadata (which users can’t edit themselves), and Row Level Security reads it from there.

1. A tenant_id on every base table

Shared schema, with a tenant_id column on every base table. At this size that’s simpler to migrate and run than a schema per tenant.

2. RESTRICTIVE policies

Postgres combines permissive policies with OR, so a permissive policy added later for some feature can quietly widen access. A restrictive tenant policy is ANDed with everything else, so it holds whatever other policies exist:

create policy tenant_isolation on projects
  as restrictive
  for all
  to authenticated
  using (tenant_id = (auth.jwt() -> 'app_metadata' ->> 'tenant_id')::uuid)
  with check (tenant_id = (auth.jwt() -> 'app_metadata' ->> 'tenant_id')::uuid);

3. Stamp tenant_id on write

Don’t trust the client to send the right tenant. A BEFORE INSERT OR UPDATE trigger sets it from the JWT:

create function stamp_tenant() returns trigger
language plpgsql as $$
begin
  new.tenant_id := (auth.jwt() -> 'app_metadata' ->> 'tenant_id')::uuid;
  return new;
end $$;

create trigger stamp_tenant
  before insert or update on projects
  for each row execute function stamp_tenant();

4. Composite foreign keys

RLS stops you reading another tenant’s rows, but a row in tenant A could still point at an id that belongs to tenant B. Making foreign keys composite, (tenant_id, project_id) referencing (tenant_id, id), makes that reference structurally impossible:

alter table projects add unique (tenant_id, id);

alter table invoices
  add constraint invoices_project_fk
  foreign key (tenant_id, project_id)
  references projects (tenant_id, id);

The parts that are easy to forget

  • RPC functions. security definer functions bypass RLS, so each one needs its own tenant guard. With 56 of them, that’s a real checklist.
  • Permissions. Boolean permission columns don’t scale across tenants. A normalized permission catalog with per-user grants does.
  • Tests. The only proof of isolation is a test that signs in as tenant A and tries to read, write and reference tenant B’s data. That suite belongs in CI.

Takeaway

If your frontend talks to the database directly, isolation has to live in the database. Routing decides what users see; signed claims and RLS decide what they can touch.

Enjoyed this?

Written by Vivek Bhange, Full-Stack AI Engineer. See his work or work with me.

Keep reading

Easter egg hunt

This site hides 16 surprises. Found ones stay found on this device. On a phone, type secret words into the command bar (the >_ button).

  1. RickrolledNot found yet

    Show hint

    My photo has a secret. Double-click it (on a phone, press and hold).

  2. Impatient clickerNot found yet

    Show hint

    Click anywhere, fast, about 25 times in a row.

  3. Agent of changeNot found yet

    Show hint

    Type the word that means "make it different".

  4. Gravity checkNot found yet

    Show hint

    Type my first name.

  5. Secret wordNot found yet

    Show hint

    Type something friendly, like "chai" or "namaste".

  6. HackerNot found yet

    Show hint

    Type what hackers do (four letters).

  7. The snapNot found yet

    Show hint

    Type the name of a purple villain who snaps.

  8. Sharp eyesNot found yet

    Show hint

    Something almost invisible floats on the screen. Find it and click it (desktop).

  9. Root accessNot found yet

    Show hint

    Open the command bar (press /, or tap >_ on a phone), then try to become root.

  10. Duck hunterNot found yet

    Show hint

    A rubber duck is hiding on the 3D island.

  11. World travellerNot found yet

    Show hint

    Visit all four views.

  12. Night owlNot found yet

    Show hint

    Come back between midnight and 4 a.m.

  13. Arcade regularNot found yet

    Show hint

    Play every quick game in the Arcade (the ones that open on the Arcade page).

  14. Chain reactorNot found yet

    Show hint

    Set off 30 bursts with a single move in Critical Mass.

  15. Last one standingNot found yet

    Show hint

    Win a match of Drop Zone.

  16. Code whizNot found yet

    Show hint

    Get 10 answers right in a row in Code Clash.

zsh — visitor@vivek: ~

Ask Vivek

Ask me anything. My work, how I run projects, or whether I'm a fit.

AI answers from this site's content. It can make mistakes.