TENANT ISOLATION

Every handler checks the tenant; the cache does not

L318 MINSECURITY
PREDICT

Every handler in your API scopes its database query to the caller's tenant; an IDOR test against every endpoint passes. The dashboard caches its monthly summary under the key summary:2026-11 for 60 seconds. Acme loads the dashboard, then Globex loads it ten seconds later. What does Globex see?

The IDOR lesson puts the tenant into every query a handler makes. That is necessary and not sufficient. A multi-tenant system shares other things between customers: a cache, a job queue, a search index, a connection pool, an export bucket. Each is a lookup by key, and if the tenant is not part of the key, the lookup crosses tenants. The handler that filled it was correct; the one that reads it never runs the query at all.

The fix is to make the tenant impossible to leave out. Handlers and jobs never touch the raw clients. They get a repository bound to one tenant that adds the tenant to every key and every query, and refuses to run without one. Row-level security in the database is the same rule one layer lower.

▶ SIMULATION
ACME (alice)
received: —
foreign data: none
SHARED: CACHE · JOBS · INDEX
cache empty
search index: 4 docs, both tenants · filter: none
GLOBEX (mallory)
received: —
foreign data: none
last—
✓ no tenant ever receives another tenant's data✓ every cache entry is keyed by the tenant whose data it holds
SCHEDULE
01[API]API: every handler scopes its own queries; cache key is "summary:<month>"
02[ACME]alice opens the dashboard
03[GLOBEX]mallory opens the dashboard
04[API]report
Start of break: cache key without tenant scenario. 4 steps.
// Each handler scopes its own query. The shared pieces do not.
const key = `summary:${month}`;                       // same key for every tenant
const hit = await cache.get(key);

await queue.add('export-overdue', { requestedBy: user.id });   // no tenant
// worker:
const rows = await db.invoice.findMany({ where: { status: 'overdue' } });

const hits = await index.search({ text: q });               // whole index

Watch the SHARED box. In CACHE, the key turns red as soon as alice fills it, before anyone has seen a leak; the first invariant fails when mallory reads it. In EXPORT JOB the job’s tenantId is empty and the email holds both tenants’ invoices. In FIX every key starts with its tenant, the job carries one, and both invariants stay green.

BREAK IT NOT YET

The cache key is "summary:2026-11" for everyone, and adding an invoice invalidates it. Each handler computes its summary from its own tenant's rows. Order the three steps so that mallory sees acme's numbers.

GOAL: mallory receives acme's summary

FIX IT NOT YET

Every handler and background job reaches shared infrastructure only through forTenant(tenantId, infra). Write it. key(...parts) builds a cache key; find(filter) queries invoices; search(text) queries the shared search index. The grader interleaves two tenants through a dashboard cache, a search, an export job and a list endpoint that passes the caller's filter through, and checks that no response ever holds the other tenant's rows, that the cache still hits for the same tenant, and that a job with no tenant returns nothing.

Mental model:

Tenant isolation fails in the shared parts, not the handlers: caches, queues, jobs, indexes, pools. Make the tenant part of every key and every query, carry it in every job payload, and enforce it below the handler with a scoped repository or row-level security, so the code that forgets the tenant gets nothing instead of everything.

IN PRODUCTION

Multi-tenant SaaS: Redis or CDN caches keyed by path or month, nightly exports and report emails, webhooks fanned out by a worker, Elasticsearch or OpenSearch indexes shared across customers, vector stores behind an AI feature, analytics warehouses. PostgreSQL row-level security policies give default-deny per table, but table owners bypass them unless the table is set to FORCE ROW LEVEL SECURITY, and superusers always do, so the application must not connect as either.

IN INTERVIEWS

“How do you guarantee one customer never sees another's data?” Go beyond the handler: list the shared resources (cache, jobs, search, files), say how the tenant gets into each key and payload, describe a scoped repository or row-level security so the check cannot be forgotten, and say how you would test it: two tenants interleaved through every path, asserting that no response holds the other tenant's ids.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.