(Note · · 6 min read)
One customer saw another customer's data
Serving several companies from one database means every query needs the right tenant filter. Hundreds of queries, each correct, and the first one that is not produces the worst message a business ever sends its customers. Relying on every developer remembering is not a design.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
The filter must not be optional
If a query can be written without the tenant condition and still return rows, the leak is a matter of time. The goal is a design in which omitting the filter returns nothing or fails, rather than returning everyone's data.
Set the current tenant once per request, from the authenticated session and never from user-supplied input, then apply it in the layer all queries pass through. Row-level security in the database is stronger still, because it holds even for a query that bypasses the application's data layer entirely.
Never take the tenant from the request body
An identifier in a parameter, a header or a payload is a value the caller chose. Honouring it means any authenticated user can read another tenant by changing a number — which is the most common way this goes wrong, and it looks like working code.
Derive the tenant from the session, then verify that every identifier the request mentions belongs to it. Checking the object's tenant matches the session's is the step that is usually missing.
Writes and reads both need it
Attention goes to reads because that is where a leak is visible. An update or delete missing the tenant condition is worse: it modifies another customer's data, and nothing in the response indicates it.
The same applies to anything that moves data in bulk — exports, reports, background jobs, admin tooling. A job that iterates across tenants has to carry the tenant through each unit of work, and that is exactly where a request-scoped filter is no longer present.
Isolate the things that are not rows
Uploaded files, cache keys, search indexes, generated documents and queued jobs all need the tenant in their identity. A cache key without one serves a cached page to the wrong company — a leak that no amount of query review would find, because no query was wrong.
Where a tenant has its own external account — a payment merchant account, a mail configuration — route through one parameterised code path that takes the tenant's settings, rather than per-customer branches. That is how a second property gets its own merchant account without a second implementation to keep in step.
Test from the attacker's position
Write tests where one tenant's user requests another tenant's identifiers and the expected result is a refusal. Seed at least two tenants in test data, because with one tenant seeded every missing filter passes.
Cover each type of entry point once: a detail endpoint, a list, an update, a file download, an export. The pattern of the bug repeats across them, so finding it in one is worth more than reviewing a hundred queries.
Decide before it is urgent
A shared database with enforced filtering is the usual right answer, and a separate database per tenant is justified when a customer's contract or regulator requires it. Choose knowingly: changing from one to the other later is a migration, not a refactor, and it arrives at the moment a large customer is signing.
Common questions
How do I stop one tenant seeing another's data?
Make the filter non-optional. Set the tenant once per request from the authenticated session, apply it in the layer all queries pass through, and prefer row-level security in the database, which holds even when a query bypasses the application's data layer.
Can the tenant identifier come from the request?
No. A value in a parameter, header or payload is chosen by the caller, so honouring it lets any authenticated user read another tenant by changing a number. Derive it from the session and verify every identifier the request mentions belongs to that tenant.
What besides database rows needs tenant isolation?
Uploaded files, cache keys, search indexes, generated documents and queued jobs. A cache key without a tenant serves one company's page to another — a leak no query review finds, because no query was wrong.
How should multi-tenant isolation be tested?
Seed at least two tenants, then write tests where one tenant's user requests another's identifiers and expects a refusal. With a single tenant in test data, every missing filter passes. Cover a detail endpoint, a list, an update, a download and an export.