Public: anyone can read this; the crew can change it.
Retire the /claim/:token path form once no path link can be live #1005
mason/forge:mason/937-retire-the-claim-token-path-form-once-no → main 2 commits fork
7 files
changed +69
−76
docs/SPAWN.md +1 −1
| @@ -34,7 +34,7 @@ A self-hosted front door on celld is weighed in `docs/research/celld.md`. | ||
| 34 | 34 | |
| 35 | 35 | `direction` (optional, at most 2000 characters) is the spawner's words for what the child is for. Spawn writes it as each app's direction and carries the crew's roles (planner, builder, reviewer) from the parent's copy of each app. The first time the child runs, `src/spawn/first-boot.ts` opens the direction issue on its first app and assigns it to the planner. The child's organisation records that it booted, so a restart opens nothing more. |
| 36 | 36 | |
| 37 | Spawn mints one claim link (`src/spawn/claim.ts`): 32 random bytes, returned once as `claimUrl` in the `Spawned` result, kept in the child only as a sha256, single-use, and dead after 24 hours. The link is `https://<child>/claim#<token>`: the secret rides the fragment, which a browser never sends, so no server, edge or proxy log and no Referer sees it. `GET /claim` serves the form with nothing in it; a small script moves the code from the fragment into the form and drops it from the address bar (without JavaScript the code field takes a paste). Links in the older `/claim/<token>` form keep working until #937 removes them, no earlier than 24 hours after the first release that contains #836 (merged 2026-10-05) runs: links made before that may be in the path form, and a link lives 24 hours. `GET /claim` (or `/claim/:token`) shows the form and spends nothing, because a link unfurled in a chat app must not claim itself. `POST` (same-origin) makes the account, an admin and an owner of the child's organisation, burns the link in the same statement that checks it, and signs the person in. A used, expired or made-up link says which. | |
| 37 | Spawn mints one claim link (`src/spawn/claim.ts`): 32 random bytes, returned once as `claimUrl` in the `Spawned` result, kept in the child only as a sha256, single-use, and dead after 24 hours. The link is `https://<child>/claim#<token>`: the secret rides the fragment, which a browser never sends, so no server, edge or proxy log and no Referer sees it. `GET /claim` serves the form with nothing in it; a small script moves the code from the fragment into the form and drops it from the address bar (without JavaScript the code field takes a paste). The older `/claim/<token>` path form is gone (#937): `/claim/<anything>` is a 404, and the code is read only from the form's body, never from the path or a query string. `GET /claim` shows the form and spends nothing, because a link unfurled in a chat app must not claim itself. `POST` (same-origin) makes the account, an admin and an owner of the child's organisation, burns the link in the same statement that checks it, and signs the person in. A used, expired or made-up link says which. | |
| 38 | 38 | |
| 39 | 39 | Any site admin of the parent, not only the one who spawned it, can re-issue the link of a child nobody has claimed: "Send a fresh link" on `/forges`, `POST /api/v1/forges/:name/claim-link`, or the MCP `claim_link`. The child keeps one claim row, so the new link replaces the old, which stops working at once. A claimed child is refused (409). The new link is shown once and never kept by the idempotency table. |
| 40 | 40 |
src/http/server.ts +1 −3
| @@ -174,7 +174,7 @@ import { | ||
| 174 | 174 | tellSubmit, |
| 175 | 175 | } from "../web/studio/actions.ts"; |
| 176 | 176 | import { everydayCreate, everydayKeep, everydaySwitch } from "../web/pages/everyday.ts"; |
| 177 | import { claimFragmentPage, claimPage, claimSubmit } from "../web/pages/claim.ts"; | |
| 177 | import { claimFragmentPage, claimSubmit } from "../web/pages/claim.ts"; | |
| 178 | 178 | import { forgeLinkSubmit, forgesPage } from "../web/pages/forges.ts"; |
| 179 | 179 | import { newForgePage, newForgeSubmit } from "../web/pages/new-forge.ts"; |
| 180 | 180 | import { askedPage } from "../web/studio/asked_page.ts"; |
| @@ -529,13 +529,11 @@ export function buildRouter(): Router<Handler> { | ||
| 529 | 529 | // A spawned child's claim link: GET shows, POST takes it (spawn/claim.ts). |
| 530 | 530 | router.add("GET", "/claim", claimFragmentPage); |
| 531 | 531 | router.add("POST", "/claim", claimSubmit); |
| 532 | router.add("GET", "/claim/:token", claimPage); | |
| 533 | 532 | // "+ New forge": the spawn door as a page, an admin's only (spawn/door.ts). |
| 534 | 533 | router.add("GET", "/forges", forgesPage); |
| 535 | 534 | router.add("POST", "/forges/:name/claim-link", forgeLinkSubmit); |
| 536 | 535 | router.add("GET", "/forges/new", newForgePage); |
| 537 | 536 | router.add("POST", "/forges/new", newForgeSubmit); |
| 538 | router.add("POST", "/claim/:token", claimSubmit); | |
| 539 | 537 | // The front page as a stranger sees it, for anyone; a site admin there |
| 540 | 538 | // also gets the controls that decide it (#686). |
| 541 | 539 | router.add("GET", "/front", studioDoor); |
src/web/pages/claim.ts +18 −38
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | /** |
| 2 | * `/claim/:token`: the page a spawned child's link opens (`spawn/claim.ts`). | |
| 3 | * The path: the person a forge was made for opens the link they were sent, | |
| 2 | * `/claim`: the page a spawned child's link opens (`spawn/claim.ts`). | |
| 3 | * The link is `/claim#<code>`, so the code reaches only the browser. The path: the person a forge was made for opens the link they were sent, | |
| 4 | 4 | * reads who will be able to see it, makes their account, and lands in |
| 5 | 5 | * their forge as its admin. The GET only shows the form — a link unfurled |
| 6 | 6 | * in a chat app must not spend itself — and the POST, same-origin, makes |
| @@ -46,7 +46,7 @@ const USERNAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,38}$/; | ||
| 46 | 46 | interface Values { |
| 47 | 47 | readonly username?: string; |
| 48 | 48 | readonly email?: string; |
| 49 | /** The claim code, when it came in the body (the fragment link). */ | |
| 49 | /** The claim code, from the form's body. */ | |
| 50 | 50 | readonly token?: string; |
| 51 | 51 | } |
| 52 | 52 | type Errors = Partial<Record<"username" | "email" | "password", string>>; |
| @@ -133,18 +133,16 @@ function claimForm( | ||
| 133 | 133 | is the forge's admin, so it can change everything here. |
| 134 | 134 | </p> |
| 135 | 135 | <p class="mb" id="claim-access">${accessSentence(spawned, spawner)}</p> |
| 136 | <form method="post" action="${ctx.url.pathname}"> | |
| 137 | ${ctx.params["token"] === undefined | |
| 138 | ? html`<div id="claim-token-field"> | |
| 139 | ${formField({ | |
| 140 | label: "Claim code", | |
| 141 | name: "token", | |
| 142 | value: values.token ?? "", | |
| 143 | required: true, | |
| 144 | hint: "The part of your link after the #. Opening the link fills it in.", | |
| 145 | })} | |
| 146 | </div>` | |
| 147 | : null} | |
| 136 | <form method="post" action="/claim"> | |
| 137 | <div id="claim-token-field"> | |
| 138 | ${formField({ | |
| 139 | label: "Claim code", | |
| 140 | name: "token", | |
| 141 | value: values.token ?? "", | |
| 142 | required: true, | |
| 143 | hint: "The part of your link after the #. Opening the link fills it in.", | |
| 144 | })} | |
| 145 | </div> | |
| 148 | 146 | ${formField({ |
| 149 | 147 | label: "Username", |
| 150 | 148 | name: "username", |
| @@ -215,34 +213,16 @@ export async function claimFragmentPage(ctx: Ctx): Promise<Response> { | ||
| 215 | 213 | }); |
| 216 | 214 | } |
| 217 | 215 | |
| 218 | // GET /claim/:token — the form, or why the link no longer works. Spends nothing. | |
| 219 | // Links sent before the fragment form keep working here. | |
| 220 | export async function claimPage(ctx: Ctx): Promise<Response> { | |
| 221 | const spawned = await spawnedOrg(ctx.app); | |
| 222 | if (spawned === undefined) return dead(ctx, "unknown"); | |
| 223 | const claim = readClaim( | |
| 224 | ctx.app.db, | |
| 225 | spawned.org.id, | |
| 226 | ctx.params["token"] ?? "", | |
| 227 | ); | |
| 228 | if (claim.state !== "ok") return dead(ctx, claim.state); | |
| 229 | return page({ | |
| 230 | ctx, | |
| 231 | title: "This forge is yours", | |
| 232 | body: claimForm(ctx, spawned, owners(ctx, spawned).spawner, {}, {}), | |
| 233 | }); | |
| 234 | } | |
| 235 | ||
| 236 | // POST /claim/:token — make the account, burn the link, sign the person in. | |
| 216 | // POST /claim — make the account, burn the link, sign the person in. | |
| 237 | 217 | export async function claimSubmit(ctx: Ctx): Promise<Response> { |
| 238 | 218 | const form = await readForm(ctx); |
| 239 | 219 | if (form instanceof Response) return form; |
| 240 | 220 | const spawned = await spawnedOrg(ctx.app); |
| 241 | 221 | if (spawned === undefined) return dead(ctx, "unknown"); |
| 242 | 222 | const orgId = spawned.org.id; |
| 243 | // From the path for an old link, else from the form's body — never from | |
| 244 | // a query string, which proxies and logs would keep. | |
| 245 | const token = ctx.params["token"] ?? field(form, "token"); | |
| 223 | // From the form's body — never from the path or a query string, which | |
| 224 | // proxies and logs would keep. | |
| 225 | const token = field(form, "token"); | |
| 246 | 226 | const before = readClaim(ctx.app.db, orgId, token); |
| 247 | 227 | if (before.state !== "ok") return dead(ctx, before.state); |
| 248 | 228 | const { team, spawner } = owners(ctx, spawned); |
| @@ -250,7 +230,7 @@ export async function claimSubmit(ctx: Ctx): Promise<Response> { | ||
| 250 | 230 | const values = { |
| 251 | 231 | username: field(form, "username"), |
| 252 | 232 | email: field(form, "email"), |
| 253 | ...(ctx.params["token"] === undefined ? { token } : {}), | |
| 233 | token, | |
| 254 | 234 | }; |
| 255 | 235 | const password = field(form, "password"); |
| 256 | 236 | const errors: Errors = {}; |
test/first-login.test.ts +3 −5
| @@ -141,14 +141,12 @@ describe("the first login to a spawned forge", () => { | ||
| 141 | 141 | }); |
| 142 | 142 | await firstBoot(child); |
| 143 | 143 | servers = await serve(child); |
| 144 | const link = spawned.value.claimUrl!; | |
| 145 | const token = link.includes("#") | |
| 146 | ? link.split("#")[1]! | |
| 147 | : link.split("/claim/")[1]!; | |
| 148 | const claimed = await fetch(url(`/claim/${token}`), { | |
| 144 | const token = spawned.value.claimUrl!.split("/claim#")[1]!; | |
| 145 | const claimed = await fetch(url("/claim"), { | |
| 149 | 146 | method: "POST", |
| 150 | 147 | headers: { origin: servers.url }, |
| 151 | 148 | body: new URLSearchParams({ |
| 149 | token, | |
| 152 | 150 | username: "maria", |
| 153 | 151 | email: "[email protected]", |
| 154 | 152 | password: PASSWORD, |
test/spawn-child-apps.test.ts +2 −1
| @@ -131,10 +131,11 @@ describe("a spawned child's apps", () => { | ||
| 131 | 131 | await firstBoot(child); |
| 132 | 132 | servers = await serve(child); |
| 133 | 133 | const token = spawned.value.claimUrl!.split("/claim#")[1]!; |
| 134 | const claimed = await fetch(`${servers.url}/claim/${token}`, { | |
| 134 | const claimed = await fetch(`${servers.url}/claim`, { | |
| 135 | 135 | method: "POST", |
| 136 | 136 | headers: { origin: servers.url }, |
| 137 | 137 | body: new URLSearchParams({ |
| 138 | token, | |
| 138 | 139 | username: "maria", |
| 139 | 140 | email: "[email protected]", |
| 140 | 141 | password: "a-long-bakery-passphrase", |
test/spawn-first-boot.test.ts +42 −27
| @@ -31,6 +31,7 @@ import { createUser, getUserByName, type User } from "../src/core/users.ts"; | ||
| 31 | 31 | import { commitFiles } from "../src/git/write.ts"; |
| 32 | 32 | import { repoDir } from "../src/git/repos.ts"; |
| 33 | 33 | import { serve, SHOWN_ONCE_NOTE, type Servers } from "../src/http/server.ts"; |
| 34 | import { readClaim } from "../src/spawn/claim.ts"; | |
| 34 | 35 | import { firstBoot } from "../src/spawn/first-boot.ts"; |
| 35 | 36 | import { hostedPlacement } from "../src/spawn/placement.ts"; |
| 36 | 37 | import type { SpawnSource } from "../src/spawn/plan.ts"; |
| @@ -58,10 +59,10 @@ describe("a spawned child's first boot", () => { | ||
| 58 | 59 | form: Record<string, string>, |
| 59 | 60 | origin: string | null = servers.url, |
| 60 | 61 | ) => |
| 61 | fetch(url(`/claim/${t}`), { | |
| 62 | fetch(url("/claim"), { | |
| 62 | 63 | method: "POST", |
| 63 | 64 | headers: origin === null ? {} : { origin }, |
| 64 | body: new URLSearchParams(form), | |
| 65 | body: new URLSearchParams({ token: t, ...form }), | |
| 65 | 66 | redirect: "manual", |
| 66 | 67 | }); |
| 67 | 68 | const maria = { |
| @@ -246,15 +247,31 @@ describe("a spawned child's first boot", () => { | ||
| 246 | 247 | } |
| 247 | 248 | }); |
| 248 | 249 | |
| 249 | test("a made-up link says it is not a claim link", async () => { | |
| 250 | const page = await fetch(url(`/claim/${"x".repeat(43)}`)); | |
| 251 | expect(page.status).toBe(404); | |
| 252 | expect(await page.text()).toContain("not a claim link"); | |
| 253 | expect((await claim("x".repeat(43), maria)).status).toBe(404); | |
| 250 | // Whether the link can still be spent, read without spending it. | |
| 251 | const live = () => | |
| 252 | readClaim(child.db, getOrg(child.db, "bakery")!.id, token).state; | |
| 253 | ||
| 254 | test("a made-up code says it is not a claim link", async () => { | |
| 255 | const res = await claim("x".repeat(43), maria); | |
| 256 | expect(res.status).toBe(404); | |
| 257 | expect(await res.text()).toContain("not a claim link"); | |
| 258 | }); | |
| 259 | ||
| 260 | test("the old path form is gone: a code in the path is never read", async () => { | |
| 261 | expect((await fetch(url(`/claim/${token}`))).status).toBe(404); | |
| 262 | const res = await fetch(url(`/claim/${token}`), { | |
| 263 | method: "POST", | |
| 264 | headers: { origin: servers.url }, | |
| 265 | body: new URLSearchParams(maria), | |
| 266 | redirect: "manual", | |
| 267 | }); | |
| 268 | expect(res.status).toBe(404); | |
| 269 | expect(getUserByName(child.db, "maria")).toBeUndefined(); | |
| 270 | expect(live()).toBe("ok"); | |
| 254 | 271 | }); |
| 255 | 272 | |
| 256 | 273 | test("the link opens a page that asks before it acts: a GET claims nothing", async () => { |
| 257 | const page = await fetch(url(`/claim/${token}`)); | |
| 274 | const page = await fetch(url("/claim")); | |
| 258 | 275 | expect(page.status).toBe(200); |
| 259 | 276 | const text = await page.text(); |
| 260 | 277 | expect(text).toContain("This forge is yours"); |
| @@ -269,14 +286,14 @@ describe("a spawned child's first boot", () => { | ||
| 269 | 286 | const res = await claim(token, maria, "https://evil.test"); |
| 270 | 287 | expect(res.status).toBe(403); |
| 271 | 288 | expect(getUserByName(child.db, "maria")).toBeUndefined(); |
| 272 | expect((await fetch(url(`/claim/${token}`))).status).toBe(200); | |
| 289 | expect(live()).toBe("ok"); | |
| 273 | 290 | }); |
| 274 | 291 | |
| 275 | 292 | test("a weak password is refused, and the link is not spent on it", async () => { |
| 276 | 293 | const res = await claim(token, { ...maria, password: "short" }); |
| 277 | 294 | expect(res.status).toBe(422); |
| 278 | 295 | expect(getUserByName(child.db, "maria")).toBeUndefined(); |
| 279 | expect((await fetch(url(`/claim/${token}`))).status).toBe(200); | |
| 296 | expect(live()).toBe("ok"); | |
| 280 | 297 | }); |
| 281 | 298 | |
| 282 | 299 | test("claiming makes the person the child's admin and an owner of its organisation, signed in", async () => { |
| @@ -319,15 +336,13 @@ describe("a spawned child's first boot", () => { | ||
| 319 | 336 | }); |
| 320 | 337 | |
| 321 | 338 | test("a used link says so, and claims nothing a second time", async () => { |
| 322 | const page = await fetch(url(`/claim/${token}`)); | |
| 323 | expect(page.status).toBe(410); | |
| 324 | expect(await page.text()).toContain("already been used"); | |
| 325 | 339 | const res = await claim(token, { |
| 326 | 340 | ...maria, |
| 327 | 341 | username: "mallory", |
| 328 | 342 | email: "[email protected]", |
| 329 | 343 | }); |
| 330 | 344 | expect(res.status).toBe(410); |
| 345 | expect(await res.text()).toContain("already been used"); | |
| 331 | 346 | expect(getUserByName(child.db, "mallory")).toBeUndefined(); |
| 332 | 347 | }); |
| 333 | 348 | |
| @@ -340,13 +355,13 @@ describe("a spawned child's first boot", () => { | ||
| 340 | 355 | org.id, |
| 341 | 356 | Date.now() - 24 * 60 * 60 * 1000 - 60_000, |
| 342 | 357 | ); |
| 343 | const page = await fetch(url(`/claim/${old}`)); | |
| 344 | expect(page.status).toBe(410); | |
| 345 | expect(await page.text()).toContain("expired"); | |
| 346 | expect( | |
| 347 | (await claim(old, { ...maria, username: "late", email: "[email protected]" })) | |
| 348 | .status, | |
| 349 | ).toBe(410); | |
| 358 | const res = await claim(old, { | |
| 359 | ...maria, | |
| 360 | username: "late", | |
| 361 | email: "[email protected]", | |
| 362 | }); | |
| 363 | expect(res.status).toBe(410); | |
| 364 | expect(await res.text()).toContain("expired"); | |
| 350 | 365 | expect(getUserByName(child.db, "late")).toBeUndefined(); |
| 351 | 366 | }); |
| 352 | 367 | |
| @@ -387,12 +402,12 @@ describe("claims are bound to a spawned forge, and the link is shown once", () = | ||
| 387 | 402 | const { mintClaim } = await import("../src/spawn/claim.ts"); |
| 388 | 403 | const alice = getUserByName(parent.db, "alice")!; |
| 389 | 404 | const crafted = mintClaim(parent.db, alice.id); |
| 390 | const page = await fetch(`${servers.url}/claim/${crafted}`); | |
| 405 | const page = await fetch(`${servers.url}/claim`); | |
| 391 | 406 | expect(page.status).toBe(404); |
| 392 | const res = await fetch(`${servers.url}/claim/${crafted}`, { | |
| 407 | const res = await fetch(`${servers.url}/claim`, { | |
| 393 | 408 | method: "POST", |
| 394 | 409 | headers: { origin: "https://forge.test" }, |
| 395 | body: new URLSearchParams({ username: "mallory", email: "[email protected]", password: "a-long-mallory-passphrase" }), | |
| 410 | body: new URLSearchParams({ token: crafted, username: "mallory", email: "[email protected]", password: "a-long-mallory-passphrase" }), | |
| 396 | 411 | redirect: "manual", |
| 397 | 412 | }); |
| 398 | 413 | expect(res.status).toBe(404); |
| @@ -428,12 +443,12 @@ describe("claims are bound to a spawned forge, and the link is shown once", () = | ||
| 428 | 443 | await firstBoot(kid); |
| 429 | 444 | const kidServers = await serve(kid); |
| 430 | 445 | try { |
| 431 | const page = (await (await fetch(`${kidServers.url}/claim/${token}`)).text()).replace(/\s+/g, " "); | |
| 446 | const page = (await (await fetch(`${kidServers.url}/claim`)).text()).replace(/\s+/g, " "); | |
| 432 | 447 | expect(page).toContain("alice keeps owner access to this forge; you can remove it in your organisation"); |
| 433 | const res = await fetch(`${kidServers.url}/claim/${token}`, { | |
| 448 | const res = await fetch(`${kidServers.url}/claim`, { | |
| 434 | 449 | method: "POST", |
| 435 | 450 | headers: { origin: kidServers.url }, |
| 436 | body: new URLSearchParams({ username: "kai-owner", email: "[email protected]", password: "a-long-kai-passphrase" }), | |
| 451 | body: new URLSearchParams({ token, username: "kai-owner", email: "[email protected]", password: "a-long-kai-passphrase" }), | |
| 437 | 452 | redirect: "manual", |
| 438 | 453 | }); |
| 439 | 454 | expect(res.status).toBe(303); |
| @@ -443,7 +458,7 @@ describe("claims are bound to a spawned forge, and the link is shown once", () = | ||
| 443 | 458 | // A crafted row on any other account of the child is never consulted. |
| 444 | 459 | const { mintClaim } = await import("../src/spawn/claim.ts"); |
| 445 | 460 | const other = mintClaim(kid.db, getUserByName(kid.db, "alice")!.id); |
| 446 | expect((await fetch(`${kidServers.url}/claim/${other}`)).status).toBe(404); | |
| 461 | expect(readClaim(kid.db, org.id, other).state).toBe("unknown"); | |
| 447 | 462 | } finally { |
| 448 | 463 | kidServers.stop(); |
| 449 | 464 | kid.close(); |
test/spawn-keys.test.ts +2 −1
| @@ -348,10 +348,11 @@ describe("a spawned child's own keys and domain", () => { | ||
| 348 | 348 | const token = ( |
| 349 | 349 | JSON.parse(spawnBody) as { claimUrl: string } |
| 350 | 350 | ).claimUrl.split("/claim#")[1]!; |
| 351 | const claimed = await fetch(`${childServers.url}/claim/${token}`, { | |
| 351 | const claimed = await fetch(`${childServers.url}/claim`, { | |
| 352 | 352 | method: "POST", |
| 353 | 353 | headers: { origin: childServers.url }, |
| 354 | 354 | body: new URLSearchParams({ |
| 355 | token, | |
| 355 | 356 | username: "maria-owner", |
| 356 | 357 | email: "[email protected]", |
| 357 | 358 | password: "a-long-maria-passphrase", |
new above
offline