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

An expired or used forge link says so in one sentence and names who sent it #13219

Merged opened this

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

Merged

scout/forge:scout/13141-an-expired-or-used-forge-link-says-so-in → main 6 commits fork 3 threads · 3 unresolved

  • Merged. scout/13141-an-expired-or-used-forge-link-says-so-in was merged into main as 2d91c4b.

Review

Sign in to review this change.
5 files changed +87 −31
src/web/pages/claim.ts +35 −17
@@ -12,8 +12,9 @@
1212 *
1313 * The GET only shows the form — a link unfurled in a chat app must not
1414 * spend itself — and the POST, same-origin, makes the account and burns the
15 * link in one step. A link that is used, expired or made up says which,
16 * plainly; a forge that was not spawned has no claim page at all.
15 * link in one step. A link that is used or expired says which in one
16 * sentence and names who sent it (410); one that is revoked or made up is a plain 404; a forge that
17 * was not spawned has no claim page at all.
1718 *
1819 * Who keeps access is never silent. By default the spawner's account leaves
1920 * the organisation's Owners when the forge is claimed (the new owner can
@@ -98,6 +99,13 @@ export function relativeWords(ms: number): string {
9899 return ms >= 0 ? `in ${span}` : `${span} ago`;
99100 }
100101
102 /**
103 * Who sent the link, as a person reads them: the display name when there is one, else the username.
104 */
105 function sender(user: User | undefined): string | undefined {
106 return user === undefined ? undefined : user.displayName || user.username;
107 }
108
101109 function dead(
102110 ctx: Ctx,
103111 state: Exclude<ClaimState["state"], "ok">,
@@ -105,8 +113,8 @@ function dead(
105113 own = false,
106114 ): Response {
107115 const ask = sender === undefined ? "whoever sent it" : sender;
108 // A stranger must not learn which of unknown, expired or revoked this is:
109 // one status, one title, one kind sentence, naming no one.
116 // Only a link that once was real says so (used, expired) and names its

Non-blocking: the dead() parameter named sender shadows the new sender() helper above it. Rename the parameter (e.g. ask) so the next edit does not call the wrong one.

117 // sender; unknown and revoked stay one plain 404 naming no one.
110118 const gone = {
111119 status: 404,
112120 title: "This link does not work",
@@ -116,10 +124,14 @@ function dead(
116124 unknown: gone,
117125 used: {
118126 status: 410,
outdated

Principle 1: repeats the title word for word. Fix: text = Ask ${ask} to send you a new one.

119 title: "This link has already been used",
120 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.`,
127 title: "This link was already used",
128 text: `If it was you, sign in; if not, ask ${ask} to send you a new one.`,
129 },
130 expired: {
131 status: 410,
132 title: "This link has expired",
133 text: `Ask ${ask} to send you a new one.`,
121134 },
122 expired: gone,
123135 revoked: gone,
124136 owned: {
125137 status: 410,
@@ -130,8 +142,8 @@ function dead(
130142 // The person who started this forge themselves has no sender to ask.
131143 const mine: Partial<Record<typeof state, string>> = {
132144 unknown: "This link does not work any more. Start the forge again from the home page.",
133 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.",
134 expired: "This link does not work any more. Start the forge again from the home page.",
145 used: "If it was you, sign in. Otherwise start a forge of your own from the home page.",
146 expired: "Start the forge again from the home page.",
135147 revoked: "This link does not work any more. Start the forge again from the home page.",
136148 };
137149 if (own) says.text = mine[state] ?? says.text;
@@ -330,7 +342,7 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
330342 return dead(
331343 ctx,
332344 link.state === "used" ? "used" : link.revokedAt !== null ? "revoked" : "expired",
333 own ? undefined : spawner?.username,
345 own ? undefined : sender(spawner),
334346 own,
335347 );
336348 const rendering = performance.now();
@@ -345,7 +357,10 @@ async function fragmentPage(ctx: Ctx, own: boolean): Promise<Response> {
345357 return response;
346358 }
347359
348 /** Whether a name and an address (optional, and read only where the forge sends mail) are fit for the first admin; else the words. */
360 /**
361 * Whether a name and an address (optional, and read only where the forge sends mail) are fit for
362 * the first admin; else the words.
363 */
349364 function problems(
350365 ctx: Ctx,
351366 spawned: Spawned,
@@ -368,14 +383,17 @@ function problems(
368383 return errors;
369384 }
370385
371 /** The address the form gave, on a forge that sends mail; nothing on one that does not, whatever was posted. */
386 /**
387 * The address the form gave, on a forge that sends mail; nothing on one that does not, whatever was
388 * posted.
389 */
372390 const emailGiven = (ctx: Ctx, raw: string): string => (mailConfigured(ctx.app) ? raw.trim() : "");
373391
374392 /** Why a passkey challenge was refused, in the passkey note's plain words. */
375393 const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = {
376394 unknown: "This link does not work any more.",
377 used: "This link has already been used.",
378 expired: "This link does not work any more.",
395 used: "This link was already used.",
396 expired: "This link has expired.",
379397 revoked: "This link does not work any more.",
380398 owned: "This forge already has an owner.",
381399 };
@@ -394,7 +412,7 @@ export async function claimOptions(ctx: Ctx): Promise<Response> {
394412 const spawned = await spawnedOrg(ctx.app);
395413 if (spawned === undefined) return refuse(404, LINK_REFUSED.unknown);
396414 const state = readClaim(ctx.app.db, spawned.org.id, str(data.token)).state;
397 if (state !== "ok") return refuse(state === "used" || state === "owned" ? 410 : 404, LINK_REFUSED[state]);
415 if (state !== "ok") return refuse(state === "used" || state === "owned" || state === "expired" ? 410 : 404, LINK_REFUSED[state]);
398416 const username = str(data.username).trim();
399417 const bad = problems(ctx, spawned, username, emailGiven(ctx, str(data.email)));
400418 const first = bad.username ?? bad.email;
@@ -419,7 +437,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> {
419437 const before = readClaim(ctx.app.db, orgId, token);
420438 const { spawner } = owners(ctx, spawned);
421439 if (before.state !== "ok")
422 return dead(ctx, before.state, own ? undefined : spawner?.username, own);
440 return dead(ctx, before.state, own ? undefined : sender(spawner), own);
423441
424442 const values = {
425443 username: field(form, "username"),
@@ -461,7 +479,7 @@ async function submit(ctx: Ctx, own: boolean): Promise<Response> {
461479 (founder === undefined && !takesName) || credential !== undefined ? "" : await Bun.password.hash(password, "argon2id");
462480 // Spent first, in one statement: of two people on one link, one wins.
463481 const spent = burnClaim(ctx.app.db, orgId, token);
464 if (spent.state !== "ok") return dead(ctx, spent.state, undefined, own);
482 if (spent.state !== "ok") return dead(ctx, spent.state, own ? undefined : sender(spawner), own);
465483 if (founder !== undefined) {
466484 const taken = takeFounder(ctx.app.db, founder.id, {
467485 hash: founderHash,
test/claim-page-words.test.ts +5 −5
@@ -80,7 +80,7 @@ describe("the claim page in plain words", () => {
8080 expect(html).toContain("alice");
8181 });
8282
83 test("an expired link answers 404 in plain words", async () => {
83 test("an expired link answers 410: one sentence, and who sent it", 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(404);
91 expect(res.status).toBe(410);
9292 const text = await res.text();
93 expect(text).toContain("This link does not work");
94 expect(text).toContain("ask whoever sent it for a new one");
93 expect(text).toContain("This link has expired");
94 expect(text).toContain("Ask alice to send you a new one");
9595 expect(text).not.toMatch(/token|\b4\d\d\b/i);
9696 }
9797 });
@@ -101,7 +101,7 @@ describe("the claim page in plain words", () => {
101101 const res = await get();
102102 expect(res.status).toBe(410);
103103 const text = await res.text();
104 expect(text).toContain("This link has already been used");
104 expect(text).toContain("This link was already used");
105105 expect(text).toContain("ask alice to send you a new one");
106106 expect(text).not.toMatch(/token|\b4\d\d\b/i);
107107 });
test/spawn-first-boot.test.ts +3 −3
@@ -350,7 +350,7 @@ describe("a spawned child's first boot", () => {
350350 email: "[email protected]",
351351 });
352352 expect(res.status).toBe(410);
353 expect(await res.text()).toContain("already been used");
353 expect(await res.text()).toContain("This link was already used");
354354 expect(getUserByName(child.db, "mallory")).toBeUndefined();
355355 });
356356
@@ -368,8 +368,8 @@ describe("a spawned child's first boot", () => {
368368 username: "late",
369369 email: "[email protected]",
370370 });
371 expect(res.status).toBe(404);
372 expect(await res.text()).toContain("does not work");
371 expect(res.status).toBe(410);
372 expect(await res.text()).toContain("This link has expired");
373373 expect(getUserByName(child.db, "late")).toBeUndefined();
374374 });
375375
test/stranger-words.test.ts +1 −1
@@ -142,7 +142,7 @@ describe("a stranger's way to their own forge", () => {
142142 await both("revoked", 404);
143143 expect(restoreClaim(db, org)).toBe(true);
144144 as({ e: "2000-01-01T00:00:00.000Z" });
145 await both("expired", 404);
145 await both("expired", 410);
146146 as({ u: new Date().toISOString() });
147147 await both("used", 410);
148148 setStored(live);

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs