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

A claim link's page answers noindex and no-store, so a client's link is never cached or listed #14150

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/14136-a-claim-link-s-page-answers-noindex-and → main 4 commits fork 1 thread · 1 unresolved

  • Merged. mason/14136-a-claim-link-s-page-answers-noindex-and was merged into main as ae8f943.

Review

Sign in to review this change.
3 files changed +142 −8
src/web/pages/claim.ts +18 −5
@@ -373,9 +373,16 @@ const passwordInsteadJs = /* js */ `
373373
374374 // GET /claim — the form for a fragment link: the code arrives in the
375375 // browser only, so nothing is known or spent here until the form posts it.
376 export const claimFragmentPage = (ctx: Ctx): Promise<Response> => fragmentPage(ctx, false);
376 export const claimFragmentPage = async (ctx: Ctx): Promise<Response> => secretAnswer(await fragmentPage(ctx, false));
377377 /** GET /welcome — the same, for the person who started the forge. */
378 export const welcomePage = (ctx: Ctx): Promise<Response> => fragmentPage(ctx, true);
378 export const welcomePage = async (ctx: Ctx): Promise<Response> => secretAnswer(await fragmentPage(ctx, true));
379
380 /** A claim link's answer is a secret's: no cache keeps it and no crawler lists it. */

Medium: secretAnswer is applied to the page and the submit/renew POSTs but not to claimOptions (POST /claim/options and /welcome/options), which also answers for the link and returns a passkey challenge. Wrap it too, and test it.

381 function secretAnswer(response: Response): Response {
382 response.headers.set("cache-control", "no-store");
383 response.headers.set("x-robots-tag", "noindex");
384 return response;
385 }
379386
380387 async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
381388 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
@@ -450,7 +457,9 @@ const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = {
450457
451458 // POST /claim/options, /welcome/options — a passkey challenge, for a live link and a fit name.
452459 // Nothing is spent: the link burns only when the key comes back.
453 export async function claimOptions(ctx: Ctx): Promise<Response> {
460 export const claimOptions = async (ctx: Ctx): Promise<Response> => secretAnswer(await options(ctx));
461
462 async function options(ctx: Ctx): Promise<Response> {
454463 const refuse = (status: number, message: string) =>
455464 json({ error: "refused", message }, status);
456465 if (crossOrigin(ctx.request, ctx.url, ctx.app.log)) return refuse(403, CAME_FROM_ELSEWHERE);
@@ -481,6 +490,10 @@ const ASK_WINDOW_MS = 3_600_000;
481490 * state answers the same plain 404 as a made-up link.
482491 */
483492 export async function claimRenew(ctx: Ctx): Promise<Response> {
493 return secretAnswer(await renew(ctx));
494 }
495
496 async function renew(ctx: Ctx): Promise<Response> {
484497 const form = await readForm(ctx);
485498 if (form instanceof Response) return form;
486499 const wait = signinAllowed(ctx, `claim-renew:${clientAddress(ctx.request)}`);
@@ -518,9 +531,9 @@ export async function claimRenew(ctx: Ctx): Promise<Response> {
518531 }
519532
520533 // POST /claim — make the account, burn the link, sign the person in.
521 export const claimSubmit = (ctx: Ctx): Promise<Response> => submit(ctx, false);
534 export const claimSubmit = async (ctx: Ctx): Promise<Response> => secretAnswer(await submit(ctx, false));
522535 /** POST /welcome — the same, for the person who started the forge. */
523 export const welcomeSubmit = (ctx: Ctx): Promise<Response> => submit(ctx, true);
536 export const welcomeSubmit = async (ctx: Ctx): Promise<Response> => secretAnswer(await submit(ctx, true));
524537
525538 async function submit(ctx: Ctx, own: boolean): Promise<Response> {
526539 const form = await readForm(ctx);
src/web/pages/start_claim.ts +8 −3
@@ -27,6 +27,7 @@ const tokenOf = (ctx: Ctx): string => ctx.params["token"] ?? "";
2727
2828 function withNoStore(response: Response): Response {
2929 response.headers.set("cache-control", "no-store");
30 response.headers.set("x-robots-tag", "noindex");
3031 return response;
3132 }
3233
@@ -35,7 +36,7 @@ export function startClaimPage(ctx: Ctx, errors: { username?: string; password?:
3536 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
3637 const token = tokenOf(ctx);
3738 const seen = viewClaim(ctx.app, token);
38 if (seen === undefined) return notFoundPage(ctx);
39 if (seen === undefined) return withNoStore(notFoundPage(ctx));
3940 const person = ctx.auth.user?.kind === "user" ? ctx.auth.user : undefined;
4041 const here = `/claim/start/${encodeURIComponent(token)}`;
4142 const body = html`<div class="narrow stack">
@@ -72,12 +73,16 @@ export async function startClaimSubmit(ctx: Ctx): Promise<Response> {
7273 const form = await readForm(ctx);
7374 if (form instanceof Response) return form;
7475 const done = await claimStart(ctx.app, ctx.auth.user, tokenOf(ctx));
75 if (!done.ok) return refused(ctx, done.error);
76 return flashTo(ctx, `/${done.value.org}`, { message: `Taken: ${done.value.repos.join(", ")} now belong to ${done.value.org}.${done.value.stayed.length === 0 ? "" : ` Not moved, still the agent's: ${done.value.stayed.join(", ")}.`}` });
76 if (!done.ok) return withNoStore(refused(ctx, done.error));
77 return withNoStore(flashTo(ctx, `/${done.value.org}`, { message: `Taken: ${done.value.repos.join(", ")} now belong to ${done.value.org}.${done.value.stayed.length === 0 ? "" : ` Not moved, still the agent's: ${done.value.stayed.join(", ")}.`}` }));
7778 }
7879
7980 // POST /claim/start/:token/account — a stranger makes an account and takes it
8081 export async function startClaimAccountSubmit(ctx: Ctx): Promise<Response> {
82 return withNoStore(await makeAccountAndTake(ctx));
83 }
84
85 async function makeAccountAndTake(ctx: Ctx): Promise<Response> {
8186 const form = await readForm(ctx);
8287 if (form instanceof Response) return form;
8388 const token = tokenOf(ctx);
test/claim-headers.test.ts added +116
@@ -0,0 +1,116 @@
1 /**
2 * A claim link's pages are secret (#14136): the page and its POST, valid and
3 * expired, answer `Cache-Control: no-store` and `X-Robots-Tag: noindex`, so no
4 * cache keeps a client's link and no crawler lists it.
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 claim link's pages are never cached or listed", () => {
21 let dir = "";
22 let parent: App;
23 let parentServers: Servers;
24 let child: App;
25 let childServers: Servers;
26 let token = "";
27
28 const get = (path = "/claim") => fetch(`${childServers.url}${path}`);
29 const post = (path = "/claim") =>
30 fetch(`${childServers.url}${path}`, { method: "POST", headers: { origin: childServers.url }, body: new URLSearchParams({ token }) });
31 const hidden = (res: Response) => {
32 expect(res.headers.get("cache-control")).toBe("no-store");
33 expect(res.headers.get("x-robots-tag")).toBe("noindex");
34 };
35
36 beforeAll(async () => {
37 dir = await mkdtemp(join(tmpdir(), "forge-claim-headers-"));
38 parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 });
39 const alice = await createUser(parent.db, {
40 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
41 password: "a-long-claim-passphrase", isAdmin: true,
42 });
43 if (!alice.ok) throw new Error("user");
44 const rw = createToken(parent.db, alice.value.id, "rw", "rw").token;
45 parentServers = await serve(parent);
46 const made = await fetch(`${parentServers.url}/api/v1/forges`, {
47 method: "POST",
48 headers: { authorization: `Bearer ${rw}`, "content-type": "application/json" },
49 body: JSON.stringify({ name: "maria", apps: [], dryRun: false }),
50 });
51 if (made.status !== 201) throw new Error(`spawn ${made.status} ${await made.text()}`);
52 const body = (await made.json()) as { claimUrl: string; bodyId: string };
53 token = body.claimUrl.split("#")[1] ?? "";
54 child = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 });
55 await firstBoot(child);
56 childServers = await serve(child);
57 }, 60_000);
58
59 afterAll(async () => {
60 childServers.stop();
61 child.close();
62 parentServers.stop();
63 parent.close();
64 await rm(dir, { recursive: true, force: true });
65 });
66
67 test("a valid link's page and the welcome page", async () => {
68 const res = await get();
69 expect(res.status).toBe(200);
70 hidden(res);
71 hidden(await get("/welcome"));
72 });
73
74 test("the passkey challenge route, for /claim and /welcome", async () => {
75 for (const path of ["/claim/options", "/welcome/options"]) {
76 const res = await fetch(`${childServers.url}${path}`, {
77 method: "POST", headers: { origin: childServers.url, "content-type": "application/json" },
78 body: JSON.stringify({ token, name: "maria" }),
79 });
80 hidden(res);
81 }
82 });
83
84 test("a refused POST (no such link)", async () => {
85 const res = await fetch(`${childServers.url}/claim`, {
86 method: "POST", headers: { origin: childServers.url }, body: new URLSearchParams({ token: "nope" }),
87 });
88 expect(res.status).toBeGreaterThanOrEqual(400);
89 hidden(res);
90 });
91
92 test("an expired link's page and POST", async () => {
93 const row = child.db.query<{ value: string }, []>("SELECT value FROM user_setting WHERE key = 'spawn_claim'").get();
94 child.db
95 .query("UPDATE user_setting SET value = ?1 WHERE key = 'spawn_claim'")
96 .run(JSON.stringify({ ...JSON.parse(row!.value), e: new Date(Date.now() - 3_600_000).toISOString() }));
97 for (const res of [await get(), await post()]) {
98 expect(res.status).toBe(410);
99 hidden(res);
100 }
101 for (const path of ["/claim/options", "/welcome/options"]) {
102 const res = await fetch(`${childServers.url}${path}`, {
103 method: "POST", headers: { origin: childServers.url, "content-type": "application/json" },
104 body: JSON.stringify({ token, name: "maria" }),
105 });
106 expect(res.status).toBe(410);
107 hidden(res);
108 }
109 });
110
111 test("the agent start link's page is hidden too, even for a made-up token", async () => {
112 const res = await get("/claim/start/no-such-token");
113 expect(res.status).toBe(404);
114 hidden(res);
115 });
116 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs