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

Merged opened this

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

Merged

scout/forge:scout/14591-a-forge-sent-in-a-link-shows-its-recipie → main 1 commit fork

  • Merged. scout/14591-a-forge-sent-in-a-link-shows-its-recipie was merged into main as e4740f2.

Review

Sign in to review this change.
7 files changed +120 −17
docs/design/quiet-debt.json −3
@@ -120,9 +120,6 @@
120120 "src/web/pages/origins.ts": {
121121 "pagehead": 1
122122 },
123 "src/web/pages/passkeys.ts": {
124 "pagehead": 8
125 },
126123 "src/web/pages/payments.ts": {
127124 "pagehead": 1
128125 },
docs/features/account-access.md +1 −1
@@ -6,7 +6,7 @@ row: Manage account: reset a person's access
66
77 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.
88
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.
1010 - **End all sessions** (after a confirm) signs them out of every browser and does nothing else.
1111 - 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.
1212 - 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 @@
1313 import type { App } from "../app.ts";
1414 import {
1515 accessLinkOwner,
16 accessLinkStanding,
17 type AccessLinkStanding,
1618 ACCESS_LINK_TTL_MS,
1719 leaveNotice,
1820 makeAccessLink,
@@ -184,6 +186,11 @@ export function accessLinkPerson(app: App, token: string, now: number = Date.now
184186 return user !== undefined && isActive(user) ? user : undefined;
185187 }
186188
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
187194 class NotRedeemed extends Error {}
188195
189196 export type RedeemError = "invalid" | "weak_password" | "password_too_long";
src/web/pages/passkeys.ts +24 −12
@@ -25,7 +25,7 @@ import { rotateSession } from "../../auth/session_rotate.ts";
2525 import { LAST_WAY_IN, removeOwnPasskey } from "../../actions/passkeys.ts";
2626 import { mailConfigured, recoveryAccount, requestRecovery, spendRecoveryLink } from "../../actions/verify.ts";
2727 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";
2929 import { CHANGE_PASSWORD_WORDS } from "../../actions/users.ts";
3030 import { cookieValue, crossOrigin, type Ctx } from "../../http/context.ts";
3131 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
@@ -277,7 +277,7 @@ export function recoverPage(ctx: Ctx): Response {
277277 return render(() => page({
278278 ctx,
279279 title: "Recover your account",
280 body: html`<div class="page-head"><h1>Recover your account</h1></div>
280 body: html`${pageHead("Recover your account")}
281281 <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>
282282 <form method="post" action="/recover">
283283 <div class="field"><label for="recover-email">Email</label>
@@ -288,12 +288,12 @@ export function recoverPage(ctx: Ctx): Response {
288288 }
289289 const user = timePhase(ctx.request, "link", () => recoveryAccount(ctx.app, token));
290290 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));
292292 return render(() => page({
293293 ctx,
294294 title: "Register a new passkey",
295295 scriptJs: passkeyJs,
296 body: html`<div class="page-head"><h1>Register a new passkey</h1></div>
296 body: html`${pageHead("Register a new passkey")}
297297 <p class="muted small mb">This signs you in as ${user.username}. Name the device, then follow your browser's prompt.</p>
298298 <div class="field"><label for="passkey-name">Name</label>
299299 <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> {
311311 const email = field(form, "email").trim().slice(0, 254);
312312 const wait = email === "" ? undefined : recoverWait(ctx, email);
313313 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);
315315 res.headers.set("retry-after", String(wait));
316316 return res;
317317 }
@@ -319,7 +319,7 @@ export async function recoverRequest(ctx: Ctx): Promise<Response> {
319319 return page({
320320 ctx,
321321 title: "Check your email",
322 body: html`<div class="page-head"><h1>Check your email</h1></div>
322 body: html`${pageHead("Check your email")}
323323 <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>`,
324324 });
325325 }
@@ -377,13 +377,22 @@ export async function recoverFinish(ctx: Ctx): Promise<Response> {
377377
378378 const ACCESS_NOT_VALID = "This link has been used or has expired. Ask the admin who sent it for a new one.";
379379
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 {
381389 return page(
382390 {
383391 ctx,
384392 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")}
386394 <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>`}
387396 ${said === undefined ? null : html`<p role="alert">${said}</p>`}
388397 <form method="post" action="/signin/access/${encodeURIComponent(token)}" class="stack">
389398 <div class="field"><label for="access-password">New password</label>
@@ -402,9 +411,11 @@ export function accessPage(ctx: Ctx): Response {
402411 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
403412 const token = ctx.params.token ?? "";
404413 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));
408419 }
409420
410421 // 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> {
414425 const token = ctx.params.token ?? "";
415426 const done = await redeemAccessLink(ctx.app, token, field(form, "password"));
416427 if (!done.ok) {
428 if (done.error === "invalid" && accessLinkState(ctx.app, token).state === "expired") return expiredPage(ctx);
417429 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);
419431 const user = accessLinkPerson(ctx.app, token);
420432 return accessForm(ctx, token, user?.username ?? "", CHANGE_PASSWORD_WORDS[done.error], 422);
421433 }
test/account-access.test.ts +1 −1
@@ -196,7 +196,7 @@ describe("an admin resets a person's access", () => {
196196 test("a link fails after an hour", async () => {
197197 const made = await resetAccess(app, getUserByName(app.db, "olive")!, "bob", { now: Date.now() - 2 * 3_600_000 });
198198 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);
200200 const fresh = await resetAccess(app, getUserByName(app.db, "olive")!, "bob", { now: Date.now() - 59 * 60_000 });
201201 if (fresh.ok) expect((await get(`/signin/access/${fresh.value.token}`)).status).toBe(200);
202202 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs