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

A forge link's landing page works with no script at 360 px and says what it is in one sentence #12438

Merged opened this

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

Merged

scout/forge:scout/12416-a-forge-link-s-landing-page-works-with-n → main 7 commits fork 1 thread · 1 unresolved

  • Merged. scout/12416-a-forge-link-s-landing-page-works-with-n was merged into main as 7eab656.

Review

Sign in to review this change.
9 files changed +211 −62
src/web/pages/claim.ts +39 −44
@@ -100,31 +100,25 @@ function dead(
100100 ctx: Ctx,
101101 state: Exclude<ClaimState["state"], "ok">,
102102 sender?: string,
103 endedAt?: string,
104103 own = false,
105104 ): Response {
106105 const ask = sender === undefined ? "whoever sent it" : sender;
106 // A stranger must not learn which of unknown, expired or revoked this is:
107 // one status, one title, one kind sentence, naming no one.
outdated

void endedAt; is dead code silenced. Drop the unused parameter (principle 8 / Clean).

108 const gone = {
109 status: 404,
110 title: "This link does not work",
111 text: "Sorry, this link does not work any more. Please ask whoever sent it for a new one.",
112 };
107113 const says = {
108 unknown: {
109 status: 404,
110 title: "This is not a forge link",
111 text: "The link may have been cut short, or replaced by a newer one. Ask whoever sent it for the latest.",
112 },
114 unknown: gone,
113115 used: {
114116 status: 410,
115117 title: "This link has already been used",
116118 text: `Each link works once. If it was you, sign in with the account you made; if not, ask ${ask} to send you a new one.`,
117119 },
118 expired: {
119 status: 410,
120 title: "This link has expired",
121 text: `It stopped working${endedAt === undefined ? "" : ` on ${whenWords(endedAt)}`}. Ask ${ask} to send you a new one.`,
122 },
123 revoked: {
124 status: 410,
125 title: "This link has ended",
126 text: `Whoever sent it took it back. Ask ${ask} to send you a new one.`,
127 },
120 expired: gone,
121 revoked: gone,
128122 owned: {
129123 status: 410,
130124 title: "This forge already has an owner",
@@ -133,10 +127,10 @@ function dead(
133127 }[state];
134128 // The person who started this forge themselves has no sender to ask.
135129 const mine: Partial<Record<typeof state, string>> = {
136 unknown: "The link may have been cut short or replaced. Start the forge again from the home page.",
130 unknown: "This link does not work any more. Start the forge again from the home page.",
137131 used: "Each link works once. If it was you, sign in with the account you made. Otherwise start a forge of your own from the home page.",
138 expired: "Your link stopped working. Start the forge again from the home page.",
139 revoked: "This link has ended. Start the forge again from the home page.",
132 expired: "This link does not work any more. Start the forge again from the home page.",
133 revoked: "This link does not work any more. Start the forge again from the home page.",
140134 };
141135 if (own) says.text = mine[state] ?? says.text;
142136 return page(
@@ -216,7 +210,7 @@ function claimForm(
216210 return html`<div class="narrow">
217211 ${pageHead("This forge is yours")}
218212 <section class="settings-block">
219 <p class="mb">${spawned.org.username} was made for you. Your account will be its admin, so it can change everything here.</p>
213 <p class="mb">${spawner === undefined ? `${spawned.org.username} was made` : `${spawner.username} made ${spawned.org.username}`} for you. Your account will be its admin, so it can change everything here.</p>
220214 ${expiresAt === undefined
221215 ? null
222216 : html`<p class="mb" id="claim-expiry">This link works until
@@ -239,15 +233,17 @@ function claimForm(
239233 */
240234 function accountForm(ctx: Ctx, action: "/claim" | "/welcome", values: Values, errors: Errors): Html {
241235 return html`<form method="post" action="${action}">
242 <div id="claim-token-field" ${values.token ? html`hidden` : null}>
243 ${formField({
244 label: "Link code",
245 name: "token",
246 value: values.token ?? "",
247 required: true,
248 hint: "The part of your link after the #. Opening the link fills it in.",
249 })}
250 </div>
236 ${values.token
237 ? html`<input type="hidden" name="token" value="${values.token}" />`
238 : html`<input type="hidden" name="token" id="claim-token" />
239 <noscript>
240 ${formField({
241 label: "Link code",
242 name: "code",
243 required: true,
244 hint: "Without script, paste the part of your link after the #.",
245 })}
246 </noscript>`}
251247 ${formField({
252248 label: "Username",
253249 name: "username",
@@ -299,18 +295,16 @@ function accountForm(ctx: Ctx, action: "/claim" | "/welcome", values: Values, er
299295 /**
300296 * The fragment link's page script: take the claim code from `#…` into the
301297 * form and drop it from the address bar, so it is never in a request line,
302 * a Referer, the history or a screenshot of the URL. Without JavaScript the
303 * code field stays visible and takes a paste.
298 * a Referer, the history or a screenshot of the URL. Without JavaScript a
299 * <noscript> code field takes a paste (the server cannot see a fragment).
304300 */
305301 const fragmentJs = /* js */ `
306302 (function () {
307 var field = document.querySelector('input[name="token"]');
303 var field = document.getElementById('claim-token');
308304 if (!field) return;
309305 var code = location.hash.slice(1);
310306 if (!code) return;
311307 field.value = code;
312 var box = document.getElementById('claim-token-field');
313 if (box) box.hidden = true;
314308 history.replaceState(null, '', location.pathname + location.search);
315309 })();
316310 `;
@@ -324,7 +318,7 @@ export const welcomePage = (ctx: Ctx): Promise<Response> => fragmentPage(ctx, tr
324318 async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
325319 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
326320 const spawned = await timePhaseAsync(ctx.request, "spawned", () => spawnedOrg(ctx.app));
327 if (spawned === undefined) return timePhase(ctx.request, "render", () => dead(ctx, "unknown", undefined, undefined, own));
321 if (spawned === undefined) return timePhase(ctx.request, "render", () => dead(ctx, "unknown", undefined, own));
328322 const { spawner } = timePhase(ctx.request, "owners", () => owners(ctx, spawned));
329323 // The forge has one link at a time, so its state says nothing about any
330324 // particular code: a stale or made-up code only ever meets "unknown" on the POST.
@@ -334,7 +328,6 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
334328 ctx,
335329 link.state === "used" ? "used" : link.revokedAt !== null ? "revoked" : "expired",
336330 own ? undefined : spawner?.username,
337 link.expiresAt,
338331 own,
339332 );
340333 const rendering = performance.now();
@@ -343,6 +336,7 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
343336 title: own ? "Make your account" : "This forge is yours",
344337 body: claimForm(ctx, spawned, spawner, {}, {}, link?.expiresAt, own),
345338 scriptJs: fragmentJs + passkeyJs,
339 bare: true,
346340 });
347341 notePhase(ctx.request, "render", performance.now() - rendering);
348342 return response;
@@ -376,10 +370,10 @@ const emailGiven = (ctx: Ctx, raw: string): string => (mailConfigured(ctx.app) ?
376370
377371 /** Why a passkey challenge was refused, in the passkey note's plain words. */
378372 const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = {
379 unknown: "This is not a forge link.",
373 unknown: "This link does not work any more.",
380374 used: "This link has already been used.",
381 expired: "This link has expired.",
382 revoked: "This link has ended.",
375 expired: "This link does not work any more.",
376 revoked: "This link does not work any more.",
383377 owned: "This forge already has an owner.",
384378 };
385379
@@ -397,7 +391,7 @@ export async function claimOptions(ctx: Ctx): Promise<Response> {
397391 const spawned = await spawnedOrg(ctx.app);
398392 if (spawned === undefined) return refuse(404, LINK_REFUSED.unknown);
399393 const state = readClaim(ctx.app.db, spawned.org.id, str(data.token)).state;
400 if (state !== "ok") return refuse(state === "unknown" ? 404 : 410, LINK_REFUSED[state]);
394 if (state !== "ok") return refuse(state === "used" || state === "owned" ? 410 : 404, LINK_REFUSED[state]);
401395 const username = str(data.username).trim();
402396 const bad = problems(ctx, spawned, username, emailGiven(ctx, str(data.email)));
403397 const first = bad.username ?? bad.email;
@@ -414,15 +408,15 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> {
414408 const form = await readForm(ctx);
415409 if (form instanceof Response) return form;
416410 const spawned = await spawnedOrg(ctx.app);
417 if (spawned === undefined) return dead(ctx, "unknown", undefined, undefined, own);
411 if (spawned === undefined) return dead(ctx, "unknown", undefined, own);
418412 const orgId = spawned.org.id;
419413 // From the form's body — never from the path or a query string, which
420414 // proxies and logs would keep.
421 const token = field(form, "token");
415 const token = field(form, "token") || field(form, "code");
422416 const before = readClaim(ctx.app.db, orgId, token);
423417 const { spawner } = owners(ctx, spawned);
424418 if (before.state !== "ok")
425 return dead(ctx, before.state, own ? undefined : spawner?.username, linkOf(ctx.app.db)?.expiresAt, own);
419 return dead(ctx, before.state, own ? undefined : spawner?.username, own);
426420
427421 const values = {
428422 username: field(form, "username"),
@@ -436,6 +430,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> {
436430 title: own ? "Make your account" : "This forge is yours",
437431 body: claimForm(ctx, spawned, spawner, values, e, linkOf(ctx.app.db)?.expiresAt, own),
438432 scriptJs: fragmentJs + passkeyJs,
433 bare: true,
439434 },
440435 422,
441436 );
@@ -463,7 +458,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> {
463458 (founder === undefined && !takesName) || credential !== undefined ? "" : await Bun.password.hash(password, "argon2id");
464459 // Spent first, in one statement: of two people on one link, one wins.
465460 const spent = burnClaim(ctx.app.db, orgId, token);
466 if (spent.state !== "ok") return dead(ctx, spent.state, undefined, undefined, own);
461 if (spent.state !== "ok") return dead(ctx, spent.state, undefined, own);
467462 if (founder !== undefined) {
468463 const taken = takeFounder(ctx.app.db, founder.id, {
469464 hash: founderHash,
test/claim-page-words.test.ts +4 −4
@@ -80,7 +80,7 @@ describe("the claim page in plain words", () => {
8080 expect(html).toContain("alice");
8181 });
8282
83 test("an expired link answers 410 in plain words", async () => {
83 test("an expired link answers 404 in plain words", async () => {
8484 setStored({ e: new Date(Date.now() - 3_600_000).toISOString() });
8585 for (const res of [
8686 await get(),
@@ -88,10 +88,10 @@ describe("the claim page in plain words", () => {
8888 method: "POST", headers: { origin: childServers.url }, body: new URLSearchParams({ token }),
8989 }),
9090 ]) {
91 expect(res.status).toBe(410);
91 expect(res.status).toBe(404);
9292 const text = await res.text();
93 expect(text).toContain("This link has expired");
94 expect(text).toContain("Ask alice to send you a new one");
93 expect(text).toContain("This link does not work");
94 expect(text).toContain("ask whoever sent it for a new one");
9595 expect(text).not.toMatch(/token|\b4\d\d\b/i);
9696 }
9797 });
test/claim-revoke.test.ts +4 −4
@@ -95,13 +95,13 @@ describe("revoking a forge link", () => {
9595 await rm(dir, { recursive: true, force: true });
9696 });
9797
98 test("revoke-then-use answers 410 in plain words and sets no session", async () => {
98 test("revoke-then-use answers 404 in plain words and sets no session", async () => {
9999 expect(state("one")).toBe("open");
100100 const done = await revoke("one", "revoke");
101101 expect(done.ok).toBe(true);
102102 expect(state("one")).toBe("ended");
103103 const res = await claim("one");
104 expect(res.status).toBe(410);
104 expect(res.status).toBe(404);
105105 expect(res.headers.get("set-cookie")).toBeNull();
106106 const form = { token: kids["one"]!.token, username: "x", email: "[email protected]" };
107107 const post = await fetch(`${kids["one"]!.servers.url}/claim`, {
@@ -110,9 +110,9 @@ describe("revoking a forge link", () => {
110110 body: new URLSearchParams({ ...form, ...(await passkeyFields(kids["one"]!.servers.url, "/claim/options", form).catch(() => ({}))) }),
111111 redirect: "manual",
112112 });
113 expect(post.status).toBe(410);
113 expect(post.status).toBe(404);
114114 expect(post.headers.get("set-cookie")).toBeNull();
115 expect(await post.text()).toContain("This link has ended");
115 expect(await post.text()).toContain("This link does not work");
116116 });
117117
118118 test("a revoke is receipted, and undone within the minute", async () => {
test/console-clean.test.ts +1 −1
@@ -174,6 +174,6 @@ function guarded(code: string, at: number, len: number): boolean {
174174
175175 test("the claim page's own elements are present", async () => {
176176 const html = await (await fetch(`${childServers.url}/claim`)).text();
177 for (const id of ["claim-token-field", "passkey-make", "passkey-make-note"]) expect(html).toContain(`id="${id}"`);
177 for (const id of ["claim-token", "passkey-make", "passkey-make-note"]) expect(html).toContain(`id="${id}"`);
178178 expect(html).toContain('name="token"');
179179 });
test/spawn-first-boot.test.ts +3 −3
@@ -258,7 +258,7 @@ describe("a spawned child's first boot", () => {
258258 test("a made-up code says it is not a forge link", async () => {
259259 const res = await claim("x".repeat(43), maria);
260260 expect(res.status).toBe(404);
261 expect(await res.text()).toContain("not a forge link");
261 expect(await res.text()).toContain("does not work");
262262 expect(getUserByName(child.db, "maria")).toBeUndefined();
263263 });
264264
@@ -368,8 +368,8 @@ describe("a spawned child's first boot", () => {
368368 username: "late",
369369 email: "[email protected]",
370370 });
371 expect(res.status).toBe(410);
372 expect(await res.text()).toContain("expired");
371 expect(res.status).toBe(404);
372 expect(await res.text()).toContain("does not work");
373373 expect(getUserByName(child.db, "late")).toBeUndefined();
374374 });
375375
test/stranger-words.test.ts +2 −2
@@ -139,10 +139,10 @@ describe("a stranger's way to their own forge", () => {
139139 screens.push(["/claim passkey note, unknown", await note(404, { ...who, token: "nope" })]);
140140 screens.push(["/claim, a passkey that failed", await submit(422, { ...who, challenge: "x", clientDataJSON: "x", attestationObject: "x" })]);
141141 expect(revokeClaim(db, org)).toBe(true);
142 await both("revoked", 410);
142 await both("revoked", 404);
143143 expect(restoreClaim(db, org)).toBe(true);
144144 as({ e: "2000-01-01T00:00:00.000Z" });
145 await both("expired", 410);
145 await both("expired", 404);
146146 as({ u: new Date().toISOString() });
147147 await both("used", 410);
148148 setStored(live);
test/welcome.test.ts +4 −4
@@ -62,7 +62,8 @@ test("a refused welcome says nothing of a sender, and a good one signs in", asyn
6262 // The page comes back with the password kept in sight (the script would hide it), and the link code out of sight.
6363 const again = await short.text();
6464 expect(again).toMatch(/<div data-passkey="off" data-keep>/);
65 expect(again).toMatch(/<div id="claim-token-field" hidden>/);
65 expect(again).toMatch(/<input type="hidden" name="token" value="[^"]+"/);
66 expect(again).not.toContain('name="code"');
6667 const ok = await post({ token: fresh.token, username: "maria", password: "a-long-enough-password" });
6768 expect(ok.status).toBe(303);
6869 expect(ok.headers.get("location")).toBe("/");
@@ -140,12 +141,11 @@ test("the home's scripts join without calling the one before", () => {
140141 for (const part of [funnelJs, startNameJs]) expect(part.trimEnd().endsWith(";")).toBe(true);
141142 });
142143
143 test("the link code field is visible and required in the server render, and the forge's own name is a name a person may take (#11948)", async () => {
144 test("the no-script link code field is required in the server render, and the forge's own name is a name a person may take (#11948)", async () => {
144145 const started = await startedForge("forge-welcome-name-");
145146 try {
146147 const html = await (await fetch(`${started.base}/welcome`)).text();
147 expect(html).not.toMatch(/<div id="claim-token-field"[^>]*hidden/);
148 expect(html).toMatch(/<input[^>]*name="token"[^>]*required|<input[^>]*required[^>]*name="token"/);
148 expect(html).toMatch(/<noscript>[\s\S]*<input[^>]*name="code"[^>]*required|<noscript>[\s\S]*<input[^>]*required[^>]*name="code"/);
149149 const org = started.child.db.query<{ username: string }, []>("SELECT username FROM user WHERE kind = 'org' LIMIT 1").get()!.username;
150150 const res = await fetch(`${started.base}/welcome`, {
151151 method: "POST",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs