Public: anyone can read this; the crew can change it.
A forge sent in a link shows its recipient the date the link stops working, and a link past it says so plainly #14629
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14591-a-forge-sent-in-a-link-shows-its-recipie → main 1 commit fork
7 files
changed +120
−17
docs/design/quiet-debt.json −3
| @@ -120,9 +120,6 @@ | ||
| 120 | 120 | "src/web/pages/origins.ts": { |
| 121 | 121 | "pagehead": 1 |
| 122 | 122 | }, |
| 123 | "src/web/pages/passkeys.ts": { | |
| 124 | "pagehead": 8 | |
| 125 | }, | |
| 126 | 123 | "src/web/pages/payments.ts": { |
| 127 | 124 | "pagehead": 1 |
| 128 | 125 | }, |
docs/features/account-access.md +1 −1
| @@ -6,7 +6,7 @@ row: Manage account: reset a person's access | ||
| 6 | 6 | |
| 7 | 7 | An admin opens a person's page (`/settings/accounts/<name>`, linked as **Manage account** from their profile and from `/settings/accounts`, which searches by name) and sees, on one screen: role and join date, email and whether it is verified, passkey count and whether a password is set (never the secrets), the browsers they are signed in on now (when, from where; a person's sign-in history is not kept), what they own, their model allowance and ceilings, and the actions. |
| 8 | 8 | |
| 9 | - **Reset access** (after a confirm) makes a link, `/signin/access/<secret>`, good for one hour and one use. The person opens it, sets a new password (the admin never sees or sets it) and is signed in, then can add a passkey at `/settings/passkeys`. The admin copies the link from the page; when the forge can send mail and the address is verified, it is mailed too. Tick *Also sign them out everywhere* to end every browser in the same step. A new link withdraws the older ones. Only the link's hash is kept. | |
| 9 | - **Reset access** (after a confirm) makes a link, `/signin/access/<secret>`, good for one hour and one use. The person opens it, sets a new password (the admin never sees or sets it) and is signed in, then can add a passkey at `/settings/passkeys`. The admin copies the link from the page; when the forge can send mail and the address is verified, it is mailed too. Tick *Also sign them out everywhere* to end every browser in the same step. A new link withdraws the older ones. Only the link's hash is kept. The page shows "This link works until <date>"; a link past its hour answers 410 "This link has expired" with one next step, asking the sender. | |
| 10 | 10 | - **End all sessions** (after a confirm) signs them out of every browser and does nothing else. |
| 11 | 11 | - Verify email, make member or guest, suspend, unsuspend, set the allowance and ceilings, and remove (with the existing confirm) are the same acts the API has, as buttons. |
| 12 | 12 | - Every act is an event with the admin as actor (`account.access_reset`, `account.sessions_ended`, `account.access_redeemed`). The person reads "An admin reset your access on …" on the next page they open in any browser; it is marked read only when a page shows it, so a redirect or an API call does not use it up. |
src/actions/account_access.ts +7
| @@ -13,6 +13,8 @@ | ||
| 13 | 13 | import type { App } from "../app.ts"; |
| 14 | 14 | import { |
| 15 | 15 | accessLinkOwner, |
| 16 | accessLinkStanding, | |
| 17 | type AccessLinkStanding, | |
| 16 | 18 | ACCESS_LINK_TTL_MS, |
| 17 | 19 | leaveNotice, |
| 18 | 20 | makeAccessLink, |
| @@ -184,6 +186,11 @@ export function accessLinkPerson(app: App, token: string, now: number = Date.now | ||
| 184 | 186 | return user !== undefined && isActive(user) ? user : undefined; |
| 185 | 187 | } |
| 186 | 188 | |
| 189 | /** Where a link stands, for the page that shows its date or says it is past. */ | |
| 190 | export function accessLinkState(app: App, token: string, now: number = Date.now()): AccessLinkStanding { | |
| 191 | return accessLinkStanding(app.db, token, now); | |
| 192 | } | |
| 193 | ||
| 187 | 194 | class NotRedeemed extends Error {} |
| 188 | 195 | |
| 189 | 196 | export type RedeemError = "invalid" | "weak_password" | "password_too_long"; |
src/core/access_link.ts +15
| @@ -49,6 +49,21 @@ export function accessLinkOwner(db: Database, token: string, now: number): strin | ||
| 49 | 49 | ); |
| 50 | 50 | } |
| 51 | 51 | |
| 52 | /** | |
| 53 | * Where a link stands: `live` with the moment it stops working, `expired` when its | |
| 54 | * hour passed unspent, `gone` when it is unknown or already spent or withdrawn. | |
| 55 | */ | |
| 56 | export type AccessLinkStanding = { state: "live"; expiresAt: string } | { state: "expired" } | { state: "gone" }; | |
| 57 | ||
| 58 | export function accessLinkStanding(db: Database, token: string, now: number): AccessLinkStanding { | |
| 59 | if (token === "") return { state: "gone" }; | |
| 60 | const row = db | |
| 61 | .query<{ expires_at: string }, [string]>("SELECT expires_at FROM access_link WHERE token_hash = ?1 AND used_at IS NULL") | |
| 62 | .get(sha256(token)); | |
| 63 | if (row === null) return { state: "gone" }; | |
| 64 | return row.expires_at > new Date(now).toISOString() ? { state: "live", expiresAt: row.expires_at } : { state: "expired" }; | |
| 65 | } | |
| 66 | ||
| 52 | 67 | /** Spend a link: whose it was, once; undefined after, or when it has lapsed. */ |
| 53 | 68 | export function spendAccessLink(db: Database, token: string, now: number): string | undefined { |
| 54 | 69 | if (token === "") return undefined; |
src/web/pages/passkeys.ts +24 −12
| @@ -25,7 +25,7 @@ import { rotateSession } from "../../auth/session_rotate.ts"; | ||
| 25 | 25 | import { LAST_WAY_IN, removeOwnPasskey } from "../../actions/passkeys.ts"; |
| 26 | 26 | import { mailConfigured, recoveryAccount, requestRecovery, spendRecoveryLink } from "../../actions/verify.ts"; |
| 27 | 27 | import { getUserById, isActive, MIN_PASSWORD } from "../../core/users.ts"; |
| 28 | import { accessLinkPerson, redeemAccessLink } from "../../actions/account_access.ts"; | |
| 28 | import { accessLinkPerson, accessLinkState, redeemAccessLink } from "../../actions/account_access.ts"; | |
| 29 | 29 | import { CHANGE_PASSWORD_WORDS } from "../../actions/users.ts"; |
| 30 | 30 | import { cookieValue, crossOrigin, type Ctx } from "../../http/context.ts"; |
| 31 | 31 | import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts"; |
| @@ -277,7 +277,7 @@ export function recoverPage(ctx: Ctx): Response { | ||
| 277 | 277 | return render(() => page({ |
| 278 | 278 | ctx, |
| 279 | 279 | title: "Recover your account", |
| 280 | body: html`<div class="page-head"><h1>Recover your account</h1></div> | |
| 280 | body: html`${pageHead("Recover your account")} | |
| 281 | 281 | <p class="muted small mb">Give the email on your account. If it is verified, we send a link that lasts 30 minutes and works once; it lets you register a new passkey.</p> |
| 282 | 282 | <form method="post" action="/recover"> |
| 283 | 283 | <div class="field"><label for="recover-email">Email</label> |
| @@ -288,12 +288,12 @@ export function recoverPage(ctx: Ctx): Response { | ||
| 288 | 288 | } |
| 289 | 289 | const user = timePhase(ctx.request, "link", () => recoveryAccount(ctx.app, token)); |
| 290 | 290 | if (user === undefined) |
| 291 | return render(() => page({ ctx, title: "Link not valid", body: html`<div class="page-head"><h1>Link not valid</h1></div><p>${NOT_VALID}</p><p><a href="/recover">Ask for a new link</a></p>` }, 400)); | |
| 291 | return render(() => page({ ctx, title: "Link not valid", body: html`${pageHead("Link not valid")}<p>${NOT_VALID}</p><p><a href="/recover">Ask for a new link</a></p>` }, 400)); | |
| 292 | 292 | return render(() => page({ |
| 293 | 293 | ctx, |
| 294 | 294 | title: "Register a new passkey", |
| 295 | 295 | scriptJs: passkeyJs, |
| 296 | body: html`<div class="page-head"><h1>Register a new passkey</h1></div> | |
| 296 | body: html`${pageHead("Register a new passkey")} | |
| 297 | 297 | <p class="muted small mb">This signs you in as ${user.username}. Name the device, then follow your browser's prompt.</p> |
| 298 | 298 | <div class="field"><label for="passkey-name">Name</label> |
| 299 | 299 | <input id="passkey-name" name="name" maxlength="${PASSKEY_NAME_MAX}" autocomplete="off" placeholder="Laptop, phone, security key" /></div> |
| @@ -311,7 +311,7 @@ export async function recoverRequest(ctx: Ctx): Promise<Response> { | ||
| 311 | 311 | const email = field(form, "email").trim().slice(0, 254); |
| 312 | 312 | const wait = email === "" ? undefined : recoverWait(ctx, email); |
| 313 | 313 | if (wait !== undefined) { |
| 314 | const res = page({ ctx, title: "Recover your account", body: html`<div class="page-head"><h1>Too many requests</h1></div><p>Try again in ${wait} seconds.</p>` }, 429); | |
| 314 | const res = page({ ctx, title: "Recover your account", body: html`${pageHead("Too many requests")}<p>Try again in ${wait} seconds.</p>` }, 429); | |
| 315 | 315 | res.headers.set("retry-after", String(wait)); |
| 316 | 316 | return res; |
| 317 | 317 | } |
| @@ -319,7 +319,7 @@ export async function recoverRequest(ctx: Ctx): Promise<Response> { | ||
| 319 | 319 | return page({ |
| 320 | 320 | ctx, |
| 321 | 321 | title: "Check your email", |
| 322 | body: html`<div class="page-head"><h1>Check your email</h1></div> | |
| 322 | body: html`${pageHead("Check your email")} | |
| 323 | 323 | <p>If that address belongs to an account with a verified email, a link is on its way. It lasts 30 minutes and works once.</p>`, |
| 324 | 324 | }); |
| 325 | 325 | } |
| @@ -377,13 +377,22 @@ export async function recoverFinish(ctx: Ctx): Promise<Response> { | ||
| 377 | 377 | |
| 378 | 378 | const ACCESS_NOT_VALID = "This link has been used or has expired. Ask the admin who sent it for a new one."; |
| 379 | 379 | |
| 380 | function accessForm(ctx: Ctx, token: string, username: string, said?: string, status = 200): Response { | |
| 380 | const ACCESS_EXPIRED = "This link has expired. Ask the person who sent it to send you a new one."; | |
| 381 | ||
| 382 | const untilWords = (iso: string): string => iso.slice(0, 16).replace("T", " ") + " UTC"; | |
| 383 | ||
| 384 | function expiredPage(ctx: Ctx): Response { | |
| 385 | return page({ ctx, title: "This link has expired", body: html`${pageHead("This link has expired")}<p>${ACCESS_EXPIRED}</p>` }, 410); | |
| 386 | } | |
| 387 | ||
| 388 | function accessForm(ctx: Ctx, token: string, username: string, said?: string, status = 200, until?: string): Response { | |
| 381 | 389 | return page( |
| 382 | 390 | { |
| 383 | 391 | ctx, |
| 384 | 392 | title: "Set a new password", |
| 385 | body: html`<div class="page-head"><h1>Set a new password</h1></div> | |
| 393 | body: html`${pageHead("Set a new password")} | |
| 386 | 394 | <p class="muted small mb">An admin made this one-use link for ${username}; choose a new password and you are signed in.</p> |
| 395 | ${until === undefined ? null : html`<p class="muted small mb">This link works until <time datetime="${until}">${untilWords(until)}</time>.</p>`} | |
| 387 | 396 | ${said === undefined ? null : html`<p role="alert">${said}</p>`} |
| 388 | 397 | <form method="post" action="/signin/access/${encodeURIComponent(token)}" class="stack"> |
| 389 | 398 | <div class="field"><label for="access-password">New password</label> |
| @@ -402,9 +411,11 @@ export function accessPage(ctx: Ctx): Response { | ||
| 402 | 411 | notePhase(ctx.request, "session", takeSessionTime(ctx.request)); |
| 403 | 412 | const token = ctx.params.token ?? ""; |
| 404 | 413 | const user = timePhase(ctx.request, "link", () => accessLinkPerson(ctx.app, token)); |
| 405 | if (user === undefined) | |
| 406 | return page({ ctx, title: "Link not valid", body: html`<div class="page-head"><h1>Link not valid</h1></div><p>${ACCESS_NOT_VALID}</p>` }, 404); | |
| 407 | return timePhase(ctx.request, "render", () => accessForm(ctx, token, user.username)); | |
| 414 | const standing = accessLinkState(ctx.app, token); | |
| 415 | if (standing.state === "expired") return expiredPage(ctx); | |
| 416 | if (user === undefined || standing.state !== "live") | |
| 417 | return page({ ctx, title: "Link not valid", body: html`${pageHead("Link not valid")}<p>${ACCESS_NOT_VALID}</p>` }, 404); | |
| 418 | return timePhase(ctx.request, "render", () => accessForm(ctx, token, user.username, undefined, 200, standing.expiresAt)); | |
| 408 | 419 | } |
| 409 | 420 | |
| 410 | 421 | // POST /signin/access/:token — spend the link: the new password is set and the person signed in. |
| @@ -414,8 +425,9 @@ export async function accessSubmit(ctx: Ctx): Promise<Response> { | ||
| 414 | 425 | const token = ctx.params.token ?? ""; |
| 415 | 426 | const done = await redeemAccessLink(ctx.app, token, field(form, "password")); |
| 416 | 427 | if (!done.ok) { |
| 428 | if (done.error === "invalid" && accessLinkState(ctx.app, token).state === "expired") return expiredPage(ctx); | |
| 417 | 429 | if (done.error === "invalid") |
| 418 | return page({ ctx, title: "Link not valid", body: html`<div class="page-head"><h1>Link not valid</h1></div><p>${ACCESS_NOT_VALID}</p>` }, 404); | |
| 430 | return page({ ctx, title: "Link not valid", body: html`${pageHead("Link not valid")}<p>${ACCESS_NOT_VALID}</p>` }, 404); | |
| 419 | 431 | const user = accessLinkPerson(ctx.app, token); |
| 420 | 432 | return accessForm(ctx, token, user?.username ?? "", CHANGE_PASSWORD_WORDS[done.error], 422); |
| 421 | 433 | } |
test/access-link-expiry.test.ts added +72
| @@ -0,0 +1,72 @@ | ||
| 1 | /** A link shows the date it stops working; one past it says so plainly (#14591). */ | |
| 2 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 3 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 4 | import { tmpdir } from "node:os"; | |
| 5 | import { join } from "node:path"; | |
| 6 | import { resetAccess } from "../src/actions/account_access.ts"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { accessLinkStanding, makeAccessLink } from "../src/core/access_link.ts"; | |
| 9 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | import { adminCookie } from "./helpers/admin.ts"; | |
| 12 | import { quietLogs } from "./helpers/globals.ts"; | |
| 13 | ||
| 14 | quietLogs(); | |
| 15 | ||
| 16 | describe("an access link and its date", () => { | |
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let bobId = ""; | |
| 21 | const get = (path: string) => fetch(`${servers.url}${path}`, { headers: { accept: "text/html" }, redirect: "manual" }); | |
| 22 | ||
| 23 | beforeAll(async () => { | |
| 24 | dataDir = await mkdtemp(join(tmpdir(), "forge-link-expiry-")); | |
| 25 | app = createApp({ dataDir, port: 0 }); | |
| 26 | servers = await serve(app); | |
| 27 | await adminCookie(app, { username: "olive", email: "[email protected]", password: "owner-passphrase" }); | |
| 28 | const b = await createUser(app.db, { origin: "seed", username: "bob", email: "[email protected]", password: "bob-old-passphrase", trusted: true }); | |
| 29 | if (!b.ok) throw new Error("user"); | |
| 30 | bobId = b.value.id; | |
| 31 | }, 30_000); | |
| 32 | ||
| 33 | afterAll(async () => { | |
| 34 | servers.stop(); | |
| 35 | app.close(); | |
| 36 | await rm(dataDir, { recursive: true, force: true }); | |
| 37 | }); | |
| 38 | ||
| 39 | test("a live link says when it stops working", async () => { | |
| 40 | const made = await resetAccess(app, getUserByName(app.db, "olive")!, "bob"); | |
| 41 | if (!made.ok || made.value.token === undefined) throw new Error("link"); | |
| 42 | const res = await get(`/signin/access/${made.value.token}`); | |
| 43 | expect(res.status).toBe(200); | |
| 44 | const text = await res.text(); | |
| 45 | const until = made.value.expiresAt!.slice(0, 16).replace("T", " ") + " UTC"; | |
| 46 | expect(text).toContain(`This link works until`); | |
| 47 | expect(text).toContain(until); | |
| 48 | }); | |
| 49 | ||
| 50 | test("a link a minute from its end still shows its date", async () => { | |
| 51 | const made = makeAccessLink(app.db, bobId, bobId, Date.now() - 59 * 60_000); | |
| 52 | const res = await get(`/signin/access/${made.token}`); | |
| 53 | expect(res.status).toBe(200); | |
| 54 | expect(await res.text()).toContain("This link works until"); | |
| 55 | }); | |
| 56 | ||
| 57 | test("an expired link answers 410 with one next step and no ids", async () => { | |
| 58 | const made = makeAccessLink(app.db, bobId, bobId, Date.now() - 2 * 3_600_000); | |
| 59 | expect(accessLinkStanding(app.db, made.token, Date.now()).state).toBe("expired"); | |
| 60 | const res = await get(`/signin/access/${made.token}`); | |
| 61 | expect(res.status).toBe(410); | |
| 62 | const text = await res.text(); | |
| 63 | expect(text).toContain("This link has expired"); | |
| 64 | expect(text).toContain("Ask the person who sent it"); | |
| 65 | expect(text).not.toContain("This link works until"); | |
| 66 | expect(text).not.toContain(bobId); | |
| 67 | }); | |
| 68 | ||
| 69 | test("an unknown link is not told apart from a spent one", async () => { | |
| 70 | expect((await get("/signin/access/nonsense")).status).toBe(404); | |
| 71 | }); | |
| 72 | }); |
test/account-access.test.ts +1 −1
| @@ -196,7 +196,7 @@ describe("an admin resets a person's access", () => { | ||
| 196 | 196 | test("a link fails after an hour", async () => { |
| 197 | 197 | const made = await resetAccess(app, getUserByName(app.db, "olive")!, "bob", { now: Date.now() - 2 * 3_600_000 }); |
| 198 | 198 | expect(made.ok && made.value.token).toBeTruthy(); |
| 199 | if (made.ok) expect((await get(`/signin/access/${made.value.token}`)).status).toBe(404); | |
| 199 | if (made.ok) expect((await get(`/signin/access/${made.value.token}`)).status).toBe(410); | |
| 200 | 200 | const fresh = await resetAccess(app, getUserByName(app.db, "olive")!, "bob", { now: Date.now() - 59 * 60_000 }); |
| 201 | 201 | if (fresh.ok) expect((await get(`/signin/access/${fresh.value.token}`)).status).toBe(200); |
| 202 | 202 | }); |
new above
offline