ServicesSecurity BlueprintInsightsAboutTalk to us

Row-Level Security: the tenant boundary your app code can’t forget

In a multi-tenant app, every query needs WHERE org_id = .... Forget it once, in a report, a background job or a new endpoint, and one customer can see another customer's data. Code review catches most of these. It will not catch all of them.

PostgreSQL Row-Level Security (RLS) moves that rule into the database. The database adds the filter to every query, so forgetting it stops being possible.

The minimal setup

ALTER TABLE invoices ENABLE ROW LEVEL SECURITY;
ALTER TABLE invoices FORCE ROW LEVEL SECURITY;

CREATE POLICY tenant_isolation ON invoices
  USING (
    org_id = nullif(current_setting('app.current_org_id', true), '')::uuid
  )
  WITH CHECK (
    org_id = nullif(current_setting('app.current_org_id', true), '')::uuid
  );

USING decides which rows a query can see, update or delete. WITH CHECK decides which rows it may write, so an insert cannot plant data in another tenant. FORCE makes the policy apply to the table's owner too, not only to other roles.

Set the tenant per transaction

The policy reads the current organization from a setting your app provides at the start of each request:

BEGIN;
SELECT set_config('app.current_org_id', $1, true);  -- true = this transaction only
-- ... the queries for this request ...
COMMIT;

The true matters. Apps share database connections through a pool, and a session-wide setting survives into the next request that borrows the connection. That request would then run as the previous customer. A transaction-scoped setting disappears when the transaction ends.

It also fails closed. If the app forgets to set the tenant, the comparison is against nothing, so queries return no rows. Never another tenant's rows.

Four ways it quietly stops working

  • The app connects as a privileged role. Superusers and roles with BYPASSRLS skip every policy. Give the application its own low-privilege role and connect as that.
  • The service key is in the wrong place. On Supabase, the service key bypasses RLS by design. It belongs on your server only, never in browser code.
  • Views skip the policy. A view runs with its owner's rights. On PostgreSQL 15 and later, create it with security_invoker = true so it runs with the caller's rights instead.
  • A new table has no protection. RLS is switched on per table. Add a CI check that fails the build when a table slips through:
SELECT c.relname
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relkind = 'r' AND NOT c.relrowsecurity;
-- any row returned = a table without RLS

One performance note: the policy runs on every query, so index org_id and put it first in composite indexes.

Test the boundary on purpose

Do not assume the policy works. Write a test that connects as the application role and proves it:

  • Set organization A, insert a row, then set organization B and confirm that row is invisible.
  • While set to B, try to insert a row for A and confirm the database rejects it.
  • Reset the setting and confirm a query returns nothing.

What it is, and what it is not

RLS is a safety net. It does not replace your authorization logic: users inside one organization still need role checks. What it does is make the worst mistake, a cross-customer leak, a database error instead of a breach.

This is the pattern behind our Atlas AI Search project, where vector search is joined to the caller's organization inside PostgreSQL.

Newsletter

Get new insights by email.

Short, practical notes on security and technology risk, sent only when there’s something worth reading. Never more than one a month.