Public: anyone can read this; the crew can change it.
The claim link of a forge made for a client can be regenerated, and the old one stops working #14088
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14068-the-claim-link-of-a-forge-made-for-a-cli → main 5 commits fork 2 threads · 2 unresolved
6 files
changed +110
−6
docs/features/README.md +1
| @@ -343,6 +343,7 @@ Read with the guide page [Hosting forges for others, and getting paid](../guide- | ||
| 343 | 343 | - [](child-proposal-credential.md) |
| 344 | 344 | - [](forge-bill.md) |
| 345 | 345 | - [](client-forge.md) |
| 346 | - [](regenerate-client-link.md) | |
| 346 | 347 | - [](client-link-times.md) |
| 347 | 348 | - [](lapsed-forge.md) |
| 348 | 349 | - [](forge-link-qr.md) |
docs/features/regenerate-client-link.md added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | # Regenerating a client forge's claim link (#14068) | |
| 2 | ||
| 3 | A forge you made for a client is claimed with one link. If it was lost, or went to the wrong person, make a new one: open **Forges you sent** (`/forges`), choose the forge, and ask for a fresh link (`/forges/<name>/claim-link`). | |
| 4 | ||
| 5 | - The first page only says what will happen and changes nothing. Confirming it makes the new link. | |
| 6 | - The answer shows the new link once, with the time it works until, in UTC (for example "Works once, until Nov 23, 10:00 UTC"). That time is read from what the client forge stored, never worked out again here. | |
| 7 | - The old link stops working the moment the new one is made. Once the forge is claimed there is nothing to regenerate. | |
| 8 | - No other page holds the link: the list and the preview never show it, and the answer is not cached. Lose it and you make another. | |
| 9 | - Who may: the account that made the forge, or a site admin. The terminal does not show claim links, so this has no terminal counterpart. | |
| 10 | - The answer page is a result of a POST, so it has no entry in `app.looks` and no picture; `test/claim-link-regenerate.test.ts` covers it. |
src/actions/spawn.ts +6 −4
| @@ -64,7 +64,7 @@ import { | ||
| 64 | 64 | import { childSubscription } from "../spawn/child_db.ts"; |
| 65 | 65 | import { loadKey, open } from "../kernel/secret.ts"; |
| 66 | 66 | import { loadOrCreateHostKey } from "../ssh/keys.ts"; |
| 67 | import { claimLink, hasAdmin, mintClaim, PLACEHOLDER_FOUNDER, spawnedOrg } from "../spawn/claim.ts"; | |
| 67 | import { claimLink, hasAdmin, linkOf, mintClaim, PLACEHOLDER_FOUNDER, spawnedOrg } from "../spawn/claim.ts"; | |
| 68 | 68 | import { childPublicUrl, parentOrigin, type Placement, type PlacementKind } from "../spawn/placement.ts"; |
| 69 | 69 | import { quotaRefusal } from "../spawn/quota.ts"; |
| 70 | 70 | import { personRefusal, planRefusal } from "../spawn/sleep.ts"; |
| @@ -843,7 +843,7 @@ export async function reissueClaim( | ||
| 843 | 843 | actor: User | undefined, |
| 844 | 844 | name: string, |
| 845 | 845 | deps: { readonly request: Request; readonly url: URL }, |
| 846 | ): Promise<Result<{ readonly name: string; readonly claimUrl: string }, SpawnError | { readonly kind: "not_found" }>> { | |
| 846 | ): Promise<Result<{ readonly name: string; readonly claimUrl: string; readonly expiresAt: string }, SpawnError | { readonly kind: "not_found" }>> { | |
| 847 | 847 | if (crossOrigin(deps.request, deps.url, app.log)) return err({ kind: "cross_origin" }); |
| 848 | 848 | const allowed = await mayReissue(app, actor, name); |
| 849 | 849 | if (!allowed.ok) return allowed; |
| @@ -898,7 +898,7 @@ export async function previewReissue( | ||
| 898 | 898 | export async function reissueClaimFor( |
| 899 | 899 | app: App, |
| 900 | 900 | name: string, |
| 901 | ): Promise<Result<{ readonly name: string; readonly claimUrl: string }, SpawnError | { readonly kind: "not_found" }>> { | |
| 901 | ): Promise<Result<{ readonly name: string; readonly claimUrl: string; readonly expiresAt: string }, SpawnError | { readonly kind: "not_found" }>> { | |
| 902 | 902 | const ledger = /^[a-z0-9-]{1,39}$/.test(name) ? await readLedger(app, name) : undefined; |
| 903 | 903 | if (!ledger || !ledger.done) return err({ kind: "not_found" }); |
| 904 | 904 | const child = createApp({ dataDir: childDirOf(app, ledger.bodyId) }); |
| @@ -908,7 +908,9 @@ export async function reissueClaimFor( | ||
| 908 | 908 | if (hasAdmin(child.db)) |
| 909 | 909 | return err({ kind: "refused", reason: "claimed", message: `${name} is already claimed; ask its owner to invite you` }); |
| 910 | 910 | const token = mintClaim(child.db, spawned.org.id); |
| 911 | return ok({ name, claimUrl: claimLink(liveChildUrl(app, name), token) }); | |
| 911 | const expiresAt = linkOf(child.db)?.expiresAt; | |
| 912 | if (expiresAt === undefined) return fail("the new link was not stored, so it has no expiry to show"); | |
| 913 | return ok({ name, claimUrl: claimLink(liveChildUrl(app, name), token), expiresAt }); | |
| 912 | 914 | } finally { |
| 913 | 915 | child.close(); |
| 914 | 916 | } |
src/web/pages/claim.ts +1 −1
| @@ -88,7 +88,7 @@ interface Values { | ||
| 88 | 88 | type Errors = Partial<Record<"username" | "email" | "password" | "passkey", string>>; |
| 89 | 89 | |
| 90 | 90 | /** `Nov 12, 14:30 UTC`, from an ISO time. */ |
| 91 | function whenWords(iso: string): string { | |
| 91 | export function whenWords(iso: string): string { | |
| 92 | 92 | const d = new Date(iso); |
| 93 | 93 | const month = d.toLocaleString("en-US", { month: "short", timeZone: "UTC" }); |
| 94 | 94 | return `${month} ${d.getUTCDate()}, ${iso.slice(11, 16)} UTC`; |
src/web/pages/forges.ts +2 −1
| @@ -29,6 +29,7 @@ import { moveToCell } from "../../spawn/move_cell.ts"; | ||
| 29 | 29 | import { sealedAt } from "../../spawn/sealed.ts"; |
| 30 | 30 | import { countRefusals, mayReadRefusals } from "../../core/first_ask_refusals.ts"; |
| 31 | 31 | import { claimLinkBody } from "./new-forge.ts"; |
| 32 | import { whenWords } from "./claim.ts"; | |
| 32 | 33 | import { appLinkSummary, appLinkWords } from "../../spawn/app_links.ts"; |
| 33 | 34 | import { REACH_SETTINGS, reachByForge, type ForgeReach } from "../../actions/child_reach.ts"; |
| 34 | 35 | import { forgeReachRows } from "./hosted-forges.ts"; |
| @@ -421,7 +422,7 @@ export async function forgeLinkSubmit(ctx: Ctx): Promise<Response> { | ||
| 421 | 422 | ${pageHead(typeof link === "string" ? `A fresh link for ${name}` : `No fresh link for ${name}`)} |
| 422 | 423 | <section class="settings-block"> |
| 423 | 424 | ${typeof link === "string" |
outdated
Principle 8 and 1: the raw ISO timestamp is not plain words, and two sentences explain what the page already shows (the old link has stopped is stated by the preview). Format the expiry for a person and keep one sentence. |
||
| 424 | ? claimLinkBody(link, true) | |
| 425 | ? html`${claimLinkBody(link, true)}<p class="muted" data-expires>Works once, until ${whenWords(String(door.body["expiresAt"] ?? ""))}.</p>` | |
| 425 | 426 | : html`<p>${String(door.body["message"] ?? "")}</p> |
| 426 | 427 | <p class="mt">${btn("Forges you sent", { href: "/forges", size: "sm" })}</p>`} |
| 427 | 428 | </section> |
test/claim-link-regenerate.test.ts added +90
| @@ -0,0 +1,90 @@ | ||
| 1 | /** | |
| 2 | * Regenerating a client forge's claim link (#14068): the preview changes nothing, the confirmed | |
| 3 | * answer shows the new link and its expiry once, the old link dies, and no page holds a link | |
| 4 | * afterwards. | |
| 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 { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createToken } from "../src/core/tokens.ts"; | |
| 14 | import { createUser } from "../src/core/users.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { firstBoot } from "../src/spawn/first-boot.ts"; | |
| 17 | import { quietLogs } from "./helpers/globals.ts"; | |
| 18 | ||
| 19 | quietLogs(); | |
| 20 | ||
| 21 | describe("regenerating a claim link", () => { | |
| 22 | let dir = ""; | |
| 23 | let parent: App; | |
| 24 | let child: App; | |
| 25 | let servers: Servers; | |
| 26 | let kid: Servers; | |
| 27 | let cookie = ""; | |
| 28 | let first = ""; | |
| 29 | const tokenOf = (url: string) => url.split("#")[1] ?? ""; | |
| 30 | const claim = (t: string) => | |
| 31 | fetch(`${kid.url}/claim`, { | |
| 32 | method: "POST", | |
| 33 | headers: { origin: kid.url }, | |
| 34 | body: new URLSearchParams({ token: t, username: "someone", email: "[email protected]" }), | |
| 35 | }); | |
| 36 | ||
| 37 | beforeAll(async () => { | |
| 38 | dir = await mkdtemp(join(tmpdir(), "forge-claim-regen-")); | |
| 39 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 40 | const made = await createUser(parent.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-claim-passphrase", isAdmin: true }); | |
| 41 | if (!made.ok) throw new Error("user"); | |
| 42 | const token = createToken(parent.db, made.value.id, "rw", "rw").token; | |
| 43 | cookie = `forge_session=${createSession(parent.db, made.value.id).token}`; | |
| 44 | servers = await serve(parent); | |
| 45 | const res = await fetch(`${servers.url}/api/v1/forges`, { | |
| 46 | method: "POST", | |
| 47 | headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, | |
| 48 | body: JSON.stringify({ name: "rosa", for: "Rosa", dryRun: false }), | |
| 49 | }); | |
| 50 | if (res.status !== 201) throw new Error(`spawn ${res.status}`); | |
| 51 | const body = (await res.json()) as { claimUrl: string; bodyId: string }; | |
| 52 | first = body.claimUrl; | |
| 53 | child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); | |
| 54 | await firstBoot(child); | |
| 55 | kid = await serve(child); | |
| 56 | }, 60_000); | |
| 57 | ||
| 58 | afterAll(async () => { | |
| 59 | kid.stop(); | |
| 60 | child.close(); | |
| 61 | servers.stop(); | |
| 62 | parent.close(); | |
| 63 | await rm(dir, { recursive: true, force: true }); | |
| 64 | }); | |
| 65 | ||
| 66 | test("preview holds no link; confirm shows the new link and its expiry once; the old link is dead; later pages hold none", async () => { | |
| 67 | const preview = await (await fetch(`${servers.url}/forges/rosa/claim-link`, { headers: { cookie } })).text(); | |
| 68 | expect(preview).not.toContain("/claim#"); | |
| 69 | expect((await claim(tokenOf(first))).status).not.toBe(404); | |
| 70 | ||
| 71 | const res = await fetch(`${servers.url}/forges/rosa/claim-link`, { method: "POST", headers: { cookie, origin: "https://forge.test" } }); | |
| 72 | expect(res.status).toBe(201); | |
| 73 | expect(res.headers.get("cache-control")).toContain("no-store"); | |
| 74 | const shown = await res.text(); | |
| 75 | const link = /\/claim#([A-Za-z0-9_-]{43})/.exec(shown)?.[1] ?? ""; | |
| 76 | expect(link).not.toBe(""); | |
| 77 | expect(link).not.toBe(tokenOf(first)); | |
| 78 | expect(shown).toMatch(/Works once, until [A-Z][a-z]{2} \d{1,2}, \d\d:\d\d UTC\./); | |
| 79 | expect(shown).not.toMatch(/20\d\d-\d\d-\d\dT/); | |
| 80 | ||
| 81 | expect((await claim(tokenOf(first))).status).toBe(404); | |
| 82 | ||
| 83 | for (const path of ["/forges", "/forges/rosa/claim-link"]) { | |
| 84 | const again = await (await fetch(`${servers.url}${path}`, { headers: { cookie } })).text(); | |
| 85 | expect(again).not.toContain(link); | |
| 86 | expect(again).not.toContain("/claim#"); | |
| 87 | } | |
| 88 | expect((await claim(link)).status).not.toBe(404); | |
| 89 | }); | |
| 90 | }); |
new above
offline
The fallback computes a fresh Date.now()+TTL that may differ from the stored expiry, so the page can show a time the link does not have. Use the value mintClaim stored, or return an error if linkOf is missing.