Public: anyone can read this; the crew can change it.
A client's forge link can be revoked and re-issued from the client list, each a receipted action #14743
Check · Review · Land · Live done — Landed and live in production.
torque/forge:torque/14518-a-client-s-forge-link-can-be-revoked-and → main 5 commits fork 3 threads · 3 unresolved
12 files
changed +251
−42
docs/features/forge-link-dead.md +3 −4
| @@ -19,7 +19,7 @@ It extends [forge-link-states.md](forge-link-states.md). | ||
| 19 | 19 | |
| 20 | 20 | | Link | Status | Recipient (`/claim`) | Starter (`/welcome`) | |
| 21 | 21 | | --- | --- | --- | --- | |
| 22 | | Withdrawn | 404 | **This link was withdrawn**: "The sender withdrew this link. Ask *alice* to send you a new one." | "You withdrew this link. Start the forge again from the home page." | | |
| 22 | | Turned off | 410 | **This link was turned off**: "This link was turned off. Ask the person who sent it for a new one." (no sender named) | "You turned this link off. Start the forge again from the home page." | | |
| 23 | 23 | | Expired | 410 | **This link has expired**: "Ask *alice* to send you a new one." | "Start the forge again from the home page." | |
| 24 | 24 | | Unknown | 404 | **This link does not work**: "Sorry, this link does not work any more. Please ask whoever sent it for a new one." | "This link does not work any more. Start the forge again from the home page." | |
| 25 | 25 | |
| @@ -29,9 +29,8 @@ never says "withdrew". A used link and a forge that already has an owner | ||
| 29 | 29 | keep their own words and a **Sign in** button. No dead link sets a session. |
| 30 | 30 | |
| 31 | 31 | The passkey step (`POST /claim/options`, `/welcome/options`) answers JSON |
| 32 | `{ "error": "refused", "message": … }` with the same status. A withdrawn link | |
| 33 | gives the message "The sender withdrew this link. Ask them to send you a new | |
| 34 | one." | |
| 32 | `{ "error": "refused", "message": … }` with the same status. A turned-off link | |
| 33 | gives the message "This link was turned off. Ask the person who sent it for a new one." | |
| 35 | 34 | |
| 36 | 35 | ## What a guess learns |
| 37 | 36 |
docs/features/forge-link-revoke.md +3 −3
| @@ -6,10 +6,10 @@ row: End a forge link, and undo it | ||
| 6 | 6 | |
| 7 | 7 | A forge you sent carries a claim link that works once and for a day. Its sender (any site admin) can end it sooner, and bring it back for a minute. |
| 8 | 8 | |
| 9 | - **Page.** On `/forges`, a forge with an open link shows *Link open* and **Revoke**. After a revoke the page returns to the row with a one-line receipt, and the row shows **Undo** for the next minute, whether or not you stay on the page. Once the minute is over, or the link was used or ran out its day, there is no button. | |
| 9 | - **Page.** On `/forges`, a forge with an open link shows *Link open* and **Revoke**, which opens its own page first: one line says what it does, and only its button ends the link. After a revoke the page returns to the row with a one-line receipt, and the row shows **Undo** for the next minute, whether or not you stay on the page. Once the minute is over, or the link was used or ran out its day, there is no button. | |
| 10 | 10 | - **Terminal.** Open *Forges you sent* from the palette (admins only). `l` picks a forge whose link is open and ends it; `u` picks one whose link ended in the last minute and brings it back. Each row says `link open` or `link ended, undo with u`. |
| 11 | - **MCP.** `claim_link` with `{ name, action: "revoke" }` or `{ name, action: "undo" }`. Without `action`, it still re-issues, and only with `reveal: true`. | |
| 11 | - **MCP.** `claim_link` with `{ name, action: "revoke" }` or `{ name, action: "undo" }`. `dryRun: true` with `revoke` says what it would do and changes nothing. Without `action`, it still re-issues, and only with `reveal: true`. | |
| 12 | 12 | |
| 13 | What the client sees: a withdrawn link answers 404, "The sender withdrew this link", and sets no session ([](forge-link-dead.md) has every case). A used link cannot be revoked; an expired or used link cannot be undone; an undo after 60 s is refused. | |
| 13 | What the client sees: a revoked link answers 410, "This link was turned off. Ask the person who sent it for a new one.", names no sender or forge, and sets no session ([](forge-link-dead.md) has every case). A used link cannot be revoked; an expired or used link cannot be undone; an undo after 60 s is refused. | |
| 14 | 14 | |
| 15 | 15 | Every change is an admin's, same-origin, and writes `forge.claim_revoked` or `forge.claim_restored`. The link's secret is in no event. The state shown on `/forges` is what the child last said to the parent's probe (no extra database open on the list); a revoke or undo forgets it, so the next list asks again. |
src/http/mcp/tools/claim_link.ts +14 −3
| @@ -1,17 +1,19 @@ | ||
| 1 | 1 | import { childNamed } from "../../../actions/spawn.ts"; |
| 2 | 2 | import { firstScreenLine } from "../../../core/next-steps.ts"; |
| 3 | import { revokePreview } from "../../../spawn/manage.ts"; | |
| 3 | 4 | import { changeDoor, previewDoor, reissueDoor } from "../../../spawn/door.ts"; |
| 4 | 5 | import { good, bad, type Tool, str, REVEAL_ARG, LINK_KEPT, LINK_REVEALED } from "../shared.ts"; |
| 5 | 6 | |
| 6 | 7 | export const tool: Tool = { |
| 7 | 8 | name: "claim_link", |
| 8 | doc: "`{ name, action?, reveal? }`: `action` `revoke`/`undo` ends the link or restores it within 1 min; else a new claim link for an unclaimed child, only with `reveal: true`; the old stops. The account that spawned it, or an admin; `revoke`/`undo`: admin", | |
| 9 | doc: "`{ name, action?, reveal? }`: `action` `revoke`/`undo` ends the link or restores it within 1 min; else a new claim link for an unclaimed child, only with `reveal: true`; the old stops. The account that spawned it, or an admin; `revoke`/`undo`: admin; `dryRun` says what it would do and never writes", | |
| 9 | 10 | description: "A child's claim link: `reveal` a new one, or `action` revoke/undo.", |
| 10 | 11 | inputSchema: { |
| 11 | 12 | type: "object", |
| 12 | 13 | properties: { |
| 13 | 14 | name: { type: "string" }, |
| 14 | 15 | action: { enum: ["revoke", "undo"] }, |
| 16 | dryRun: { type: "boolean" }, | |
| 15 | 17 | reveal: REVEAL_ARG, |
| 16 | 18 | }, |
| 17 | 19 | required: ["name"], |
| @@ -19,11 +21,20 @@ export const tool: Tool = { | ||
| 19 | 21 | writes: true, |
| 20 | 22 | async run(ctx, user, args) { |
| 21 | 23 | const action = args["action"]; |
| 22 | if (action === "revoke" || action === "undo") { | |
| 24 | const dry = args["dryRun"] === true; | |
| 25 | if (dry && (action === "revoke" || action === "undo")) { | |
| 26 | // A dry run never writes: the real call's refusals first, then the forge must exist. | |
| 27 | if (!user.isAdmin) return bad("forbidden", "only a site admin can end or bring back a claim link"); | |
| 28 | const name = str(args["name"]); | |
| 29 | if ((await childNamed(ctx.app, name)) === undefined) return bad("not_found", `no child forge named ${name}`); | |
| 30 | const what = action === "revoke" ? revokePreview(name) : `This opens the link for ${name} again, if it was ended within the last minute.`; | |
| 31 | return good({ dryRun: true, says: `${what} Nothing changed yet.` }); | |
| 32 | } | |
| 33 | if (!dry && (action === "revoke" || action === "undo")) { | |
| 23 | 34 | const done = await changeDoor(ctx, user, str(args["name"]), action); |
| 24 | 35 | return done.status >= 400 ? bad(String(done.body["error"]), String(done.body["message"])) : good(done.body); |
| 25 | 36 | } |
| 26 | if (args["reveal"] !== true) { | |
| 37 | if (dry || args["reveal"] !== true) { | |
| 27 | 38 | const seen = await previewDoor(ctx, user, str(args["name"])); |
| 28 | 39 | if (seen.status < 400) return good({ ...seen.body, says: `${String(seen.body["says"])} Nothing changed yet. ${LINK_KEPT}`, reissued: false }); |
| 29 | 40 | return bad(String(seen.body["error"]), String(seen.body["message"])); |
src/http/server.ts +2 −1
| @@ -330,7 +330,7 @@ import { forgeName } from "../web/pages/forge-name.ts"; | ||
| 330 | 330 | import { claimFragmentPage, claimOptions, claimRenew, claimSubmit, welcomePage, welcomeSubmit } from "../web/pages/claim.ts"; |
| 331 | 331 | import { firstAdminOptions, firstAdminPage, firstAdminSubmit } from "../web/pages/first-admin.ts"; |
| 332 | 332 | import { familyPage } from "../web/pages/family.ts"; |
| 333 | import { forgeLinkForm, forgeLinkRevoke, forgeLinkSubmit, forgeLinkUndo, forgeArchiveConfirm, forgeArchiveSubmit, forgeRestoreSubmit, forgeRemoveConfirm, forgeRemoveSubmit, forgeStopSubmit, | |
| 333 | import { forgeLinkForm, forgeRevokeConfirm, forgeLinkRevoke, forgeLinkSubmit, forgeLinkUndo, forgeArchiveConfirm, forgeArchiveSubmit, forgeRestoreSubmit, forgeRemoveConfirm, forgeRemoveSubmit, forgeStopSubmit, | |
| 334 | 334 | forgeOwnCodeSubmit, forgeSizeSubmit, forgeSandboxSubmit, forgesPage, forgesCsv } from "../web/pages/forges.ts"; |
| 335 | 335 | import { billDownload } from "./api/forge_bill.ts"; |
| 336 | 336 | import { forgeRecoveryForm, forgeRecoverySubmit } from "../web/pages/forges.ts"; |
| @@ -989,6 +989,7 @@ export function buildRouter(): Router<Handler> { | ||
| 989 | 989 | router.add("POST", "/forges/:name/recovery-link", forgeRecoverySubmit); |
| 990 | 990 | router.add("GET", "/forges/:name/bill", billDownload); |
| 991 | 991 | router.add("POST", "/forges/:name/claim-link", forgeLinkSubmit); |
| 992 | router.add("GET", "/forges/:name/revoke-link", forgeRevokeConfirm); | |
| 992 | 993 | router.add("POST", "/forges/:name/revoke-link", forgeLinkRevoke); |
| 993 | 994 | router.add("POST", "/forges/:name/undo-link", forgeLinkUndo); |
| 994 | 995 | router.add("POST", "/forges/:name/stop", forgeStopSubmit); |
src/spawn/manage.ts +6
| @@ -56,6 +56,12 @@ export async function removeForge(app: App, name: string): Promise<Managed> { | ||
| 56 | 56 | return { ok: true, says: `${name} is removed, with its data` }; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | /** | |
| 60 | * One line for what ending a forge's link does, shown before the confirm (page and MCP, #14518). | |
| 61 | */ | |
| 62 | export const revokePreview = (name: string): string => | |
| 63 | `This ends the link sent for ${name} now: anyone who opens it is told it was turned off. You can undo it for a minute, or send a new link after.`; | |
| 64 | ||
| 59 | 65 | /** |
| 60 | 66 | * Take a child's unclaimed link back, or undo that within the minute: the |
| 61 | 67 | * site admin's (callers check), receipted; the link's secret is in no event. |
src/tui/screens/forges.ts +16 −2
| @@ -19,7 +19,7 @@ import { diskWords } from "../../spawn/disk_mount.ts"; | ||
| 19 | 19 | import { archiveForge, archivedForges, destinationWords, restoreForge } from "../../spawn/archive.ts"; |
| 20 | 20 | import { writeClientsCsv } from "../../spawn/clients_csv.ts"; |
| 21 | 21 | import { claimedWords, keepWords } from "../../spawn/keep_words.ts"; |
| 22 | import { changeLink, ownCode, removeForge, resizeForge, stopForge } from "../../spawn/manage.ts"; | |
| 22 | import { changeLink, revokePreview, ownCode, removeForge, resizeForge, stopForge } from "../../spawn/manage.ts"; | |
| 23 | 23 | import { moveToCell } from "../../spawn/move_cell.ts"; |
| 24 | 24 | import { readOwnCode } from "../../spawn/own_code.ts"; |
| 25 | 25 | import type { App } from "../../app.ts"; |
| @@ -146,7 +146,21 @@ export function forgesScreen(ctx: Ctx, rows: readonly ClientRow[], session?: Ses | ||
| 146 | 146 | title: act === "revoke" ? "End which link?" : "Bring back which link?", |
| 147 | 147 | items, |
| 148 | 148 | onPick: (ids) => { |
| 149 | if (ids[0] !== undefined) void changeLink(session.app, user.id, ids[0], act).then((done) => ctx.toast(done.says, done.ok ? "success" : "danger")); | |
| 149 | const name = ids[0]; | |
| 150 | if (name === undefined) return; | |
| 151 | const run = (): void => void changeLink(session.app, user.id, name, act).then((done) => ctx.toast(done.says, done.ok ? "success" : "danger")); | |
| 152 | // Ending a link is confirmed with the page's one line (#14518); bringing it back is not. | |
| 153 | if (act === "undo") return run(); | |
| 154 | ctx.push( | |
| 155 | confirmScreen(ctx, screen, { | |
| 156 | title: `End the link for ${name}?`, | |
| 157 | body: [revokePreview(name)], | |
| 158 | actions: [{ label: "Revoke" }, { label: "Keep it" }], | |
| 159 | onChoose: (i) => { | |
| 160 | if (i === 0) run(); | |
| 161 | }, | |
| 162 | }), | |
| 163 | ); | |
| 150 | 164 | }, |
| 151 | 165 | }), |
| 152 | 166 | ); |
src/web/pages/claim.ts +11 −8
| @@ -13,8 +13,8 @@ | ||
| 13 | 13 | * The GET only shows the form — a link unfurled in a chat app must not |
| 14 | 14 | * spend itself — and the POST, same-origin, makes the account and burns the |
| 15 | 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 says it was withdrawn (404); one made | |
| 17 | * up is a plain 404; a forge that was not spawned has no claim page at all. | |
| 16 | * sentence and names who sent it (410); one that is revoked says it was turned off (410, no sender | |
| 17 | * named); one made up is a plain 404; a forge that was not spawned has no claim page at all. | |
| 18 | 18 | * |
| 19 | 19 | * Who keeps access is never silent. By default the spawner's account leaves |
| 20 | 20 | * the organisation's Owners when the forge is claimed (the new owner can |
| @@ -110,6 +110,9 @@ function sender(user: User | undefined): string | undefined { | ||
| 110 | 110 | return user === undefined ? undefined : user.displayName || user.username; |
| 111 | 111 | } |
| 112 | 112 | |
| 113 | /** What a revoked link says to whoever opens it: no sender, no forge detail (#14518). */ | |
| 114 | export const LINK_TURNED_OFF = "This link was turned off. Ask the person who sent it for a new one."; | |
| 115 | ||
| 113 | 116 | function dead( |
| 114 | 117 | ctx: Ctx, |
| 115 | 118 | state: Exclude<ClaimState["state"], "ok">, |
| @@ -144,9 +147,9 @@ function dead( | ||
| 144 | 147 | text: `Ask ${ask} to send you a new one.`, |
| 145 | 148 | }, |
| 146 | 149 | revoked: { |
| 147 | status: 404, | |
| 148 | title: "This link was withdrawn", | |
| 149 | text: `The sender withdrew this link. Ask ${ask} to send you a new one.`, | |
| 150 | status: 410, | |
| 151 | title: "This link was turned off", | |
| 152 | text: LINK_TURNED_OFF, | |
| 150 | 153 | }, |
| 151 | 154 | owned: { |
| 152 | 155 | status: 410, |
| @@ -158,7 +161,7 @@ function dead( | ||
| 158 | 161 | const mine: Partial<Record<typeof state, string>> = { |
| 159 | 162 | unknown: "This link does not work any more. Start the forge again from the home page.", |
| 160 | 163 | expired: "Start the forge again from the home page.", |
| 161 | revoked: "You withdrew this link. Start the forge again from the home page.", | |
| 164 | revoked: "You turned this link off. Start the forge again from the home page.", | |
| 162 | 165 | }; |
| 163 | 166 | if (own) says.text = mine[state] ?? says.text; |
| 164 | 167 | return page( |
| @@ -467,7 +470,7 @@ const LINK_REFUSED: Record<Exclude<ClaimState["state"], "ok">, string> = { | ||
| 467 | 470 | unknown: "This link does not work any more.", |
| 468 | 471 | used: "This link was already used.", |
| 469 | 472 | expired: "This link has expired.", |
| 470 | revoked: "The sender withdrew this link. Ask them to send you a new one.", | |
| 473 | revoked: LINK_TURNED_OFF, | |
| 471 | 474 | owned: "This forge already has an owner.", |
| 472 | 475 | }; |
| 473 | 476 | |
| @@ -487,7 +490,7 @@ async function options(ctx: Ctx): Promise<Response> { | ||
| 487 | 490 | const spawned = await spawnedOrg(ctx.app); |
| 488 | 491 | if (spawned === undefined) return refuse(404, LINK_REFUSED.unknown); |
| 489 | 492 | const state = readClaim(ctx.app.db, spawned.org.id, str(data.token)).state; |
| 490 | if (state !== "ok") return refuse(state === "used" || state === "owned" || state === "expired" ? 410 : 404, LINK_REFUSED[state]); | |
| 493 | if (state !== "ok") return refuse(state === "unknown" ? 404 : 410, LINK_REFUSED[state]); | |
| 491 | 494 | const username = str(data.username).trim(); |
| 492 | 495 | const bad = problems(ctx, spawned, username, emailGiven(ctx, str(data.email))); |
| 493 | 496 | const first = bad.username ?? bad.email; |
src/web/pages/forges.ts +27 −1
| @@ -28,6 +28,7 @@ import { readOwnCode } from "../../spawn/own_code.ts"; | ||
| 28 | 28 | import { moveToCell } from "../../spawn/move_cell.ts"; |
| 29 | 29 | import { sealedAt } from "../../spawn/sealed.ts"; |
| 30 | 30 | import { countRefusals, mayReadRefusals } from "../../core/first_ask_refusals.ts"; |
| 31 | import { revokePreview } from "../../spawn/manage.ts"; | |
| 31 | 32 | import { clientsCsv } from "../../spawn/clients_csv.ts"; |
| 32 | 33 | import { claimLinkBody } from "./new-forge.ts"; |
| 33 | 34 | import { whenWords } from "./claim.ts"; |
| @@ -155,7 +156,7 @@ export function forgeRow(r: ClientRow, now: number, reach?: ForgeReach, bill = f | ||
| 155 | 156 | : null}${r.link === null |
| 156 | 157 | ? null |
| 157 | 158 | : html`<span data-link="${r.link.state}">${badge(LINK_WORDS[r.link.state], { variant: "outline" as const })}</span>${state === "no" && daysLeft !== null ? html`<span data-days-left class="muted">${daysLeft}</span>` : null}${r.link.state === "open" |
| 158 | ? html`<form method="post" action="/forges/${r.name}/revoke-link" class="inline">${btn("Revoke", { size: "sm", title: "End this link now; you can undo for a minute" })}</form>` | |
| 159 | ? btn("Revoke", { size: "sm", href: `/forges/${r.name}/revoke-link`, title: "See what it does, then confirm; you can undo for a minute" }) | |
| 159 | 160 | : undoable(r.link, now) |
| 160 | 161 | ? html`<form method="post" action="/forges/${r.name}/undo-link" class="inline">${btn("Undo", { size: "sm", title: "Bring the link back; possible for a minute after it ended" })}</form>` |
| 161 | 162 | : null}`}${r.where.movable |
| @@ -362,6 +363,31 @@ export async function forgeLinkForm(ctx: Ctx): Promise<Response> { | ||
| 362 | 363 | return response; |
| 363 | 364 | } |
| 364 | 365 | |
| 366 | // GET /forges/:name/revoke-link — what ending the link does in one line and the one button that | |
| 367 | // does it; nothing changes until it is pressed (#14518). | |
| 368 | export async function forgeRevokeConfirm(ctx: Ctx): Promise<Response> { | |
| 369 | const user = ctx.auth.user; | |
| 370 | if (!user?.isAdmin) return notFoundPage(ctx); | |
| 371 | const name = ctx.params["name"] ?? ""; | |
| 372 | if (!CHILD_NAME.test(name)) return notFoundPage(ctx); | |
| 373 | const response = page({ | |
| 374 | ctx, | |
| 375 | title: "End this link", | |
| 376 | body: html`<div class="narrow-wide" data-forges> | |
| 377 | ${pageHead(`End the link for ${name}`)} | |
| 378 | <section class="settings-block"> | |
| 379 | <p data-preview>${revokePreview(name)}</p> | |
| 380 | <form method="post" action="/forges/${name}/revoke-link" class="form-actions"> | |
| 381 | ${btn("Revoke", { variant: "primary", title: "End the link now" })} | |
| 382 | ${btn("Keep it", { href: "/forges", variant: "outline" })} | |
| 383 | </form> | |
| 384 | </section> | |
| 385 | </div>`, | |
| 386 | }); | |
| 387 | response.headers.set("cache-control", "no-store"); | |
| 388 | return response; | |
| 389 | } | |
| 390 | ||
| 365 | 391 | /** One line each for what was archived: where the copy is, and the way back. */ |
| 366 | 392 | function archivedSection(ctx: Ctx, archived: readonly ArchiveRecord[]): Html | null { |
| 367 | 393 | const said = ctx.url.searchParams.get("archive"); |
test/claim-revoke.test.ts +46 −12
| @@ -32,6 +32,7 @@ describe("revoking a forge link", () => { | ||
| 32 | 32 | let rwToken = ""; |
| 33 | 33 | let aliceCookie = ""; |
| 34 | 34 | let bobCookie = ""; |
| 35 | let bobId = ""; | |
| 35 | 36 | const state = (name: string) => linkOf(kids[name]!.app.db)?.state; |
| 36 | 37 | const orgOf = async (name: string) => (await spawnedOrg(kids[name]!.app))!.org.id; |
| 37 | 38 | /** The page's button, as a browser posts it. */ |
| @@ -60,6 +61,7 @@ describe("revoking a forge link", () => { | ||
| 60 | 61 | aliceCookie = `forge_session=${createSession(parent.db, alice.id).token}`; |
| 61 | 62 | const bob = await createUser(parent.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-long-claim-passphrase" }); |
| 62 | 63 | if (!bob.ok) throw new Error("bob"); |
| 64 | bobId = bob.value.id; | |
| 63 | 65 | bobCookie = `forge_session=${createSession(parent.db, bob.value.id).token}`; |
| 64 | 66 | const rw = createToken(parent.db, alice.id, "rw", "rw").token; |
| 65 | 67 | rwToken = rw; |
| @@ -95,13 +97,13 @@ describe("revoking a forge link", () => { | ||
| 95 | 97 | await rm(dir, { recursive: true, force: true }); |
| 96 | 98 | }); |
| 97 | 99 | |
| 98 | test("revoke-then-use answers 404 in plain words and sets no session", async () => { | |
| 100 | test("revoke-then-use answers 410 in plain words and sets no session", async () => { | |
| 99 | 101 | expect(state("one")).toBe("open"); |
| 100 | 102 | const done = await revoke("one", "revoke"); |
| 101 | 103 | expect(done.ok).toBe(true); |
| 102 | 104 | expect(state("one")).toBe("ended"); |
| 103 | 105 | const res = await claim("one"); |
| 104 | expect(res.status).toBe(404); | |
| 106 | expect(res.status).toBe(410); | |
| 105 | 107 | expect(res.headers.get("set-cookie")).toBeNull(); |
| 106 | 108 | const form = { token: kids["one"]!.token, username: "x", email: "[email protected]" }; |
| 107 | 109 | const post = await fetch(`${kids["one"]!.servers.url}/claim`, { |
| @@ -110,12 +112,12 @@ describe("revoking a forge link", () => { | ||
| 110 | 112 | body: new URLSearchParams({ ...form, ...(await passkeyFields(kids["one"]!.servers.url, "/claim/options", form).catch(() => ({}))) }), |
| 111 | 113 | redirect: "manual", |
| 112 | 114 | }); |
| 113 | expect(post.status).toBe(404); | |
| 115 | expect(post.status).toBe(410); | |
| 114 | 116 | expect(post.headers.get("set-cookie")).toBeNull(); |
| 115 | expect(await post.text()).toContain("The sender withdrew this link"); | |
| 117 | expect(await post.text()).toContain("This link was turned off. Ask the person who sent it for a new one."); | |
| 116 | 118 | }); |
| 117 | 119 | |
| 118 | test("the starter's own page says 'You withdrew this link'; the recipient's never does, nor the reverse", async () => { | |
| 120 | test("the starter's own page says 'You turned this link off'; the recipient's never does, nor the reverse", async () => { | |
| 119 | 121 | // "one" is still revoked by the test above; the next test undoes it. |
| 120 | 122 | const url = kids["one"]!.servers.url; |
| 121 | 123 | const send = (path: string) => |
| @@ -127,14 +129,14 @@ describe("revoking a forge link", () => { | ||
| 127 | 129 | }); |
| 128 | 130 | const own = await send("/welcome"); |
| 129 | 131 | const ownText = await own.text(); |
| 130 | expect(own.status).toBe(404); | |
| 132 | expect(own.status).toBe(410); | |
| 131 | 133 | expect(own.headers.get("set-cookie")).toBeNull(); |
| 132 | expect(ownText).toContain("You withdrew this link"); | |
| 133 | expect(ownText).not.toContain("The sender withdrew this link"); | |
| 134 | expect(ownText).toContain("You turned this link off"); | |
| 135 | expect(ownText).not.toContain("This link was turned off. Ask the person who sent it for a new one."); | |
| 134 | 136 | const theirs = await send("/claim"); |
| 135 | 137 | const theirText = await theirs.text(); |
| 136 | expect(theirText).toContain("The sender withdrew this link"); | |
| 137 | expect(theirText).not.toContain("You withdrew this link"); | |
| 138 | expect(theirText).toContain("This link was turned off. Ask the person who sent it for a new one."); | |
| 139 | expect(theirText).not.toContain("You turned this link off"); | |
| 138 | 140 | }); |
| 139 | 141 | |
| 140 | 142 | test("a revoke is receipted, and undone within the minute", async () => { |
| @@ -217,7 +219,7 @@ describe("revoking a forge link", () => { | ||
| 217 | 219 | const list = await (await fetch(`${parentServers.url}${where}`, { headers: { cookie: aliceCookie } })).text(); |
| 218 | 220 | expect(list).toContain("has ended. Undo is beside it"); |
| 219 | 221 | expect(list).toContain('action="/forges/one/undo-link"'); |
| 220 | expect(list).not.toContain('action="/forges/one/revoke-link"'); | |
| 222 | expect(list).not.toContain(`href="/forges/one/revoke-link"`); | |
| 221 | 223 | // Leaving the receipt behind, the row still offers Undo for the minute. |
| 222 | 224 | const plain = await (await fetch(`${parentServers.url}/forges`, { headers: { cookie: aliceCookie } })).text(); |
| 223 | 225 | expect(plain).toContain('action="/forges/one/undo-link"'); |
| @@ -226,7 +228,7 @@ describe("revoking a forge link", () => { | ||
| 226 | 228 | expect(back.headers.get("location")).toBe("/forges?link=restored&forge=one"); |
| 227 | 229 | const after = await (await fetch(`${parentServers.url}/forges`, { headers: { cookie: aliceCookie } })).text(); |
| 228 | 230 | expect(after).not.toContain('action="/forges/one/undo-link"'); |
| 229 | expect(after).toContain('action="/forges/one/revoke-link"'); | |
| 231 | expect(after).toContain(`href="/forges/one/revoke-link"`); | |
| 230 | 232 | }); |
| 231 | 233 | |
| 232 | 234 | test("a receipt names only a forge in the list, in fixed words", async () => { |
| @@ -254,6 +256,38 @@ describe("revoking a forge link", () => { | ||
| 254 | 256 | expect(state("one")).toBe("open"); |
| 255 | 257 | }); |
| 256 | 258 | |
| 259 | test("dryRun never writes: not for undo, reveal or a non-admin, and a missing forge is not found", async () => { | |
| 260 | const call = async (args: Record<string, unknown>, token = rwToken) => { | |
| 261 | const res = await fetch(`${parentServers.url}/mcp`, { | |
| 262 | method: "POST", | |
| 263 | headers: { "content-type": "application/json", authorization: `Bearer ${token}` }, | |
| 264 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "claim_link", arguments: args } }), | |
| 265 | }); | |
| 266 | const r = ((await res.json()) as any).result; | |
| 267 | return { error: r.isError === true, text: r.content[0].text as string }; | |
| 268 | }; | |
| 269 | expect((await call({ name: "one", action: "revoke" })).error).toBe(false); | |
| 270 | expect(state("one")).toBe("ended"); | |
| 271 | const undo = await call({ name: "one", action: "undo", dryRun: true }); | |
| 272 | expect(undo.error).toBe(false); | |
| 273 | expect(undo.text).toContain("Nothing changed yet"); | |
| 274 | expect(state("one")).toBe("ended"); | |
| 275 | expect((await call({ name: "one", action: "undo" })).error).toBe(false); | |
| 276 | expect(state("one")).toBe("open"); | |
| 277 | const bobToken = createToken(parent.db, bobId, "rw", "rw").token; | |
| 278 | const refused = await call({ name: "one", action: "revoke", dryRun: true }, bobToken); | |
| 279 | expect(refused.error).toBe(true); | |
| 280 | expect(refused.text).toContain("only a site admin"); | |
| 281 | expect((await call({ name: "one", action: "revoke", dryRun: true }, bobToken)).text).toBe((await call({ name: "one", action: "revoke" }, bobToken)).text); | |
| 282 | expect((await call({ name: "one", action: "undo", dryRun: true }, bobToken)).error).toBe(true); | |
| 283 | expect(state("one")).toBe("open"); | |
| 284 | const reveal = await call({ name: "one", reveal: true, dryRun: true }); | |
| 285 | expect(reveal.text).toContain("Nothing changed yet"); | |
| 286 | const missing = await call({ name: "nobody", action: "revoke", dryRun: true }); | |
| 287 | expect(missing.error).toBe(true); | |
| 288 | expect(missing.text).toContain("no child forge named nobody"); | |
| 289 | }); | |
| 290 | ||
| 257 | 291 | test("the terminal's action ends and restores a link through the same code, and names a missing forge", async () => { |
| 258 | 292 | const ended = await changeLink(parent, alice.id, "one", "revoke"); |
| 259 | 293 | expect(ended.ok).toBe(true); |
test/client-link-revoke.test.ts added +115
| @@ -0,0 +1,115 @@ | ||
| 1 | /** | |
| 2 | * A client's forge link can be revoked and re-issued from the client list (#14518): Revoke is | |
| 3 | * confirmed on its own page, a revoked link answers 410 in fixed words with no forge detail, a new | |
| 4 | * link works while the old stays dead, and a cross-origin POST of either is refused. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, 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 { createUser } from "../src/core/users.ts"; | |
| 13 | import { createSession } from "../src/core/sessions.ts"; | |
| 14 | import { createToken } from "../src/core/tokens.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { linkOf } from "../src/spawn/claim.ts"; | |
| 17 | import { useChildFetch } from "../src/spawn/clients.ts"; | |
| 18 | import { firstBoot } from "../src/spawn/first-boot.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | const TURNED_OFF = "This link was turned off. Ask the person who sent it for a new one."; | |
| 24 | let dir = ""; | |
| 25 | let parent: App; | |
| 26 | let servers: Servers; | |
| 27 | let kid: { app: App; servers: Servers; token: string }; | |
| 28 | let cookie = ""; | |
| 29 | let rw = ""; | |
| 30 | ||
Box 4/5 cross-origin checks are weak: the revoke one only asserts the location lacks |
||
| 31 | const post = (path: string, origin = "https://forge.test") => | |
| 32 | fetch(`${servers.url}${path}`, { method: "POST", headers: { cookie, origin }, redirect: "manual" }); | |
| 33 | const options = (token: string) => | |
| 34 | fetch(`${kid.servers.url}/claim/options`, { | |
| 35 | method: "POST", | |
| 36 | headers: { origin: kid.servers.url, "content-type": "application/json" }, | |
| 37 | body: JSON.stringify({ token, username: "friend", email: "[email protected]" }), | |
| 38 | }); | |
| 39 | ||
| 40 | beforeAll(async () => { | |
| 41 | dir = await mkdtemp(join(tmpdir(), "forge-link-revoke-")); | |
| 42 | parent = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 43 | const made = await createUser(parent.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-long-claim-passphrase", isAdmin: true }); | |
| 44 | if (!made.ok) throw new Error("user"); | |
| 45 | cookie = `forge_session=${createSession(parent.db, made.value.id).token}`; | |
| 46 | rw = createToken(parent.db, made.value.id, "rw", "rw").token; | |
| 47 | servers = await serve(parent); | |
| 48 | const res = await fetch(`${servers.url}/api/v1/forges`, { | |
| 49 | method: "POST", | |
| 50 | headers: { authorization: `Bearer ${rw}`, "content-type": "application/json" }, | |
| 51 | body: JSON.stringify({ name: "client", apps: [], dryRun: false }), | |
| 52 | }); | |
| 53 | if (res.status !== 201) throw new Error(`spawn ${res.status}`); | |
| 54 | const body = (await res.json()) as { claimUrl: string; bodyId: string }; | |
| 55 | const app = createApp({ dataDir: childDirOf(parent, body.bodyId), port: 0 }); | |
| 56 | await firstBoot(app); | |
| 57 | kid = { app, servers: await serve(app), token: body.claimUrl.split("#")[1] ?? "" }; | |
| 58 | useChildFetch(parent, (input, init) => fetch(`${kid.servers.url}${new URL(String(input)).pathname}`, init)); | |
| 59 | }, 60_000); | |
| 60 | ||
| 61 | afterAll(async () => { | |
| 62 | kid.servers.stop(); | |
| 63 | kid.app.close(); | |
| 64 | servers.stop(); | |
| 65 | parent.close(); | |
| 66 | await rm(dir, { recursive: true, force: true }); | |
| 67 | }); | |
| 68 | ||
| 69 | test("Revoke asks first, a cross-origin POST changes nothing, the confirm ends the link", async () => { | |
| 70 | const list = await (await fetch(`${servers.url}/forges`, { headers: { cookie } })).text(); | |
| 71 | expect(list).toContain('href="/forges/client/revoke-link"'); | |
| 72 | const ask = await fetch(`${servers.url}/forges/client/revoke-link`, { headers: { cookie } }); | |
| 73 | expect(ask.status).toBe(200); | |
| 74 | expect(await ask.text()).toContain("anyone who opens it is told it was turned off"); | |
| 75 | expect(linkOf(kid.app.db)?.state).toBe("open"); | |
| 76 | const cross = await post("/forges/client/revoke-link", "https://evil.test"); | |
| 77 | expect(cross.status).toBe(403); | |
| 78 | expect(cross.headers.get("location") ?? "").not.toContain("link=revoked"); | |
| 79 | expect(linkOf(kid.app.db)?.state).toBe("open"); | |
| 80 | expect((await post("/forges/client/revoke-link")).headers.get("location")).toBe("/forges?link=revoked&forge=client"); | |
| 81 | expect(linkOf(kid.app.db)?.state).toBe("ended"); | |
| 82 | }); | |
| 83 | ||
| 84 | test("a revoked link is 410 in fixed words, naming no sender and no forge", async () => { | |
| 85 | const res = await options(kid.token); | |
| 86 | expect(res.status).toBe(410); | |
| 87 | expect(res.headers.get("set-cookie")).toBeNull(); | |
| 88 | const page = await fetch(`${kid.servers.url}/claim`); | |
| 89 | expect(page.status).toBe(410); | |
| 90 | const text = await page.text(); | |
| 91 | expect(text).toContain(TURNED_OFF); | |
| 92 | expect(text).not.toContain("alice"); | |
| 93 | }); | |
| 94 | ||
| 95 | test("the row offers a new link; cross-origin is refused; the new link works and the old stays dead", async () => { | |
| 96 | const list = await (await fetch(`${servers.url}/forges`, { headers: { cookie } })).text(); | |
| 97 | expect(list).toContain('href="/forges/client/claim-link"'); | |
| 98 | const old = kid.token; | |
| 99 | const tokensBefore = parent.db.query<{ n: number }, []>("SELECT count(*) AS n FROM user_setting").get()!.n; | |
| 100 | const cross = await post("/forges/client/claim-link", "https://evil.test"); | |
| 101 | expect(cross.status).toBe(403); | |
| 102 | expect(linkOf(kid.app.db)?.state).toBe("ended"); | |
| 103 | expect(parent.db.query<{ n: number }, []>("SELECT count(*) AS n FROM user_setting").get()!.n).toBe(tokensBefore); | |
| 104 | const sent = await post("/forges/client/claim-link"); | |
| 105 | expect(sent.status).toBe(201); | |
| 106 | const text = await sent.text(); | |
| 107 | const fresh = /#([A-Za-z0-9_-]{16,})/.exec(text)?.[1] ?? ""; | |
| 108 | expect(fresh).not.toBe(""); | |
| 109 | expect(fresh).not.toBe(old); | |
| 110 | expect((await options(fresh)).status).toBe(200); | |
| 111 | expect((await options(old)).status).toBe(404); | |
| 112 | // The token is not kept in the forge's list page afterwards. | |
| 113 | const after = await (await fetch(`${servers.url}/forges`, { headers: { cookie } })).text(); | |
| 114 | expect(after).not.toContain(fresh); | |
| 115 | }); |
test/forge-link-landing.test.ts +7 −7
| @@ -84,7 +84,7 @@ describe("a forge link's landing page", () => { | ||
| 84 | 84 | expect(text).not.toMatch(/href="[^"]*token[^"]*"/i); |
| 85 | 85 | }); |
| 86 | 86 | |
| 87 | test("a revoked link says the sender withdrew it; an unknown token says only that it does not work; neither names the forge", async () => { | |
| 87 | test("a revoked link says it was turned off; an unknown token says only that it does not work; neither names the forge", async () => { | |
| 88 | 88 | setStored({ e: future(), r: new Date().toISOString() }); |
| 89 | 89 | const revoked = await get(); |
| 90 | 90 | const withToken = await fetch(`${childServers.url}/claim`, { |
| @@ -101,13 +101,13 @@ describe("a forge link's landing page", () => { | ||
| 101 | 101 | const expired = await get(); |
| 102 | 102 | const [rev, tok, unk] = await Promise.all([revoked, withToken, unknown].map((r) => r.text())); |
| 103 | 103 | for (const r of [revoked, withToken, unknown]) { |
| 104 | expect(r.status).toBe(404); | |
| 104 | expect(r.status).toBe(r === unknown ? 404 : 410); | |
| 105 | 105 | expect(r.headers.get("x-content-type-options")).toBe("nosniff"); |
| 106 | 106 | expect(r.headers.get("set-cookie")).toBeNull(); |
| 107 | 107 | } |
| 108 | 108 | for (const t of [rev!, tok!]) { |
| 109 | expect(main(t)).toContain("The sender withdrew this link."); | |
| 110 | expect(main(t)).toContain("to send you a new one."); | |
| 109 | expect(main(t)).toContain("This link was turned off. Ask the person who sent it for a new one."); | |
| 110 | expect(main(t)).not.toContain("alice"); | |
| 111 | 111 | expect(main(t)).not.toMatch(/expired/i); |
| 112 | 112 | } |
| 113 | 113 | expect(unk).toContain("does not work any more"); |
| @@ -143,15 +143,15 @@ describe("a forge link's landing page", () => { | ||
| 143 | 143 | setStored({ u: null }); |
| 144 | 144 | }); |
| 145 | 145 | |
| 146 | test("the passkey path answers 410 for an expired link and a spent one, 404 for a revoked one", async () => { | |
| 146 | test("the passkey path answers 410 for an expired link and a spent one, 410 for a revoked one", async () => { | |
| 147 | 147 | setStored({ r: undefined, u: null, e: new Date(Date.now() - 3_600_000).toISOString() }); |
| 148 | 148 | expect((await optionsFor()).status).toBe(410); |
| 149 | 149 | setStored({ e: future(), u: new Date().toISOString() }); |
| 150 | 150 | expect((await optionsFor()).status).toBe(410); |
| 151 | 151 | setStored({ u: null, r: new Date().toISOString() }); |
| 152 | 152 | const revoked = await optionsFor(); |
| 153 | expect(revoked.status).toBe(404); | |
| 154 | expect(await revoked.text()).toContain("The sender withdrew this link."); | |
| 153 | expect(revoked.status).toBe(410); | |
| 154 | expect(await revoked.text()).toContain("This link was turned off."); | |
| 155 | 155 | setStored({ r: undefined, e: future() }); |
| 156 | 156 | }); |
| 157 | 157 |
test/stranger-words.test.ts +1 −1
| @@ -139,7 +139,7 @@ describe("a stranger's way to their own forge", () => { | ||
| 139 | 139 | screens.push(["/claim passkey note, unknown", await note(404, { ...who, token: "nope" })]); |
| 140 | 140 | screens.push(["/claim, a passkey that failed", await submit(422, { ...who, challenge: "x", clientDataJSON: "x", attestationObject: "x" })]); |
| 141 | 141 | expect(revokeClaim(db, org)).toBe(true); |
| 142 | await both("revoked", 404); | |
| 142 | await both("revoked", 410); | |
| 143 | 143 | expect(restoreClaim(db, org)).toBe(true); |
| 144 | 144 | as({ e: "2000-01-01T00:00:00.000Z" }); |
| 145 | 145 | await both("expired", 410); |
new above
offline
dryRun: trueis honoured only forrevokeby an admin. Withaction: "undo", or from a non-admin, it falls through and performs the write. A flag named dryRun must never write: refuse or preview for every action, and for non-admins return the same refusal as the real call. Also the preview is built from an unvalidatedstr(args.name)without checking the forge exists; use childNamed and answer not-found. Add tests for dryRun+undo and dryRun by a non-admin.