Public: anyone can read this; the crew can change it.
A forge link says when it expires on opening, and an expired link offers one way to ask for a fresh one #13564
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13525-a-forge-link-says-when-it-expires-on-ope → main 5 commits fork 3 threads · 3 unresolved
6 files
changed +179
−1
docs/features/README.md +1
| @@ -437,6 +437,7 @@ Read with the guide page [Every setting, by topic](../guide-settings.md). | ||
| 437 | 437 | - [](rotate-secret-key.md) |
| 438 | 438 | - [](signin-asking.md) |
| 439 | 439 | - [](claim-own-name.md) |
| 440 | - [](claim-renew.md) | |
| 440 | 441 | - [](trusted-sources.md) |
| 441 | 442 | - [](visibility-line.md) |
| 442 | 443 | - [](dotfiles.md) |
docs/features/claim-renew.md added +14
| @@ -0,0 +1,14 @@ | ||
| 1 | # Ask for a new forge link | |
| 2 | ||
| 3 | When a forge link has expired, the claim page says so and offers one action: **Ask for a new link**. It records one | |
| 4 | ask on this forge's own feed, once per link (a repeat answers the same and records nothing). It does not reach the | |
| 5 | sender, who lives on the parent forge, so the page says the ask is recorded and tells the person to ask whoever sent | |
| 6 | the link too. | |
| 7 | Only an expired, unused, unrevoked link can ask; every other state answers the same plain 404 as a made-up link. | |
| 8 | ||
| 9 | Try it: open `/claim` on a forge whose link has expired and press **Ask for a new link**. | |
| 10 | ||
| 11 | The terminal (both doors): the claim link is a web-only handoff for a person with no account yet, so `src/tui` has no | |
| 12 | claim screen to bring in line. The admins see the event on the same feed as other forge events. | |
| 13 | ||
| 14 | Pinned by `test/forge-link-expiry.test.ts`. |
src/core/event_audience.ts +1
| @@ -130,6 +130,7 @@ const READER_KINDS: ReadonlySet<string> = new Set([ | ||
| 130 | 130 | "forge.claim_reissued", |
| 131 | 131 | "forge.claim_revoked", |
| 132 | 132 | "forge.claim_restored", |
| 133 | "forge.claim_renewal_asked", | |
| 133 | 134 | "agent.created", |
| 134 | 135 | "agent.updated", |
| 135 | 136 | "agent.deleted", |
src/http/server.ts +2 −1
| @@ -310,7 +310,7 @@ import { | ||
| 310 | 310 | import { shareTellPage } from "../web/studio/share_tell.ts"; |
| 311 | 311 | import { forgeReady } from "../web/pages/forge-ready.ts"; |
| 312 | 312 | import { forgeName } from "../web/pages/forge-name.ts"; |
| 313 | import { claimFragmentPage, claimOptions, claimSubmit, welcomePage, welcomeSubmit } from "../web/pages/claim.ts"; | |
| 313 | import { claimFragmentPage, claimOptions, claimRenew, claimSubmit, welcomePage, welcomeSubmit } from "../web/pages/claim.ts"; | |
| 314 | 314 | import { firstAdminOptions, firstAdminPage, firstAdminSubmit } from "../web/pages/first-admin.ts"; |
| 315 | 315 | import { familyPage } from "../web/pages/family.ts"; |
| 316 | 316 | import { forgeLinkForm, forgeLinkRevoke, forgeLinkSubmit, forgeLinkUndo, forgeArchiveConfirm, forgeArchiveSubmit, forgeRestoreSubmit, forgeRemoveConfirm, forgeRemoveSubmit, forgeStopSubmit, |
| @@ -933,6 +933,7 @@ export function buildRouter(): Router<Handler> { | ||
| 933 | 933 | router.add("GET", "/claim", claimFragmentPage); |
| 934 | 934 | router.add("POST", "/claim", claimSubmit); |
| 935 | 935 | router.add("POST", "/claim/options", claimOptions); |
| 936 | router.add("POST", "/claim/renew", claimRenew); | |
| 936 | 937 | router.add("GET", "/welcome", welcomePage); |
| 937 | 938 | router.add("POST", "/welcome", welcomeSubmit); |
| 938 | 939 | router.add("POST", "/welcome/options", claimOptions); |
src/web/pages/claim.ts +31
| @@ -155,6 +155,11 @@ function dead( | ||
| 155 | 155 | ${pageHead(says.title)} |
| 156 | 156 | <section class="settings-block"> |
| 157 | 157 | <p>${says.text}</p> |
| 158 | ${state === "expired" && !own | |
| 159 | ? html`<form method="post" action="/claim/renew" class="form-actions"> | |
| 160 | ${btn("Ask for a new link", { type: "submit", variant: "primary" })} | |
| 161 | </form>` | |
| 162 | : null} | |
| 158 | 163 | ${state === "used" || state === "owned" |
| 159 | 164 | ? html`<div class="form-actions"> |
| 160 | 165 | ${btn("Sign in", { href: "/signin", variant: "primary" })} |
| @@ -420,6 +425,32 @@ export async function claimOptions(ctx: Ctx): Promise<Response> { | ||
| 420 | 425 | return json(makeOptions(ctx, "claim", username)); |
| 421 | 426 | } |
| 422 | 427 | |
| 428 | /** | |
| 429 | * POST /claim/renew — "Ask for a new link" on an expired link. It records the ask on this forge's | |
| 430 | * own feed, once per link: a repeat answers the same sentence and records nothing. It does not | |
| 431 | * reach the sender: they live on the parent forge, and no path from here to them exists, so the | |
| 432 | * page says "recorded", never "told". Only an expired, unused, unrevoked link of a spawned forge | |
| 433 | * can ask; every other state answers the same plain 404 as a made-up link. | |
| 434 | */ | |
| 435 | export async function claimRenew(ctx: Ctx): Promise<Response> { | |
| 436 | const form = await readForm(ctx); | |
| 437 | if (form instanceof Response) return form; | |
| 438 | const spawned = await spawnedOrg(ctx.app); | |
| 439 | const link = linkOf(ctx.app.db); | |
| 440 | if (spawned === undefined || link === null || link.state !== "ended" || link.revokedAt !== null || link.usedAt !== null) | |
| 441 | return dead(ctx, "unknown"); | |
| 442 | const open = ctx.app.db | |
| 443 | .query("SELECT 1 FROM event WHERE kind = 'forge.claim_renewal_asked' AND json_extract(payload, '$.link') = ?1 LIMIT 1") | |
outdated
HIGH: this event stays on the child forge. The sender is not told. Deliver it to the sender, or to the parent forge they read, and dedupe repeat presses per link. |
||
| 444 | .get(link.expiresAt ?? ""); | |
| 445 | if (open === null) | |
| 446 | ctx.app.bus.append({ kind: "forge.claim_renewal_asked", payload: { org: spawned.org.username, link: link.expiresAt ?? "" } }); | |
| 447 | return page({ | |
| 448 | ctx, | |
outdated
HIGH: 'We have told' is untrue until a delivery happens. Principle 1: one honest sentence, and the title above should not repeat it. |
||
| 449 | title: "Ask recorded", | |
| 450 | body: html`<div class="narrow">${pageHead("Ask recorded")}<section class="settings-block"><p id="claim-asked">Your ask is recorded on this forge's feed. It has not reached whoever sent the link, so ask them for a new one too.</p></section></div>`, | |
| 451 | }); | |
| 452 | } | |
| 453 | ||
| 423 | 454 | // POST /claim — make the account, burn the link, sign the person in. |
| 424 | 455 | export const claimSubmit = (ctx: Ctx): Promise<Response> => submit(ctx, false); |
| 425 | 456 | /** POST /welcome — the same, for the person who started the forge. */ |
test/forge-link-expiry.test.ts added +130
| @@ -0,0 +1,130 @@ | ||
| 1 | /** | |
| 2 | * A forge link says when it expires on opening, and an expired one offers one way to ask for a | |
| 3 | * fresh link (#13525): a form that records an event for the forge's admins, and nothing for a | |
| 4 | * link that is not expired. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { childDirOf } from "../src/actions/spawn.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { firstBoot } from "../src/spawn/first-boot.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | describe("a forge link's expiry", () => { | |
| 21 | let dir = ""; | |
| 22 | let parent: App; | |
| 23 | let parentServers: Servers; | |
| 24 | let child: App; | |
| 25 | let childServers: Servers; | |
| 26 | ||
| 27 | const get = () => fetch(`${childServers.url}/claim`); | |
| 28 | const renew = (origin = childServers.url) => | |
| 29 | fetch(`${childServers.url}/claim/renew`, { method: "POST", headers: { origin }, body: new URLSearchParams({}) }); | |
| 30 | const setExpiry = (ms: number) => { | |
| 31 | const row = child.db.query<{ value: string }, []>("SELECT value FROM user_setting WHERE key = 'spawn_claim'").get(); | |
| 32 | child.db | |
| 33 | .query("UPDATE user_setting SET value = ?1 WHERE key = 'spawn_claim'") | |
| 34 | .run(JSON.stringify({ ...JSON.parse(row!.value), e: new Date(Date.now() + ms).toISOString(), r: undefined })); | |
| 35 | }; | |
| 36 | const asked = () => child.db.query("SELECT 1 FROM event WHERE kind = 'forge.claim_renewal_asked'").all().length; | |
| 37 | ||
| 38 | beforeAll(async () => { | |
| 39 | dir = await mkdtemp(join(tmpdir(), "forge-expiry-")); | |
| 40 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 41 | const alice = await createUser(parent.db, { | |
| 42 | origin: "seed", trusted: true, username: "alice", email: "[email protected]", | |
| 43 | password: "a-long-claim-passphrase", isAdmin: true, | |
| 44 | }); | |
| 45 | if (!alice.ok) throw new Error("user"); | |
| 46 | const rw = createToken(parent.db, alice.value.id, "rw", "rw").token; | |
| 47 | parentServers = await serve(parent); | |
| 48 | const made = await fetch(`${parentServers.url}/api/v1/forges`, { | |
| 49 | method: "POST", | |
| 50 | headers: { authorization: `Bearer ${rw}`, "content-type": "application/json" }, | |
| 51 | body: JSON.stringify({ name: "maria", apps: [], dryRun: false }), | |
| 52 | }); | |
| 53 | if (made.status !== 201) throw new Error(`spawn ${made.status}`); | |
| 54 | const body = (await made.json()) as { bodyId: string }; | |
| 55 | child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); | |
| 56 | await firstBoot(child); | |
| 57 | childServers = await serve(child); | |
| 58 | }, 60_000); | |
| 59 | ||
| 60 | afterAll(async () => { | |
| 61 | childServers.stop(); | |
| 62 | child.close(); | |
| 63 | parentServers.stop(); | |
| 64 | parent.close(); | |
| 65 | await rm(dir, { recursive: true, force: true }); | |
| 66 | }); | |
| 67 | ||
| 68 | test("a live link says when it ends, in plain words, and offers no ask", async () => { | |
| 69 | setExpiry(3 * 3_600_000); | |
| 70 | const text = await (await get()).text(); | |
| 71 | expect(text).toContain('id="claim-expiry"'); | |
| 72 | expect(text).toMatch(/This link works until/); | |
| 73 | expect(text).toMatch(/in 3 hours/); | |
| 74 | expect(text).not.toContain("/claim/renew"); | |
| 75 | expect((await renew()).status).toBe(404); | |
| 76 | expect(asked()).toBe(0); | |
| 77 | }); | |
| 78 | ||
| 79 | test("an expired link answers one sentence and one action; asking records one ask", async () => { | |
| 80 | setExpiry(-3_600_000); | |
| 81 | const res = await get(); | |
| 82 | const text = await res.text(); | |
| 83 | expect(res.status).toBe(410); | |
| 84 | expect(text).toContain("This link has expired"); | |
| 85 | expect(text).toContain('action="/claim/renew"'); | |
| 86 | expect(text).toContain("Ask for a new link"); | |
| 87 | expect(text).not.toContain('name="username"'); | |
| 88 | const done = await renew(); | |
| 89 | expect(done.status).toBe(200); | |
| 90 | const said = await done.text(); | |
| 91 | expect(said).toContain("Your ask is recorded"); | |
| 92 | expect(said).not.toContain("We have told"); | |
| 93 | expect(asked()).toBe(1); | |
| 94 | }); | |
| 95 | ||
| 96 | test("a repeat ask answers the same and records nothing more", async () => { | |
| 97 | const again = await renew(); | |
| 98 | expect(again.status).toBe(200); | |
| 99 | expect(await again.text()).toContain("Your ask is recorded"); | |
| 100 | for (let i = 0; i < 5; i++) await renew(); | |
| 101 | expect(asked()).toBe(1); | |
| 102 | }); | |
| 103 | ||
| 104 | test("a revoked or used link answers 404 to an ask and records nothing", async () => { | |
| 105 | const key = "SELECT value FROM user_setting WHERE key = 'spawn_claim'"; | |
| 106 | const original = child.db.query<{ value: string }, []>(key).get()!.value; | |
| 107 | const write = (v: object) => child.db.query("UPDATE user_setting SET value = ?1 WHERE key = 'spawn_claim'").run(JSON.stringify(v)); | |
| 108 | const stored = JSON.parse(original); | |
| 109 | const before = asked(); | |
| 110 | write({ ...stored, e: new Date(Date.now() - 7_200_000).toISOString(), r: new Date().toISOString() }); | |
| 111 | expect((await renew()).status).toBe(404); | |
| 112 | write({ ...stored, e: new Date(Date.now() - 7_200_000).toISOString(), r: undefined, u: new Date().toISOString() }); | |
| 113 | expect((await renew()).status).toBe(404); | |
| 114 | write(stored); | |
| 115 | expect(asked()).toBe(before); | |
| 116 | }); | |
| 117 | ||
| 118 | test("a fresh expired link may ask once more", async () => { | |
| 119 | setExpiry(-1_800_000); | |
| 120 | await renew(); | |
| 121 | expect(asked()).toBe(2); | |
| 122 | }); | |
| 123 | ||
| 124 | test("a cross-site ask is refused and records nothing", async () => { | |
| 125 | const before = asked(); | |
| 126 | const res = await renew("https://evil.test"); | |
| 127 | expect(res.status).toBeGreaterThanOrEqual(400); | |
| 128 | expect(asked()).toBe(before); | |
| 129 | }); | |
| 130 | }); |
new above
offline
MEDIUM: a hand-rolled 429 page with inline markup. Principle 1 and the named-components rule: reuse a shared helper. Add a test for this branch.