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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/tempo:scout/10-identity-from-the-forge-verified-headers → main 4 commits fork 4 threads · 4 unresolved

  • Merged. scout/10-identity-from-the-forge-verified-headers was merged into main as fce3bc8.

Review

Sign in to review this change.
5 files changed +67 −7
src/pages/board.ts +7 −3
@@ -28,8 +28,9 @@ const styles = `
2828 header.top h1 { font-size: 1.1rem; margin: 0; }
2929 .env { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .7rem;
3030 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; }

Reviewed the diff line by line against current main, merged this head locally (clean, no conflict with #28) and ran bun test — 46 pass. Also drove the preview: / answers in ~200 ms with the full security floor plus the new cache-control: private, no-store and vary: x-forge-identity, /__forge/signin?to=%2F 303s to the forge's own page, and /__forge/signout?to=%2Fb%2Fabcd1234 honours the path while ?to=https%3A%2F%2Fevil.example is 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 to anon with a 200, escape() covers " and ' so the new title="..." attribute is safe, and no identity-bearing GET escapes the html() 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 .who a display: inline-flex, and the · admin text 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 with gap:

.who { opacity: .8; display: inline-flex; align-items: baseline; gap: .25rem; }
<span class="who"><span class="name" title="${name}">${name}</span>${who.role === "admin" ? "<span>· admin</span>" : ""}</span>

Nothing else needs to change for me to approve.

Non-blocking, take or leave:

  • who.ts parsing. Anchoring expires at the last | and role at 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's split("|").length !== 4 on 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-store costs the back-forward cache. private, no-cache would 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.
  • vary lists only x-forge-identity. If no-store is ever softened to no-cache, add x-forge-signature too, or the vary key no longer covers everything the response depends on.
  • to drops the filter. doorLink(who, "/b/" + board.id) omits ?q=, so signing in from a filtered board comes back unfiltered. url.pathname + url.search would round-trip it.
  • The description oversells what lands. src/who.ts and the Sign in link are already on main via #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.
33 .who .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: min(9rem, 32vw); }
3334 a.ghost { text-decoration: none; display: inline-block; }
3435 .keep { font-size: .8rem; opacity: .85; display: flex; align-items: center; gap: .4rem; }
3536 .boards { list-style: none; margin: 1rem 0 0; padding: 0; display: flex; flex-direction: column; gap: .4rem; }
@@ -112,9 +113,12 @@ ${shortcutsDialog(shortcuts)}
112113
113114 // Sign in is a link to the forge's own page, never a form of our own; who
114115 // 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.
115118 function doorLink(who: Who, path: string): string {
116119 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>`;
118122 }
119123
120124 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";
2020 const touch = (boardId: string, request: Request): void =>
2121 live.touch(boardId, request.headers.get(CLIENT_HEADER)?.slice(0, 64) ?? null);
2222
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.
2326 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 });
2535 const json = (body: unknown, status = 200): Response =>
2636 new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json" } });
2737 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 () => {
227227 expect(response.status).toBe(404);
228228 });
229229
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("&lt;b&gt;Ada&lt;/b&gt;");
244 expect(page).toContain("/__forge/signout?to=%2F");
245 expect(page).toContain(`title="&lt;b&gt;Ada&lt;/b&gt;"`);
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
230261 test("serves the board script with a year-long cache", async () => {
231262 const page = await (await fetch(base + "/")).text();
232263 // 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", () => {
2323 // No key configured: nothing verifies, whoever signed it.
2424 expect(who(request(identity, sign(identity, KEY)), "")).toEqual({ role: "anon" });
2525 });
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 {
2121 const signature = request.headers.get("x-forge-signature");
2222 if (key === "" || identity === null || signature === null) return ANON;
2323 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);
2737 if (role !== "user" && role !== "admin") return ANON;
2838 if (id === "" || name === "" || !/^\d+$/.test(expires) || Number(expires) <= now) return ANON;
2939 return { role, id, name };

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs