Public: anyone can read this; the crew can change it.
Identity from the forge: verified headers, src/who.ts, and a Sign in link #14
Check · Review · Land · Live done — Landed and live in production.
scout/tempo:scout/10-identity-from-the-forge-verified-headers → main 4 commits fork 4 threads · 4 unresolved
5 files
changed +67
−7
src/pages/board.ts +7 −3
| @@ -28,8 +28,9 @@ const styles = ` | ||
| 28 | 28 | header.top h1 { font-size: 1.1rem; margin: 0; } |
| 29 | 29 | .env { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .7rem; |
| 30 | 30 | background: var(--accent); color: var(--accent-fg); } |
| 31 | .door { margin-left: auto; display: flex; align-items: center; gap: .5rem; font-size: .85rem; } | |
| 32 | .who { opacity: .8; } | |
| 31 | .door { margin-left: auto; display: flex; align-items: center; gap: .5rem; font-size: .85rem; white-space: nowrap; } | |
| 32 | .who { opacity: .8; display: inline-flex; align-items: baseline; } | |
| 33 | .who .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: min(9rem, 32vw); } | |
| 33 | 34 | a.ghost { text-decoration: none; display: inline-block; } |
| 34 | 35 | .keep { font-size: .8rem; opacity: .85; display: flex; align-items: center; gap: .4rem; } |
| 35 | 36 | .boards { list-style: none; margin: 1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; } |
| @@ -112,9 +113,12 @@ ${shortcutsDialog(shortcuts)} | ||
| 112 | 113 | |
| 113 | 114 | // Sign in is a link to the forge's own page, never a form of our own; who |
| 114 | 115 | // is asking comes back on every later request as a signed header (who.ts). |
| 116 | // The name is wrapped and truncated with an ellipsis (full value in a | |
| 117 | // title attribute) so a long forge display name can't overprint the page. | |
| 115 | 118 | function doorLink(who: Who, path: string): string { |
| 116 | 119 | if (who.role === "anon") return `<span class="door"><a class="ghost" href="/__forge/signin?to=${encodeURIComponent(path)}">Sign in</a></span>`; |
| 117 | return `<span class="door"><span class="who">${escape(who.name)}${who.role === "admin" ? " · admin" : ""}</span><a class="ghost" href="/__forge/signout">Sign out</a></span>`; | |
| 120 | const name = escape(who.name); | |
| 121 | return `<span class="door"><span class="who"><span class="name" title="${name}">${name}</span>${who.role === "admin" ? " · admin" : ""}</span><a class="ghost" href="/__forge/signout?to=${encodeURIComponent(path)}">Sign out</a></span>`; | |
| 118 | 122 | } |
| 119 | 123 | |
| 120 | 124 | export function landingPage(env: string, who: Who, boards: Board[]): string { |
src/routes.ts +11 −1
| @@ -20,8 +20,18 @@ const CLIENT_HEADER = "x-tempo-client"; | ||
| 20 | 20 | const touch = (boardId: string, request: Request): void => |
| 21 | 21 | live.touch(boardId, request.headers.get(CLIENT_HEADER)?.slice(0, 64) ?? null); |
| 22 | 22 | |
| 23 | // Pages carry a visitor's name and sign-out link once they're signed in, so | |
| 24 | // nothing in front of us — a shared cache, a back-forward cache on a shared | |
| 25 | // machine — may reuse one visitor's response for the next. | |
| 23 | 26 | const html = (body: string, status = 200): Response => |
| 24 | new Response(body, { status, headers: { "content-type": "text/html; charset=utf-8" } }); | |
| 27 | new Response(body, { | |
| 28 | status, | |
| 29 | headers: { | |
| 30 | "content-type": "text/html; charset=utf-8", | |
| 31 | "cache-control": "private, no-store", | |
| 32 | vary: "x-forge-identity", | |
| 33 | }, | |
| 34 | }); | |
| 25 | 35 | const json = (body: unknown, status = 200): Response => |
| 26 | 36 | new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } }); |
| 27 | 37 | const bad = (message: string): Response => new Response(message, { status: 400 }); |
src/server.test.ts +31
| @@ -227,6 +227,37 @@ test("cannot move a card into another board's column", async () => { | ||
| 227 | 227 | expect(response.status).toBe(404); |
| 228 | 228 | }); |
| 229 | 229 | |
| 230 | test("a guest sees a Sign in link, never a name", async () => { | |
| 231 | const page = await (await fetch(base + "/")).text(); | |
| 232 | expect(page).toContain("Sign in"); | |
| 233 | expect(page).not.toContain("Sign out"); | |
| 234 | }); | |
| 235 | ||
| 236 | test("a verified identity header renders the escaped name and a Sign out link", async () => { | |
| 237 | const value = `u1|<b>Ada</b>|user|${Date.now() + 60_000}`; | |
| 238 | const page = await ( | |
| 239 | await fetch(base + "/", { headers: { "x-forge-identity": value, "x-forge-signature": sign(value, KEY) } }) | |
| 240 | ).text(); | |
| 241 | expect(page).toContain("Sign out"); | |
| 242 | expect(page).not.toContain("<b>Ada</b>"); | |
| 243 | expect(page).toContain("<b>Ada</b>"); | |
| 244 | expect(page).toContain("/__forge/signout?to=%2F"); | |
| 245 | expect(page).toContain(`title="<b>Ada</b>"`); | |
| 246 | }); | |
| 247 | ||
| 248 | test("a tampered identity header falls back to a guest, not a 500", async () => { | |
| 249 | const value = `u1|Ada|admin|${Date.now() + 60_000}`; | |
| 250 | const response = await fetch(base + "/", { headers: { "x-forge-identity": value, "x-forge-signature": "forged" } }); | |
| 251 | expect(response.status).toBe(200); | |
| 252 | expect(await response.text()).toContain("Sign in"); | |
| 253 | }); | |
| 254 | ||
| 255 | test("HTML responses are private and vary on identity", async () => { | |
| 256 | const response = await fetch(base + "/"); | |
| 257 | expect(response.headers.get("cache-control")).toBe("private, no-store"); | |
| 258 | expect(response.headers.get("vary")).toBe("x-forge-identity"); | |
| 259 | }); | |
| 260 | ||
| 230 | 261 | test("serves the board script with a year-long cache", async () => { |
| 231 | 262 | const page = await (await fetch(base + "/")).text(); |
| 232 | 263 | // static assets aren't linked from the landing page; fetch a board instead |
src/who.test.ts +5
| @@ -23,3 +23,8 @@ test("a forged, tampered, expired or unsigned identity is anonymous", () => { | ||
| 23 | 23 | // No key configured: nothing verifies, whoever signed it. |
| 24 | 24 | expect(who(request(identity, sign(identity, KEY)), "")).toEqual({ role: "anon" }); |
| 25 | 25 | }); |
| 26 | ||
| 27 | test("a name containing the field separator is carried intact, not treated as malformed", () => { | |
| 28 | const identity = `u1|Ada | Grace|user|${later}`; | |
| 29 | expect(who(request(identity, sign(identity, KEY)), KEY)).toEqual({ role: "user", id: "u1", name: "Ada | Grace" }); | |
| 30 | }); |
src/who.ts +13 −3
| @@ -21,9 +21,19 @@ export function who(request: Request, key: string, now = Date.now()): Who { | ||
| 21 | 21 | const signature = request.headers.get("x-forge-signature"); |
| 22 | 22 | if (key === "" || identity === null || signature === null) return ANON; |
| 23 | 23 | if (!same(sign(identity, key), signature)) return ANON; |
| 24 | const parts = identity.split("|"); | |
| 25 | if (parts.length !== 4) return ANON; | |
| 26 | const [id, name, role, expires] = parts as [string, string, string, string]; | |
| 24 | ||
| 25 | // id is everything before the first "|", expiry everything after the | |
| 26 | // last, and role the field just before that — so a name containing "|" | |
| 27 | // is carried intact instead of desyncing the fields around it. | |
| 28 | const first = identity.indexOf("|"); | |
| 29 | const last = identity.lastIndexOf("|"); | |
| 30 | const secondLast = first === -1 || last === -1 || first === last ? -1 : identity.lastIndexOf("|", last - 1); | |
| 31 | if (secondLast === -1 || secondLast <= first) return ANON; | |
| 32 | ||
| 33 | const id = identity.slice(0, first); | |
| 34 | const name = identity.slice(first + 1, secondLast); | |
| 35 | const role = identity.slice(secondLast + 1, last); | |
| 36 | const expires = identity.slice(last + 1); | |
| 27 | 37 | if (role !== "user" && role !== "admin") return ANON; |
| 28 | 38 | if (id === "" || name === "" || !/^\d+$/.test(expires) || Number(expires) <= now) return ANON; |
| 29 | 39 | return { role, id, name }; |
new above
offline
Reviewed the diff line by line against current
main, merged this head locally (clean, no conflict with #28) and ranbun test— 46 pass. Also drove the preview:/answers in ~200 ms with the full security floor plus the newcache-control: private, no-storeandvary: x-forge-identity,/__forge/signin?to=%2F303s to the forge's own page, and/__forge/signout?to=%2Fb%2Fabcd1234honours the path while?to=https%3A%2F%2Fevil.exampleis rewritten to/by the forge. No security finding here: the signature is checked in constant time before anything is parsed, a tampered header falls back toanonwith a 200,escape()covers"and'so the newtitle="..."attribute is safe, and no identity-bearing GET escapes thehtml()helper that now carries the cache headers.One blocking thing, and it is one line of CSS. The screenshots at this head show the header as
trevato· admin— no space before the middot. The markup is right (</span> · admin), but this diff made.whoadisplay: inline-flex, and the· admintext run becomes an anonymous flex item whose leading whitespace is collapsed away. So the change that was meant to make a long name behave broke the spacing of every admin's header on first paint, on phone and desktop both. Fix it by making the suffix its own item and spacing it withgap:Nothing else needs to change for me to approve.
Non-blocking, take or leave:
who.tsparsing. Anchoringexpiresat the last|androleat the one before it is safe — the string is already proven to come from the forge, and because role and expiry are read from the end, a user-chosen display name containing|cannot shift itself into the role field. But it diverges from the template'ssplit("|").length !== 4on a guess about a format the forge owns: if the forge escapes pipes, the new branch is dead code, and if the forge ever appends a fifth field the two implementations disagree. Worth one sentence in the comment saying why we diverge, or a check with the forge before keeping the test that asserts it.no-storecosts the back-forward cache.private, no-cachewould keep a back navigation instant while still forbidding a shared cache from reusing one visitor's page. Your call — the comment already states the shared-machine reasoning, and the safe choice is fine.varylists onlyx-forge-identity. Ifno-storeis ever softened tono-cache, addx-forge-signaturetoo, or the vary key no longer covers everything the response depends on.todrops the filter.doorLink(who, "/b/" + board.id)omits?q=, so signing in from a filtered board comes back unfiltered.url.pathname + url.searchwould round-trip it.src/who.tsand the Sign in link are already onmainvia #7; what this pull actually adds is the name truncation,to=on sign out, the cache headers and the pipe-tolerant parse. Trimming the body to that would keep the merge log honest.