Workbench / The TiffinBox Build
S51 · V3·50
Whose Order Is It?
Volume 3, chapter 50. For fifty stages TiffinBox believed the X-Customer-Id header: anyone could place an order as anyone, read any order by counting its sequential ids, and accept or cancel any kitchen's orders. This stage adds signed tokens (HS256 with a key id, audience and expiry, verified strictly: one algorithm, constant-time comparison, claims read with the strict JSON reader, so a duplicate "role" is refused), keys in the data directory with rotation, a session cookie for browsers (whose event streams can't send headers), and one Access class with every rule: customers see and cancel their own orders, kitchens move their own, admins anything, and someone else's order is a 404. It ships in report mode first: the browser suite showed 124 of 124 protected requests would have been refused, all from TiffinBox's own tests, before they carried credentials. Then the suite and the release check run enforced.
Stage 51: whose order is it?
The brief
What you'll build.
Tokens: compact JWTs signed with HMAC-SHA256: headeralg,typ,kid; claimssub,role(customer, kitchen, admin),aud("tiffinbox"),exp. Verification accepts only HS256, looks the key up bykid, compares in constant time, parses claims strictly, checks audience and expiry against the request's instant. Keys inDATA/keys.properties(created on first start, owner-only),currentsigns, every listed key verifies.tiffinbox token DIR ROLE SUBJECT [DAYS]: mints a token with the directory's current key.- Who's calling:
RequestContextgains the principal, fromAuthorization: Beareror thetb_sessioncookie, and remembers why a presented token couldn't be used. Access: every rule, andreportorenforce. Denials are 401 (withWWW-Authenticate: Bearer, or303 /loginfor a browser asking for HTML), 403, or 404 for orders the caller may not see. In report mode they're only logged ("authz"in the access line).- Routes:
POST /orders(a customer, as themselves;X-Customer-Idoptional),GET /orders/{id}(its customer, its kitchen, an admin), events (its kitchen; its customer may cancel; refund-paid needs an admin), the kitchen page,/kitchens/{id}/nextand the stream (that kitchen or an admin),/rum/summary(admin).GET /loginandPOST /sessionset the cookie (HttpOnly,SameSite=Strict). Orders.customerOf: who placed each order, for the checks.- Config:
auth(reportby default this release, orenforce). - Clients: the browser suite mints tokens (shared keys for all its servers), signs browsers in with a cookie and runs enforced;
simulate BASE N SEED DIRsigns each customer; the release check runs enforced with tokens. - Tests:
TokensTest(4),ServerTest.enforcedAccessKeepsOrdersToTheirCustomerAndKitchen. - Measurements in
exp/51:idor.sh(idor-before.out,idor-after.out),authz.py(authz.out),Claims.java(claims.out).
Why a real company needs this. Broken object-level authorisation is the most common serious API flaw: a signed-in user reading or changing someone else's data. And access control added carelessly to a live system takes it down.
Why now. Chapter 50 is Spring Security and identity. Stage 49 left X-Customer-Id believed and deprecation counts without a caller's identity.
What you'll have at the end.
- every order reachable only by its customer, its kitchen and admins;
- tokens that verify only as issued, and keys that rotate;
- browsers and API clients authenticated, each by what it can carry;
- a rollout that measured its breakage before enforcing;
- ADR-0053: identity and access in TiffinBox.
Theory to own
- Object-level authorisation. Why isn't "signed in" enough, and why 404 rather than 403? (Companion, Gaps 1 and Theory 1.)
- Strict verification. What must a verifier refuse, beyond a bad signature? (The chapter, "JWT"; the lab, Experiment 2; Companion, Gaps 3 and Theory 2.)
- Carriers. Why a cookie for browsers and a header for APIs, and what each costs? (The chapter, "Sessions", "CSRF"; Companion, Gaps 4 and Theory 3.)
- Rollout. How do you turn access control on without an outage? (Companion, Gaps 2.)
Build it
Step 1: what anyone can do
Write exp/51/idor.sh: c1 places an order; then c2, nobody and kitchen k3 read and change it; then c1 and k7 do. Run it on stage 50's server.
Checkpoint. exp/51/idor-before.out.
Step 2: tokens
Write Tokens (issue, verify, load with a generated key file) and the token command; write TokensTest and exp/51/Claims.java.
Checkpoint. exp/51/claims.out.
Step 3: rules, reported
Add Access, the principal in RequestContext, the checks in OrdersApi and the route table, customerOf, the login page and POST /session, and the auth setting (report by default). Run the browser suite with TB_SUITE_LOGS and count with exp/51/authz.py.
Checkpoint. The first half of exp/51/authz.out; golden master: 47 exchanges, identical (report mode changes no response).
Step 4: clients, then enforce
Give the suite tokens (one key file shared by all its servers, because cookies ignore ports), a signed-in browser context and --auth=enforce; give simulate a key directory; make the release check mint tokens and enforce. Run idor.sh against an enforcing server.
Checkpoint. The second half of exp/51/authz.out; exp/51/idor-after.out; the suite passes enforced.
Senior and architect lens
1. Who owns each object is a data question. The rules were short once every order knew its customer and kitchen; the work was making that lookup cheap.
2. ADR-0053: identity and access in TiffinBox. Write it with these points:
- Tokens: HS256 JWTs,
kid,aud=tiffinbox,exp; one algorithm; strict claims; keys inDATA/keys.properties, rotated with overlap (add, make current, remove after the longest token's lifetime). - Carriers: bearer tokens for API clients; an
HttpOnly,SameSite=Strictcookie for browsers, set byPOST /session; cross-origin writes refused (stage 12). - Rules: all in
Access, called from the route table and the controllers; 404 for objects the caller may not see; tests per role, action and owner. A check that every route declares a rule, so that none is public by accident, is stage 55's. - Rollout:
reportthis release;enforcefrom 0.4.0, once production's report log shows no denials for a week. - Not yet: issuing tokens to customers (sign-up, login) is outside TiffinBox; an identity provider issues them in production, and TiffinBox verifies.
3. Measure before you enforce. The same discipline as stage 49's deprecation: decisions logged, clients moved, count at zero, then the switch.
What breaks first, and when it's addressed:
| Pressure | What breaks | Where it's addressed |
|---|---|---|
| tokens can't be revoked one at a time | a stolen token works until it expires | Volume 10 (security: short lifetimes and revocation from an identity provider; stage 53 measured overload first) |
HTML receipts (/receipt/{id}) have no owner check |
anyone can read a receipt by id | stage 56 (projects: the customer flow signs in) |
| report-mode denials have no per-client view | hard to tell whose code to fix in production | stage 52 (jobs: a daily report of denials and deprecated uses by caller) |
| an HMAC key shared by issuer and verifier | anyone who can verify can also issue | Volume 10 (security: asymmetric keys, an identity provider, JWKS) |
Evidence
exp/51/idor-before.out,idor-after.out,authz.out,claims.out.Tokens, thetokencommand,Access, the principal inRequestContext,customerOf,/loginandPOST /session, theauthsetting.- The suite,
simulateand the release check carry credentials and run enforced. - Core 56, server 71 and tbx 21 tests;
golden master: 47 exchanges, identical; the release check passes. - ADR-0053 is committed.
Carry forward
- Every new route gets a rule in
Access, and a test with another user. - Objects someone may not see are 404.
- Credentials are checked, not believed: no header names a caller without a token behind it.