Public: anyone can read this; the crew can change it.
An expired or used forge link says so in one sentence and names who sent it #13219
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13141-an-expired-or-used-forge-link-says-so-in → main 6 commits fork 3 threads · 3 unresolved
5 files
changed +87
−31
src/web/pages/claim.ts +35 −17
| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | * |
| 13 | 13 | * The GET only shows the form — a link unfurled in a chat app must not |
| 14 | 14 | * spend itself — and the POST, same-origin, makes the account and burns the |
| 15 | * link in one step. A link that is used, expired or made up says which, | |
| 16 | * plainly; a forge that was not spawned has no claim page at all. | |
| 15 | * link in one step. A link that is used or expired says which in one | |
| 16 | * sentence and names who sent it (410); one that is revoked or made up is a plain 404; a forge that | |
| 17 | * was not spawned has no claim page at all. | |
| 17 | 18 | * |
| 18 | 19 | * Who keeps access is never silent. By default the spawner's account leaves |
| 19 | 20 | * the organisation's Owners when the forge is claimed (the new owner can |
| @@ -98,6 +99,13 @@ export function relativeWords(ms: number): string { | ||
| 98 | 99 | return ms >= 0 ? `in ${span}` : `${span} ago`; |
| 99 | 100 | } |
| 100 | 101 | |
| 102 | /** | |
| 103 | * Who sent the link, as a person reads them: the display name when there is one, else the username. | |
| 104 | */ | |
| 105 | function sender(user: User | undefined): string | undefined { | |
| 106 | return user === undefined ? undefined : user.displayName || user.username; | |
| 107 | } | |
| 108 | ||
| 101 | 109 | function dead( |
| 102 | 110 | ctx: Ctx, |
| 103 | 111 | state: Exclude<ClaimState["state"], "ok">, |
| @@ -105,8 +113,8 @@ function dead( | ||
| 105 | 113 | own = false, |
| 106 | 114 | ): Response { |
| 107 | 115 | const ask = sender === undefined ? "whoever sent it" : sender; |
| 108 | // A stranger must not learn which of unknown, expired or revoked this is: | |
| 109 | // one status, one title, one kind sentence, naming no one. | |
| 116 | // Only a link that once was real says so (used, expired) and names its | |
| 117 | // sender; unknown and revoked stay one plain 404 naming no one. | |
| 110 | 118 | const gone = { |
| 111 | 119 | status: 404, |
| 112 | 120 | title: "This link does not work", |
| @@ -116,10 +124,14 @@ function dead( | ||
| 116 | 124 | unknown: gone, |
| 117 | 125 | used: { |
| 118 | 126 | status: 410, |
outdated
Principle 1: repeats the title word for word. Fix: text = |
||
| 119 | title: "This link has already been used", | |
| 120 | text: `Each link works once. If it was you, sign in with the account you made; if not, ask ${ask} to send you a new one.`, | |
| 127 | title: "This link was already used", | |
| 128 | text: `If it was you, sign in; if not, ask ${ask} to send you a new one.`, | |
| 129 | }, | |
| 130 | expired: { | |
| 131 | status: 410, | |
| 132 | title: "This link has expired", | |
| 133 | text: `Ask ${ask} to send you a new one.`, | |
| 121 | 134 | }, |
| 122 | expired: gone, | |
| 123 | 135 | revoked: gone, |
| 124 | 136 | owned: { |
| 125 | 137 | status: 410, |
| @@ -130,8 +142,8 @@ function dead( | ||
| 130 | 142 | // The person who started this forge themselves has no sender to ask. |
| 131 | 143 | const mine: Partial<Record<typeof state, string>> = { |
| 132 | 144 | unknown: "This link does not work any more. Start the forge again from the home page.", |
| 133 | used: "Each link works once. If it was you, sign in with the account you made. Otherwise start a forge of your own from the home page.", | |
| 134 | expired: "This link does not work any more. Start the forge again from the home page.", | |
| 145 | used: "If it was you, sign in. Otherwise start a forge of your own from the home page.", | |
| 146 | expired: "Start the forge again from the home page.", | |
| 135 | 147 | revoked: "This link does not work any more. Start the forge again from the home page.", |
| 136 | 148 | }; |
| 137 | 149 | if (own) says.text = mine[state] ?? says.text; |
| @@ -330,7 +342,7 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> { | ||
| 330 | 342 | return dead( |
| 331 | 343 | ctx, |
| 332 | 344 | link.state === "used" ? "used" : link.revokedAt !== null ? "revoked" : "expired", |
| 333 | own ? undefined : spawner?.username, | |
| 345 | own ? undefined : sender(spawner), | |
| 334 | 346 | own, |
| 335 | 347 | ); |
| 336 | 348 | const rendering = performance.now(); |
| @@ -345,7 +357,10 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> { | ||
| 345 | 357 | return response; |
| 346 | 358 | } |
| 347 | 359 | |
| 348 | /** Whether a name and an address (optional, and read only where the forge sends mail) are fit for the first admin; else the words. */ | |
| 360 | /** | |
| 361 | * Whether a name and an address (optional, and read only where the forge sends mail) are fit for | |
| 362 | * the first admin; else the words. | |
| 363 | */ | |
| 349 | 364 | function problems( |
| 350 | 365 | ctx: Ctx, |
| 351 | 366 | spawned: Spawned, |
| @@ -368,14 +383,17 @@ function problems( | ||
| 368 | 383 | return errors; |
| 369 | 384 | } |
| 370 | 385 | |
| 371 | /** The address the form gave, on a forge that sends mail; nothing on one that does not, whatever was posted. */ | |
| 386 | /** | |
| 387 | * The address the form gave, on a forge that sends mail; nothing on one that does not, whatever was | |
| 388 | * posted. | |
| 389 | */ | |
| 372 | 390 | const emailGiven = (ctx: Ctx, raw: string): string => (mailConfigured(ctx.app) ? raw.trim() : ""); |
| 373 | 391 | |
| 374 | 392 | /** Why a passkey challenge was refused, in the passkey note's plain words. */ |
| 375 | 393 | const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = { |
| 376 | 394 | unknown: "This link does not work any more.", |
| 377 | used: "This link has already been used.", | |
| 378 | expired: "This link does not work any more.", | |
| 395 | used: "This link was already used.", | |
| 396 | expired: "This link has expired.", | |
| 379 | 397 | revoked: "This link does not work any more.", |
| 380 | 398 | owned: "This forge already has an owner.", |
| 381 | 399 | }; |
| @@ -394,7 +412,7 @@ export async function claimOptions(ctx: Ctx): Promise<Response> { | ||
| 394 | 412 | const spawned = await spawnedOrg(ctx.app); |
| 395 | 413 | if (spawned === undefined) return refuse(404, LINK_REFUSED.unknown); |
| 396 | 414 | const state = readClaim(ctx.app.db, spawned.org.id, str(data.token)).state; |
| 397 | if (state !== "ok") return refuse(state === "used" || state === "owned" ? 410 : 404, LINK_REFUSED[state]); | |
| 415 | if (state !== "ok") return refuse(state === "used" || state === "owned" || state === "expired" ? 410 : 404, LINK_REFUSED[state]); | |
| 398 | 416 | const username = str(data.username).trim(); |
| 399 | 417 | const bad = problems(ctx, spawned, username, emailGiven(ctx, str(data.email))); |
| 400 | 418 | const first = bad.username ?? bad.email; |
| @@ -419,7 +437,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> { | ||
| 419 | 437 | const before = readClaim(ctx.app.db, orgId, token); |
| 420 | 438 | const { spawner } = owners(ctx, spawned); |
| 421 | 439 | if (before.state !== "ok") |
| 422 | return dead(ctx, before.state, own ? undefined : spawner?.username, own); | |
| 440 | return dead(ctx, before.state, own ? undefined : sender(spawner), own); | |
| 423 | 441 | |
| 424 | 442 | const values = { |
| 425 | 443 | username: field(form, "username"), |
| @@ -461,7 +479,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> { | ||
| 461 | 479 | (founder === undefined && !takesName) || credential !== undefined ? "" : await Bun.password.hash(password, "argon2id"); |
| 462 | 480 | // Spent first, in one statement: of two people on one link, one wins. |
| 463 | 481 | const spent = burnClaim(ctx.app.db, orgId, token); |
| 464 | if (spent.state !== "ok") return dead(ctx, spent.state, undefined, own); | |
| 482 | if (spent.state !== "ok") return dead(ctx, spent.state, own ? undefined : sender(spawner), own); | |
| 465 | 483 | if (founder !== undefined) { |
| 466 | 484 | const taken = takeFounder(ctx.app.db, founder.id, { |
| 467 | 485 | hash: founderHash, |
test/claim-page-words.test.ts +5 −5
| @@ -80,7 +80,7 @@ describe("the claim page in plain words", () => { | ||
| 80 | 80 | expect(html).toContain("alice"); |
| 81 | 81 | }); |
| 82 | 82 | |
| 83 | test("an expired link answers 404 in plain words", async () => { | |
| 83 | test("an expired link answers 410: one sentence, and who sent it", async () => { | |
| 84 | 84 | setStored({ e: new Date(Date.now() - 3_600_000).toISOString() }); |
| 85 | 85 | for (const res of [ |
| 86 | 86 | await get(), |
| @@ -88,10 +88,10 @@ describe("the claim page in plain words", () => { | ||
| 88 | 88 | method: "POST", headers: { origin: childServers.url }, body: new URLSearchParams({ token }), |
| 89 | 89 | }), |
| 90 | 90 | ]) { |
| 91 | expect(res.status).toBe(404); | |
| 91 | expect(res.status).toBe(410); | |
| 92 | 92 | const text = await res.text(); |
| 93 | expect(text).toContain("This link does not work"); | |
| 94 | expect(text).toContain("ask whoever sent it for a new one"); | |
| 93 | expect(text).toContain("This link has expired"); | |
| 94 | expect(text).toContain("Ask alice to send you a new one"); | |
| 95 | 95 | expect(text).not.toMatch(/token|\b4\d\d\b/i); |
| 96 | 96 | } |
| 97 | 97 | }); |
| @@ -101,7 +101,7 @@ describe("the claim page in plain words", () => { | ||
| 101 | 101 | const res = await get(); |
| 102 | 102 | expect(res.status).toBe(410); |
| 103 | 103 | const text = await res.text(); |
| 104 | expect(text).toContain("This link has already been used"); | |
| 104 | expect(text).toContain("This link was already used"); | |
| 105 | 105 | expect(text).toContain("ask alice to send you a new one"); |
| 106 | 106 | expect(text).not.toMatch(/token|\b4\d\d\b/i); |
| 107 | 107 | }); |
test/forge-link-landing.test.ts +43 −5
| @@ -31,7 +31,7 @@ describe("a forge link's landing page", () => { | ||
| 31 | 31 | const row = child.db.query<{ value: string }, []>("SELECT value FROM user_setting WHERE key = 'spawn_claim'").get(); |
| 32 | 32 | child.db |
| 33 | 33 | .query("UPDATE user_setting SET value = ?1 WHERE key = 'spawn_claim'") |
| 34 | .run(JSON.stringify({ ...JSON.parse(row!.value), ...patch })); | |
| 34 | .run(JSON.stringify(Object.fromEntries(Object.entries({ ...JSON.parse(row!.value), ...patch }).filter(([, v]) => v !== undefined)))); | |
| 35 | 35 | }; |
| 36 | 36 | const future = () => new Date(Date.now() + 3_600_000).toISOString(); |
| 37 | 37 | |
| @@ -84,7 +84,7 @@ describe("a forge link's landing page", () => { | ||
| 84 | 84 | expect(text).not.toMatch(/href="[^"]*token[^"]*"/i); |
| 85 | 85 | }); |
| 86 | 86 | |
| 87 | test("revoked, expired and unknown answer the same page and status", async () => { | |
| 87 | test("revoked and unknown answer the same 404 and name no one; expired is a 410 naming the sender", async () => { | |
| 88 | 88 | setStored({ e: future(), r: new Date().toISOString() }); |
| 89 | 89 | const revoked = await get(); |
| 90 | 90 | setStored({ r: undefined, e: new Date(Date.now() - 3_600_000).toISOString() }); |
| @@ -94,18 +94,56 @@ describe("a forge link's landing page", () => { | ||
| 94 | 94 | headers: { origin: childServers.url }, |
| 95 | 95 | body: new URLSearchParams({ token: "x".repeat(43) }), |
| 96 | 96 | }); |
| 97 | const bodies = await Promise.all([revoked, expired, unknown].map((r) => r.text())); | |
| 98 | for (const r of [revoked, expired, unknown]) { | |
| 97 | const bodies = await Promise.all([revoked, unknown].map((r) => r.text())); | |
| 98 | for (const r of [revoked, unknown]) { | |
| 99 | 99 | expect(r.status).toBe(404); |
| 100 | 100 | expect(r.headers.get("x-content-type-options")).toBe("nosniff"); |
| 101 | 101 | expect(r.headers.get("set-cookie")).toBeNull(); |
| 102 | 102 | } |
| 103 | 103 | const strip = (t: string) => t.replace(/nonce="[^"]*"|<time[^>]*>/g, ""); |
| 104 | 104 | expect(strip(bodies[0]!)).toBe(strip(bodies[1]!)); |
| 105 | expect(strip(bodies[1]!)).toBe(strip(bodies[2]!)); | |
| 106 | 105 | expect(bodies[0]).toContain("does not work any more"); |
| 107 | 106 | expect(bodies[0]).not.toMatch(/expired|revoked|ended|alice|made-up/i); |
| 108 | 107 | expect(Buffer.byteLength(bodies[0]!)).toBeLessThan(20 * 1024); |
| 108 | expect(expired.status).toBe(410); | |
| 109 | expect(await expired.text()).toContain("This link has expired"); | |
| 110 | }); | |
| 111 | ||
| 112 | const sendersName = (name: string) => | |
| 113 | child.db.query("UPDATE user SET display_name = ?1 WHERE username = 'alice'").run(name); | |
| 114 | const main = (t: string) => t.slice(t.indexOf("<main"), t.indexOf("</main>")); | |
| 115 | const optionsFor = () => | |
| 116 | fetch(`${childServers.url}/claim/options`, { | |
| 117 | method: "POST", | |
| 118 | headers: { "content-type": "application/json", origin: childServers.url }, | |
| 119 | body: JSON.stringify({ token, username: "maria" }), | |
| 120 | }); | |
| 121 | ||
| 122 | test("a dead link says it once, then the next step; the sender is the display name, escaped", async () => { | |
| 123 | setStored({ r: undefined, e: new Date(Date.now() - 3_600_000).toISOString(), u: null }); | |
| 124 | sendersName("Alice <script>alert(1)</script>"); | |
| 125 | const expired = await (await get()).text(); | |
| 126 | expect(expired).toContain("Ask Alice <script>alert(1)</script> to send you a new one."); | |
| 127 | expect(expired).not.toContain("<script>alert(1)"); | |
| 128 | expect(main(expired).match(/has expired/g)?.length).toBe(1); | |
| 129 | setStored({ e: future(), u: new Date().toISOString() }); | |
| 130 | const used = await (await get()).text(); | |
| 131 | expect(used).toContain("This link was already used"); | |
| 132 | expect(main(used).match(/already used/g)?.length).toBe(1); | |
| 133 | expect(used).toContain("Alice <script>"); | |
| 134 | sendersName(""); | |
| 135 | expect(await (await get()).text()).toContain("ask alice to send you a new one"); | |
| 136 | setStored({ u: null }); | |
| 137 | }); | |
| 138 | ||
| 139 | test("the passkey path answers 410 for an expired link and a spent one, 404 for a revoked one", async () => { | |
| 140 | setStored({ r: undefined, u: null, e: new Date(Date.now() - 3_600_000).toISOString() }); | |
| 141 | expect((await optionsFor()).status).toBe(410); | |
| 142 | setStored({ e: future(), u: new Date().toISOString() }); | |
| 143 | expect((await optionsFor()).status).toBe(410); | |
| 144 | setStored({ u: null, r: new Date().toISOString() }); | |
| 145 | expect((await optionsFor()).status).toBe(404); | |
| 146 | setStored({ r: undefined, e: future() }); | |
| 109 | 147 | }); |
| 110 | 148 | |
| 111 | 149 | test("with no script: the server-rendered form, filled as a person would paste it, makes the account", async () => { |
test/spawn-first-boot.test.ts +3 −3
| @@ -350,7 +350,7 @@ describe("a spawned child's first boot", () => { | ||
| 350 | 350 | email: "[email protected]", |
| 351 | 351 | }); |
| 352 | 352 | expect(res.status).toBe(410); |
| 353 | expect(await res.text()).toContain("already been used"); | |
| 353 | expect(await res.text()).toContain("This link was already used"); | |
| 354 | 354 | expect(getUserByName(child.db, "mallory")).toBeUndefined(); |
| 355 | 355 | }); |
| 356 | 356 | |
| @@ -368,8 +368,8 @@ describe("a spawned child's first boot", () => { | ||
| 368 | 368 | username: "late", |
| 369 | 369 | email: "[email protected]", |
| 370 | 370 | }); |
| 371 | expect(res.status).toBe(404); | |
| 372 | expect(await res.text()).toContain("does not work"); | |
| 371 | expect(res.status).toBe(410); | |
| 372 | expect(await res.text()).toContain("This link has expired"); | |
| 373 | 373 | expect(getUserByName(child.db, "late")).toBeUndefined(); |
| 374 | 374 | }); |
| 375 | 375 |
test/stranger-words.test.ts +1 −1
| @@ -142,7 +142,7 @@ describe("a stranger's way to their own forge", () => { | ||
| 142 | 142 | await both("revoked", 404); |
| 143 | 143 | expect(restoreClaim(db, org)).toBe(true); |
| 144 | 144 | as({ e: "2000-01-01T00:00:00.000Z" }); |
| 145 | await both("expired", 404); | |
| 145 | await both("expired", 410); | |
| 146 | 146 | as({ u: new Date().toISOString() }); |
| 147 | 147 | await both("used", 410); |
| 148 | 148 | setStored(live); |
new above
offline
Non-blocking: the dead() parameter named sender shadows the new sender() helper above it. Rename the parameter (e.g. ask) so the next edit does not call the wrong one.