TB-004
Bulletin
TB-004Bulletin
Designing permission checks that are fast enough
One admin screen asked “may this person do that?” 31 times. How a check became a single bit test, and which check still belongs in your WHERE clause.
Aoife BrennanEngineer, organizations, Dublin
- Title
- Technical bulletin
- Doc. no.
- LM-09 / TB-004
- Subject
- Infrastructure
- Issued
- 13 Oct 2026
- Reading
- 4 min
A settings page in a B2B app asks “may this person do that?” more often than you would guess. We counted 31 checks while rendering one admin screen for a customer: every button, every menu item, every row action. If each check were a call to an auth service at 38 ms, that page would spend more than a second asking permission. So the first requirement for a permission check is not expressiveness. It is that the check costs nothing.
1.0Decide where the answer lives
In Latch, a user belongs to an organization through a membership, a membership has a role, and a role is a named set of permissions such as billing:manage or members:invite. Customers edit roles themselves, so the mapping is data, not code. There are three places the answer to a check can come from.
| Where the answer lives | Per check | Stale for | Breaks when |
|---|---|---|---|
| API call for every check | 38 ms | 0 s | The network is slow, or you check in a loop |
| Role in token, role table cached | ~10 µs | 60 s + cache TTL | Cold starts fetch the table first |
| Permissions in the token | under 1 µs | 60 s | The list outgrows a cookie |
We already rotate the session token every 60 seconds (see bulletin TB-005), which gives us a vehicle that is re-issued often and verified locally. Putting the permissions in it means a check is a lookup in data your server already holds. The remaining problem is size: forty permission names at sixteen characters each is 640 bytes before encoding, and cookies stop at about 4 kB with everything else they carry.
2.0A bitmap, not a list
Each permission in an application gets a stable index when it is created: members:invite is 0, billing:manage is 5, and so on. The token carries one bit per index, base64url-encoded. Sixty-four permissions are eight bytes, which is eleven characters in the token. The SDK holds the name-to-index table, which changes only when you add a permission, and is versioned in the token so a stale table is detected rather than trusted.
// "billing:manage" -> 5, from the application's permission tableexport function has(claims: Claims, permission: string) { const index = claims.table[permission]; if (index === undefined) return false; // unknown permission: deny return (claims.bitmap[index >> 3] & (1 << (index & 7))) !== 0;}That is the whole check: one object lookup, one shift, one mask. On a 2023 laptop it runs in about 0.4 microseconds, so the 31 checks on that admin screen cost less than rendering a single button. In application code it reads the way you would want:
import { auth } from "@latch/nextjs/server";export async function POST(request: Request) { const { orgId, has } = await auth(); if (!has("billing:manage")) { return new Response(null, { status: 403 }); } // The second check: this invoice must belong to this organization. const invoice = await db.invoice.create({ data: { orgId, ...(await request.json()) } }); return Response.json(invoice);}Request The token already contains the organization, the role and the permission bitmap.
Check Signature first, then has(): about 0.4 µs after verification.
Scope The data check. Tenant isolation is enforced in the query, not by the role.
Change The membership version is bumped. Nothing is pushed to your server.
Propagation Within 60 seconds every active session carries the new permissions.
3.0Sixty seconds of staleness, on purpose
The price of a local answer is that it can be up to a minute old. For a promotion, nobody notices. For a demotion, a minute is usually fine and occasionally not: someone removed from an organization during an incident should lose access now. Removal therefore does two things. It bumps the membership version, like any role change, and it marks the organization claim on that user's sessions as invalid, so the very next refresh fails over to “no active organization” without waiting for the timer. For the few routes where even that is too slow, auth({ fresh: true }) asks Latch directly.
4.0The check that matters most is in the WHERE clause
A role answers “may this person manage billing in this organization?”. It does not answer “does this invoice belong to this organization?”. That second question is about your data, and no auth vendor can answer it for you. Most real access-control bugs are of this kind: the role check passes, and the handler then loads a record by ID without asking whose it is.
- Take
orgIdfrom the verified token, never from the URL or the request body. - Put it in every query. If your database supports row-level security, set it as a session variable and let the database refuse.
- Deny by default. An unknown permission name, a missing claim or a table version mismatch is a
false, not an exception to be caught somewhere. - Return 404 rather than 403 for records in other tenants, so IDs cannot be probed.
<Protect permission="billing:manage"> decides what to render; the route behind the button must call has() again on the server, every time.5.0Limits we chose
An application can define up to 256 permissions; that is 32 bytes in the token. We have been asked for more twice. Both times the customer was encoding resources as permissions (project:4821:edit), which is a different model, relationship-based access, and belongs in their database next to the projects. Roles are for what a kind of person may do. Which things they may do it to is a join.
0.4 µs
has() after verification
11
Token bytes for 64 permissions
60 s
Longest wait for a role change
6.0
Get started
6.0Get started
Read enough. Try the part.
Everything in these bulletins is switched on in the free plan: passkeys, 60-second tokens, organizations and roles, for your first 10,000 monthly active users.
npm install @latch/nextjsIncluded on Free
- Monthly active users
- 10,000
- Passkeys, codes, links
- Included
- Social providers
- 3
- Organizations
- 5 members each
- Card required
- No