Public: anyone can read this; the crew can change it.

Retire the /claim/:token path form once no path link can be live #1005

Closed opened this in answer to
Closed without merging

mason/forge:mason/937-retire-the-claim-token-path-form-once-no → main 2 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
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`.
3434
3535 `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.
3636
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.
3838
3939 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.
4040
src/http/server.ts +1 −3
@@ -174,7 +174,7 @@ import {
174174 tellSubmit,
175175 } from "../web/studio/actions.ts";
176176 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";
178178 import { forgeLinkSubmit, forgesPage } from "../web/pages/forges.ts";
179179 import { newForgePage, newForgeSubmit } from "../web/pages/new-forge.ts";
180180 import { askedPage } from "../web/studio/asked_page.ts";
@@ -529,13 +529,11 @@ export function buildRouter(): Router<Handler> {
529529 // A spawned child's claim link: GET shows, POST takes it (spawn/claim.ts).
530530 router.add("GET", "/claim", claimFragmentPage);
531531 router.add("POST", "/claim", claimSubmit);
532 router.add("GET", "/claim/:token", claimPage);
533532 // "+ New forge": the spawn door as a page, an admin's only (spawn/door.ts).
534533 router.add("GET", "/forges", forgesPage);
535534 router.add("POST", "/forges/:name/claim-link", forgeLinkSubmit);
536535 router.add("GET", "/forges/new", newForgePage);
537536 router.add("POST", "/forges/new", newForgeSubmit);
538 router.add("POST", "/claim/:token", claimSubmit);
539537 // The front page as a stranger sees it, for anyone; a site admin there
540538 // also gets the controls that decide it (#686).
541539 router.add("GET", "/front", studioDoor);
src/web/pages/claim.ts +18 −38
@@ -1,6 +1,6 @@
11 /**
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,
44 * reads who will be able to see it, makes their account, and lands in
55 * their forge as its admin. The GET only shows the form — a link unfurled
66 * 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}$/;
4646 interface Values {
4747 readonly username?: string;
4848 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. */
5050 readonly token?: string;
5151 }
5252 type Errors = Partial<Record<"username" | "email" | "password", string>>;
@@ -133,18 +133,16 @@ function claimForm(
133133 is the forge's admin, so it can change everything here.
134134 </p>
135135 <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>
148146 ${formField({
149147 label: "Username",
150148 name: "username",
@@ -215,34 +213,16 @@ export async function claimFragmentPage(ctx: Ctx): Promise<Response> {
215213 });
216214 }
217215
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.
237217 export async function claimSubmit(ctx: Ctx): Promise<Response> {
238218 const form = await readForm(ctx);
239219 if (form instanceof Response) return form;
240220 const spawned = await spawnedOrg(ctx.app);
241221 if (spawned === undefined) return dead(ctx, "unknown");
242222 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");
246226 const before = readClaim(ctx.app.db, orgId, token);
247227 if (before.state !== "ok") return dead(ctx, before.state);
248228 const { team, spawner } = owners(ctx, spawned);
@@ -250,7 +230,7 @@ export async function claimSubmit(ctx: Ctx): Promise<Response> {
250230 const values = {
251231 username: field(form, "username"),
252232 email: field(form, "email"),
253 ...(ctx.params["token"] === undefined ? { token } : {}),
233 token,
254234 };
255235 const password = field(form, "password");
256236 const errors: Errors = {};
test/first-login.test.ts +3 −5
@@ -141,14 +141,12 @@ describe("the first login to a spawned forge", () => {
141141 });
142142 await firstBoot(child);
143143 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"), {
149146 method: "POST",
150147 headers: { origin: servers.url },
151148 body: new URLSearchParams({
149 token,
152150 username: "maria",
153151 email: "[email protected]",
154152 password: PASSWORD,
test/spawn-child-apps.test.ts +2 −1
@@ -131,10 +131,11 @@ describe("a spawned child's apps", () => {
131131 await firstBoot(child);
132132 servers = await serve(child);
133133 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`, {
135135 method: "POST",
136136 headers: { origin: servers.url },
137137 body: new URLSearchParams({
138 token,
138139 username: "maria",
139140 email: "[email protected]",
140141 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";
3131 import { commitFiles } from "../src/git/write.ts";
3232 import { repoDir } from "../src/git/repos.ts";
3333 import { serve, SHOWN_ONCE_NOTE, type Servers } from "../src/http/server.ts";
34 import { readClaim } from "../src/spawn/claim.ts";
3435 import { firstBoot } from "../src/spawn/first-boot.ts";
3536 import { hostedPlacement } from "../src/spawn/placement.ts";
3637 import type { SpawnSource } from "../src/spawn/plan.ts";
@@ -58,10 +59,10 @@ describe("a spawned child's first boot", () => {
5859 form: Record<string, string>,
5960 origin: string | null = servers.url,
6061 ) =>
61 fetch(url(`/claim/${t}`), {
62 fetch(url("/claim"), {
6263 method: "POST",
6364 headers: origin === null ? {} : { origin },
64 body: new URLSearchParams(form),
65 body: new URLSearchParams({ token: t, ...form }),
6566 redirect: "manual",
6667 });
6768 const maria = {
@@ -246,15 +247,31 @@ describe("a spawned child's first boot", () => {
246247 }
247248 });
248249
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");
254271 });
255272
256273 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"));
258275 expect(page.status).toBe(200);
259276 const text = await page.text();
260277 expect(text).toContain("This forge is yours");
@@ -269,14 +286,14 @@ describe("a spawned child's first boot", () => {
269286 const res = await claim(token, maria, "https://evil.test");
270287 expect(res.status).toBe(403);
271288 expect(getUserByName(child.db, "maria")).toBeUndefined();
272 expect((await fetch(url(`/claim/${token}`))).status).toBe(200);
289 expect(live()).toBe("ok");
273290 });
274291
275292 test("a weak password is refused, and the link is not spent on it", async () => {
276293 const res = await claim(token, { ...maria, password: "short" });
277294 expect(res.status).toBe(422);
278295 expect(getUserByName(child.db, "maria")).toBeUndefined();
279 expect((await fetch(url(`/claim/${token}`))).status).toBe(200);
296 expect(live()).toBe("ok");
280297 });
281298
282299 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", () => {
319336 });
320337
321338 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");
325339 const res = await claim(token, {
326340 ...maria,
327341 username: "mallory",
328342 email: "[email protected]",
329343 });
330344 expect(res.status).toBe(410);
345 expect(await res.text()).toContain("already been used");
331346 expect(getUserByName(child.db, "mallory")).toBeUndefined();
332347 });
333348
@@ -340,13 +355,13 @@ describe("a spawned child's first boot", () => {
340355 org.id,
341356 Date.now() - 24 * 60 * 60 * 1000 - 60_000,
342357 );
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");
350365 expect(getUserByName(child.db, "late")).toBeUndefined();
351366 });
352367
@@ -387,12 +402,12 @@ describe("claims are bound to a spawned forge, and the link is shown once", () =
387402 const { mintClaim } = await import("../src/spawn/claim.ts");
388403 const alice = getUserByName(parent.db, "alice")!;
389404 const crafted = mintClaim(parent.db, alice.id);
390 const page = await fetch(`${servers.url}/claim/${crafted}`);
405 const page = await fetch(`${servers.url}/claim`);
391406 expect(page.status).toBe(404);
392 const res = await fetch(`${servers.url}/claim/${crafted}`, {
407 const res = await fetch(`${servers.url}/claim`, {
393408 method: "POST",
394409 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" }),
396411 redirect: "manual",
397412 });
398413 expect(res.status).toBe(404);
@@ -428,12 +443,12 @@ describe("claims are bound to a spawned forge, and the link is shown once", () =
428443 await firstBoot(kid);
429444 const kidServers = await serve(kid);
430445 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, " ");
432447 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`, {
434449 method: "POST",
435450 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" }),
437452 redirect: "manual",
438453 });
439454 expect(res.status).toBe(303);
@@ -443,7 +458,7 @@ describe("claims are bound to a spawned forge, and the link is shown once", () =
443458 // A crafted row on any other account of the child is never consulted.
444459 const { mintClaim } = await import("../src/spawn/claim.ts");
445460 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");
447462 } finally {
448463 kidServers.stop();
449464 kid.close();
test/spawn-keys.test.ts +2 −1
@@ -348,10 +348,11 @@ describe("a spawned child's own keys and domain", () => {
348348 const token = (
349349 JSON.parse(spawnBody) as { claimUrl: string }
350350 ).claimUrl.split("/claim#")[1]!;
351 const claimed = await fetch(`${childServers.url}/claim/${token}`, {
351 const claimed = await fetch(`${childServers.url}/claim`, {
352352 method: "POST",
353353 headers: { origin: childServers.url },
354354 body: new URLSearchParams({
355 token,
355356 username: "maria-owner",
356357 email: "[email protected]",
357358 password: "a-long-maria-passphrase",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs