Multi-Tenant JWT
Scope a JWT to a tenant with a tid claim, enforce tenant isolation in every query, and prevent cross-tenant access through a forged or reused token.
On this page
Introduction
This guide covers multi-tenant JWT isolation, a recurring concern in the auth-jwt scenario. In a multi-tenant system a token that proves who you are is not enough; it must also prove which tenant you belong to, and every query must enforce that boundary so a forged or reused token cannot reach another tenant data. A tid claim plus a row-level check is the minimum.
Add a tid claim
Put the tenant id in the token at login so the tenant is bound to the credential, not to a request parameter the client can change.
The trade-off worth naming is that put the tenant id in the token at login so the tenant is bound to the credential, not to a request parameter the client can change adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const jwt = await new SignJWT({ sub: user.id, tid: user.tenantId, role: user.role }).setProtectedHeader({ alg: 'HS256' }).setExpirationTime('15m').sign(secret);
Derive from the token
Read the tenant from the verified token, never from the URL or body, so a client cannot swap it to reach another tenant, which is the whole bug.
The trade-off worth naming is that read the tenant from the verified token, never from the url or body, so a client cannot swap it to reach another tenant, which is the whole bug adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Enforce in queries
Add a tenant_id filter to every query so the database only returns rows for the token tenant, which is the row-level boundary that catches a missed check in code.
The trade-off worth naming is that add a tenant_id filter to every query so the database only returns rows for the token tenant, which is the row-level boundary that catches a missed check in code adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
async function listDocs(tid) {
return db.query('SELECT * FROM doc WHERE tenant_id=$1', [tid]);
}
Row-level security
If your database supports it, set a row-level security policy scoped to the session tenant so a query that forgets the filter still cannot leak, which is defense in depth.
The trade-off worth naming is that if your database supports it, set a row-level security policy scoped to the session tenant so a query that forgets the filter still cannot leak, which is defense in depth adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Isolate keys per tenant
For strong isolation, sign each tenant with its own key so a compromise of one tenant secret cannot forge tokens for another, at the cost of key management.
The trade-off worth naming is that for strong isolation, sign each tenant with its own key so a compromise of one tenant secret cannot forge tokens for another, at the cost of key management adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test cross-tenant
Log in as tenant A, request tenant B data, and assert an empty result or a 403, so the boundary holds against a direct attempt.
The trade-off worth naming is that log in as tenant a, request tenant b data, and assert an empty result or a 403, so the boundary holds against a direct attempt adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: Postgres RLS, JWT RFC 7519.
FAQ
- Should the tenant come from the token or the URL?
- From the token; if it comes from the URL or body, a client can swap it to access another tenant, which is the classic cross-tenant bug.
- How do I enforce it in the database?
- Add a tenant_id column to every tenant-scoped table and filter on it in every query, or use row-level security if your database supports it, so a missed filter is not a leak.
Report a problem with this articleOpens a pre-filled issue on GitHub.