Public: anyone can read this; the crew can change it.
The account page lists the browsers signed in to it and can sign out every other one in one confirmed action #14571
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14559-the-account-page-lists-the-browsers-sign → main 8 commits fork 3 threads · 3 unresolved
docs/features/sign-out-elsewhere.md added +15
| @@ -0,0 +1,15 @@ | ||
| 1 | --- | |
| 2 | topic: Every setting, by topic | |
| 3 | row: Sign out every other browser at once | |
| 4 | --- | |
| 5 | # Sign out every other browser at once | |
| 6 | ||
| 7 | A lost phone or a shared laptop is closed from wherever you are. Open **Settings → Tokens** (`/settings/tokens`); under **Signed-in browsers**, when another browser is signed in besides this one, press **Sign out everywhere else**. A confirm page says what it does; on yes every other browser of your account is signed out at once and has to sign in again, and this one stays signed in. The page then reads "Signed out of 2 other browsers." (or "No other browser was signed in."). | |
| 8 | ||
| 9 | - Only web sessions end. App terminals, machine shells and access codes (tokens) are not touched; revoke a token on its own row. | |
| 10 | - The act is a same-origin POST to `/settings/sessions/end-others`, reached only through the confirm page. A request with no browser session of its own (an access code, say) is refused: there is no "this one" to keep. | |
| 11 | - Each ended browser's open streams end with it, and the record keeps one `account.signed_out_elsewhere` event with you as actor and how many were ended; only you see it. | |
| 12 | ||
| 13 | ## Terminal | |
| 14 | ||
| 15 | The palette's **Signed in** lists your browsers, tokens and this terminal's key. `o` signs out every browser after a confirm (Keep is the default): the terminal is held by its key, so no browser is "this one" and all of them end. The same `account.signed_out_elsewhere` event is written, only when a browser was signed out. `d` still signs out one browser or revokes one token. |
src/actions/signins.ts +43
| @@ -9,6 +9,8 @@ | ||
| 9 | 9 | import type { App } from "../app.ts"; |
| 10 | 10 | import { |
| 11 | 11 | destroySession, |
| 12 | endAllSessions, | |
| 13 | endOtherSessions, | |
| 12 | 14 | endSession, |
| 13 | 15 | listSessions, |
| 14 | 16 | sessionOf, |
| @@ -127,3 +129,44 @@ export function endAccountSession( | ||
| 127 | 129 | }); |
| 128 | 130 | return ok(); |
| 129 | 131 | } |
| 132 | ||
| 133 | /** | |
| 134 | * Sign the person out of every browser but the one asking (#14559): a lost | |
| 135 | * phone or a shared laptop closed from anywhere. The receipt is theirs on | |
| 136 | * the record, and each ended browser's open streams end with it. | |
| 137 | */ | |
| 138 | export function signOutElsewhere( | |
| 139 | app: App, | |
| 140 | actor: User | undefined, | |
| 141 | current: string | undefined, | |
| 142 | ): Result<{ ended: number }, "not_found"> { | |
| 143 | const asker = requireUser(actor); | |
| 144 | if (!asker.ok || current === undefined) return err("not_found"); | |
| 145 | return signedOut(app, asker.value, endOtherSessions(app.db, asker.value.id, current)); | |
| 146 | } | |
| 147 | ||
| 148 | /** | |
| 149 | * Sign the person out of every browser (#14559): the terminal's "everywhere | |
| 150 | * else", since a terminal is held by its key and no browser is this one. | |
| 151 | */ | |
| 152 | export function signOutEveryBrowser(app: App, actor: User | undefined): Result<{ ended: number }, "not_found"> { | |
| 153 | const asker = requireUser(actor); | |
| 154 | if (!asker.ok) return err("not_found"); | |
| 155 | return signedOut(app, asker.value, endAllSessions(app.db, asker.value.id)); | |
| 156 | } | |
| 157 | ||
| 158 | /** | |
| 159 | * The receipt and each ended browser's stream end, for both doors. A press | |
| 160 | * that ended nothing (a replayed confirm, no other browser) writes no receipt. | |
| 161 | */ | |
| 162 | function signedOut(app: App, user: User, ended: readonly string[]): Result<{ ended: number }, "not_found"> { | |
| 163 | if (ended.length === 0) return ok({ ended: 0 }); | |
| 164 | app.bus.append({ | |
| 165 | kind: "account.signed_out_elsewhere", | |
| 166 | actorId: user.id, | |
| 167 | payload: { username: user.username, sessionsEnded: ended.length }, | |
| 168 | }); | |
| 169 | for (const sessionId of ended) | |
| 170 | app.bus.append({ kind: "session.ended", actorId: user.id, payload: { sessionId } }); | |
| 171 | return ok({ ended: ended.length }); | |
| 172 | } |
src/core/event_audience.ts +2
| @@ -363,6 +363,8 @@ export function classifiedAudience(kind: string): EventAudience | undefined { | ||
| 363 | 363 | kind.startsWith("reach.") || |
| 364 | 364 | // A stranger's ask for a new forge link, for the person who made it (`spawn/heartbeat.ts`). |
| 365 | 365 | kind === "forge.claim_renewal_note" || |
| 366 | // A person signed their other browsers out (`actions/signins.ts`): their own receipt. | |
| 367 | kind === "account.signed_out_elsewhere" || | |
| 366 | 368 | kind.startsWith("thought.") |
| 367 | 369 | ) |
| 368 | 370 | return "actor"; |
src/core/sessions.ts +31
| @@ -188,6 +188,37 @@ export function endSession(db: Database, userId: string, id: string): boolean { | ||
| 188 | 188 | ); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | /** | |
| 192 | * Sign out every browser of this account but the one holding `keep` (#14559), | |
| 193 | * the ids of those ended. Only web sessions: app terminals and machine shells | |
| 194 | * keep their own lists. Nothing ends when `keep` is not this account's. | |
| 195 | */ | |
| 196 | export function endOtherSessions(db: Database, userId: string, keep: string): string[] { | |
| 197 | const hash = sha256(keep); | |
| 198 | return db.transaction(() => { | |
| 199 | const mine = db | |
| 200 | .query<{ n: number }, [string, string]>("SELECT COUNT(*) AS n FROM session WHERE token_hash = ?1 AND user_id = ?2") | |
| 201 | .get(hash, userId); | |
| 202 | if (!mine || mine.n === 0) return []; | |
| 203 | return db | |
| 204 | .query<{ id: string }, [string, string]>("DELETE FROM session WHERE user_id = ?1 AND token_hash <> ?2 RETURNING id") | |
| 205 | .all(userId, hash) | |
| 206 | .map((r) => r.id); | |
| 207 | })(); | |
| 208 | } | |
| 209 | ||
| 210 | /** | |
| 211 | * Sign out every browser of this account (#14559), the ids of those ended: | |
| 212 | * what a terminal, which holds no browser of its own, means by "everywhere | |
| 213 | * else". Only web sessions, as `endOtherSessions`. | |
| 214 | */ | |
| 215 | export function endAllSessions(db: Database, userId: string): string[] { | |
| 216 | return db | |
| 217 | .query<{ id: string }, [string]>("DELETE FROM session WHERE user_id = ?1 RETURNING id") | |
| 218 | .all(userId) | |
| 219 | .map((r) => r.id); | |
| 220 | } | |
| 221 | ||
| 191 | 222 | /** Housekeeping for the background worker: expired and idle sessions go. */ |
| 192 | 223 | export function pruneSessions(db: Database): number { |
| 193 | 224 | const now = new Date(); |
src/http/marks/settings.json +1
| @@ -30,6 +30,7 @@ | ||
| 30 | 30 | "/settings/tokens/:id/delete": "open", |
| 31 | 31 | "/settings/grants": "open", |
| 32 | 32 | "/settings/grants/:id/revoke": "open", |
| 33 | "/settings/sessions/end-others": "open", | |
| 33 | 34 | "/settings/sessions/:id/delete": "open", |
| 34 | 35 | "/settings/confirms": "open", |
| 35 | 36 | "/settings/confirms/:id": "open", |
src/http/settings_routes.ts +2
| @@ -122,6 +122,7 @@ import { | ||
| 122 | 122 | passwordUpdate, |
| 123 | 123 | tokenCreate, |
| 124 | 124 | sessionDelete, |
| 125 | sessionsOthersDelete, | |
| 125 | 126 | tokenDelete, |
| 126 | 127 | tokensPage, |
| 127 | 128 | } from "../web/pages/account.ts"; |
| @@ -186,6 +187,7 @@ export function settingsRoutes(router: Router<Handler>, knownPatterns: () => rea | ||
| 186 | 187 | router.add("POST", "/settings/tokens/:id/delete", tokenDelete); |
| 187 | 188 | router.add("GET", "/settings/grants", grantsPage); |
| 188 | 189 | router.add("POST", "/settings/grants/:id/revoke", grantRevoke); |
| 190 | router.add("POST", "/settings/sessions/end-others", sessionsOthersDelete); | |
| 189 | 191 | router.add("POST", "/settings/sessions/:id/delete", sessionDelete); |
| 190 | 192 | router.add("GET", "/settings/confirms", confirmsList); |
| 191 | 193 | router.add("POST", "/settings/confirms/:id", confirmYes); |
src/tui/screens/signins.ts +47 −14
| @@ -8,21 +8,21 @@ | ||
| 8 | 8 | * as Settings and the API do (your own only, and `token.deleted` is emitted). |
| 9 | 9 | * `m` makes a token through the action `createToken` (what Settings uses, so |
| 10 | 10 | * `token.created` is emitted and `ADMIN_TOKEN_WORDS` is the refusal): a name, |
| 11 | * a scope, and for an admin's code of any scope a second explicit `y`. The secret is shown once on a | |
| 12 | * screen of its own and forgotten when that screen is left. Only a person | |
| 13 | * minting from a terminal session may: an agent is not offered `m`. | |
| 14 | * `p` changes your password through the action `changePassword` (what | |
| 15 | * Settings › Password and the API use, so the sign-in throttle applies and | |
| 16 | * `user.updated` is emitted): the current one, the new one and the new one | |
| 17 | * again, each in a masked prompt. The typed passwords live only in the | |
| 18 | * prompts' closures for the call; refusals read in Settings' words. An agent | |
| 19 | * is not offered `p`, as it is not offered `m`. | |
| 20 | * This terminal is held by its key, not a browser session, and cannot be | |
| 21 | * ended from here. | |
| 11 | * a scope, and for an admin's code of any scope a second explicit `y`. The secret is shown once on | |
| 12 | * a screen of its own and forgotten when that screen is left. Only a person minting from a terminal | |
| 13 | * session may: an agent is not offered `m`. `p` changes your password through the action | |
| 14 | * `changePassword` (what Settings › Password and the API use, so the sign-in throttle applies and | |
| 15 | * `user.updated` is emitted): the current one, the new one and the new one again, each in a masked | |
| 16 | * prompt. The typed passwords live only in the prompts' closures for the call; refusals read in | |
| 17 | * Settings' words. An agent is not offered `p`, as it is not offered `m`. | |
| 18 | * `o` signs out every browser after a confirm, through `signOutEveryBrowser` | |
| 19 | * (#14559). Unlike the page's "Sign out everywhere else", which keeps the | |
| 20 | * browser asking, it ends all browsers: this terminal is held by its key, | |
| 21 | * not a browser session, so its key stays and it cannot be ended from here. | |
| 22 | 22 | */ |
outdated
The terminal's |
||
| 23 | 23 | |
| 24 | 24 | import { UPSTREAM_REPO } from "../../core/upstream.ts"; |
| 25 | import { accountSignIns, endAccountSession } from "../../actions/signins.ts"; | |
| 25 | import { accountSignIns, endAccountSession, signOutEveryBrowser } from "../../actions/signins.ts"; | |
| 26 | 26 | import { |
| 27 | 27 | CHANGE_PASSWORD_WORDS, |
| 28 | 28 | PASSWORD_CHANGED_WORDS, |
| @@ -208,6 +208,34 @@ export function signInsScreen(f: Session, ctx: Ctx): Screen { | ||
| 208 | 208 | ); |
| 209 | 209 | }; |
| 210 | 210 | |
| 211 | const endBrowsers = (all: readonly SignIn[]): void => { | |
| 212 | const user = f.user; | |
| 213 | if (user === undefined) return; | |
| 214 | const n = all.filter((s) => s.kind === "browser").length; | |
| 215 | if (n === 0) { | |
| 216 | note = "No browser is signed in."; | |
| 217 | ctx.invalidate(); | |
| 218 | return; | |
| 219 | } | |
| 220 | ctx.push( | |
| 221 | confirmScreen(ctx, screen, { | |
| 222 | title: "Sign out every browser", | |
| 223 | body: [ | |
| 224 | `Sign out ${n === 1 ? "the 1 browser" : `all ${n} browsers`} signed in to this account? Each has to sign in again.`, | |
| 225 | "Every browser goes, including any you are using now. This terminal's key stays, and app terminals and machine shells are not touched.", | |
| 226 | ], | |
| 227 | actions: [{ label: "Keep", variant: "ghost" }, { label: "Sign out", variant: "danger" }], | |
| 228 | onChoose: (i) => { | |
| 229 | if (i < 1) return; | |
| 230 | const done = signOutEveryBrowser(f.app, user); | |
| 231 | const ended = done.ok ? done.value.ended : 0; | |
| 232 | note = ended === 0 ? "No browser was signed in." : `Signed out of ${ended} ${ended === 1 ? "browser" : "browsers"}.`; | |
| 233 | ctx.invalidate(); | |
| 234 | }, | |
| 235 | }), | |
| 236 | ); | |
| 237 | }; | |
| 238 | ||
| 211 | 239 | /** Pushes the secret's screen; call it once the modal has popped. */ |
| 212 | 240 | const mint = (name: string, scope: TokenScope): void => { |
| 213 | 241 | const user = f.user; |
| @@ -328,6 +356,7 @@ export function signInsScreen(f: Session, ctx: Ctx): Screen { | ||
| 328 | 356 | keys: [ |
| 329 | 357 | binding(["j", "k"], "move cursor"), |
| 330 | 358 | binding(["d"], "sign a browser out or revoke a token"), |
| 359 | binding(["o"], "sign out all browsers, this key stays"), | |
| 331 | 360 | binding(["m"], "make an access code"), |
| 332 | 361 | binding(["p"], "change your password"), |
| 333 | 362 | ], |
| @@ -341,8 +370,8 @@ export function signInsScreen(f: Session, ctx: Ctx): Screen { | ||
| 341 | 370 | : f.user === undefined |
| 342 | 371 | ? "Sign in to see where you are signed in." |
| 343 | 372 | : f.user.kind === "agent" |
| 344 | ? "Where you are signed in. d signs a browser out or revokes a token." | |
| 345 | : "Where you are signed in. d signs a browser out or revokes a token, m makes an access code, p changes your password."; | |
| 373 | ? "Where you are signed in. d ends one, o signs out all browsers (this key stays)." | |
| 374 | : "Where you are signed in. d ends one, o signs out all browsers (this key stays), m makes an access code, p changes your password."; | |
| 346 | 375 | s.text(head!.x, head!.y, truncate(text, head!.w), style(c.mutedFg, c.bg), head!.x + head!.w); |
| 347 | 376 | drawList(s, body!, c, state, rows(all)); |
| 348 | 377 | }, |
| @@ -359,6 +388,10 @@ export function signInsScreen(f: Session, ctx: Ctx): Screen { | ||
| 359 | 388 | rotate(); |
| 360 | 389 | return true; |
| 361 | 390 | } |
| 391 | if (e.key === "o") { | |
| 392 | endBrowsers(all); | |
| 393 | return true; | |
| 394 | } | |
| 362 | 395 | if (e.key !== "d") return false; |
| 363 | 396 | const picked = all[state.index]; |
| 364 | 397 | if (picked === undefined) return false; |
src/web/pages/account.ts +22 −3
| @@ -41,7 +41,7 @@ import { | ||
| 41 | 41 | PROFILE_REFUSAL_FIELD, |
| 42 | 42 | PROFILE_REFUSALS, |
| 43 | 43 | } from "../../actions/profile_words.ts"; |
| 44 | import { endAccountSession } from "../../actions/signins.ts"; | |
| 44 | import { endAccountSession, signOutElsewhere } from "../../actions/signins.ts"; | |
| 45 | 45 | import { listSessions, type SessionInfo } from "../../core/sessions.ts"; |
| 46 | 46 | import { ACTS_AS_ADMIN_WORDS, ADMIN_TOKEN_WORDS, type TokenScope } from "../../core/tokens.ts"; |
| 47 | 47 | import { claudeInstall, opencodeInstall } from "../../core/plugin_install.ts"; |
| @@ -784,9 +784,12 @@ export function sessionsCard( | ||
| 784 | 784 | sessions: readonly SessionInfo[], |
| 785 | 785 | base: string, |
| 786 | 786 | ): Html { |
| 787 | // Only the person's own page, holding its current browser and others, offers signing the rest | |
| 788 | // out at once (#14559); the owner's page for someone has its own "End all sessions". | |
| 789 | const others = base === "/settings" && sessions.some((x) => x.current) && sessions.some((x) => !x.current); | |
| 787 | 790 | return block( |
| 788 | 791 | "Signed-in browsers", |
| 789 | sessions.length === 0 | |
| 792 | html`${sessions.length === 0 | |
| 790 | 793 | ? html`<p class="muted">Not signed in on any browser.</p>` |
| 791 | 794 | : html`<ul class="list setting-list"> |
| 792 | 795 | ${sessions.map((x) => |
| @@ -806,7 +809,9 @@ export function sessionsCard( | ||
| 806 | 809 | })}`, |
| 807 | 810 | }), |
| 808 | 811 | )} |
| 809 | </ul>`, | |
| 812 | </ul>`}${others | |
| 813 | ? html`<div class="form-actions">${confirmAction({ action: "/settings/sessions/end-others", verb: "Sign out everywhere else", size: "sm" })}</div>` | |
outdated
Design, principle 3 (space between things): |
||
| 814 | : null}`, | |
| 810 | 815 | ); |
| 811 | 816 | } |
| 812 | 817 | |
| @@ -991,6 +996,20 @@ export async function sessionDelete(ctx: Ctx): Promise<Response> { | ||
| 991 | 996 | }); |
| 992 | 997 | } |
| 993 | 998 | |
| 999 | // POST /settings/sessions/end-others | |
| 1000 | export async function sessionsOthersDelete(ctx: Ctx): Promise<Response> { | |
| 1001 | if (!ctx.auth.user) return signInFirst(ctx); | |
| 1002 | const form = await readForm(ctx, "admin"); | |
| 1003 | if (form instanceof Response) return form; | |
| 1004 | const done = signOutElsewhere(ctx.app, ctx.auth.user, cookieValue(ctx.request, "forge_session")); | |
| 1005 | if (!done.ok) | |
| 1006 | return flashTo(ctx, "/settings/tokens", { tone: "danger", message: "Only a signed-in browser can sign the others out." }); | |
| 1007 | const n = done.value.ended; | |
| 1008 | return flashTo(ctx, "/settings/tokens", { | |
| 1009 | message: n === 0 ? "No other browser was signed in." : `Signed out of ${n} other ${n === 1 ? "browser" : "browsers"}.`, | |
| 1010 | }); | |
| 1011 | } | |
| 1012 | ||
| 994 | 1013 | // ---- ssh keys ---- |
| 995 | 1014 | |
| 996 | 1015 | function keysView( |
src/web/pages/search.ts +9
| @@ -845,6 +845,15 @@ const DESTRUCTIVE: { | ||
| 845 | 845 | phrase: "", |
| 846 | 846 | }), |
| 847 | 847 | }, |
| 848 | { | |
| 849 | pattern: /^\/settings\/sessions\/end-others$/, | |
| 850 | describe: () => ({ | |
| 851 | verb: "Sign out everywhere else", | |
| 852 | title: "Sign out every other browser?", | |
| 853 | detail: "Every other browser signed in to this account is signed out at once and has to sign in again. This one stays signed in. App terminals and machine shells are not touched.", | |
| 854 | phrase: "", | |
| 855 | }), | |
| 856 | }, | |
| 848 | 857 | { |
| 849 | 858 | pattern: /^\/settings\/sessions\/([^/]+)\/delete$/, |
| 850 | 859 | describe: () => ({ |
test/account-sessions.test.ts added +140
| @@ -0,0 +1,140 @@ | ||
| 1 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 2 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 3 | import { tmpdir } from "node:os"; | |
| 4 | import { join } from "node:path"; | |
| 5 | import { signOutEveryBrowser } from "../src/actions/signins.ts"; | |
| 6 | import { createApp, type App } from "../src/app.ts"; | |
| 7 | import { listEvents } from "../src/core/events.ts"; | |
| 8 | import { createSession, endOtherSessions, listSessions } from "../src/core/sessions.ts"; | |
| 9 | import { createUser, type User } from "../src/core/users.ts"; | |
| 10 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 11 | ||
| 12 | // #14559: the account page lists the person's own browsers and signs every | |
| 13 | // other one out in one confirmed act, leaving this one and nobody else's. | |
| 14 | describe("sign out everywhere else", () => { | |
| 15 | let dataDir: string; | |
| 16 | let app: App; | |
| 17 | let servers: Servers; | |
| 18 | let base: string; | |
| 19 | let ada: User; | |
| 20 | let bo: User; | |
| 21 | ||
| 22 | const person = async (username: string) => { | |
| 23 | const made = await createUser(app.db, { | |
| 24 | origin: "seed", | |
| 25 | trusted: true, | |
| 26 | username, | |
| 27 | email: `${username}@f.dev`, | |
| 28 | password: "a-good-passphrase", | |
| 29 | }); | |
| 30 | if (!made.ok) throw new Error(made.error); | |
| 31 | return made.value; | |
| 32 | }; | |
| 33 | const tokenFor = (user: User) => createSession(app.db, user.id).token; | |
| 34 | const post = (path: string, token: string, origin = base) => | |
| 35 | fetch(`${base}${path}`, { | |
| 36 | method: "POST", | |
| 37 | headers: { cookie: `forge_session=${token}`, origin }, | |
| 38 | body: new URLSearchParams({}), | |
| 39 | redirect: "manual", | |
| 40 | }); | |
| 41 | const page = async (token: string) => | |
| 42 | (await fetch(`${base}/settings/tokens`, { headers: { cookie: `forge_session=${token}`, "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 14_0; rv:130.0) Gecko/20100101 Firefox/130.0" } })).text(); | |
| 43 | ||
| 44 | beforeAll(async () => { | |
| 45 | dataDir = await mkdtemp(join(tmpdir(), "forge-account-sessions-")); | |
| 46 | app = createApp({ dataDir, port: 0 }); | |
| 47 | servers = await serve(app); | |
| 48 | base = servers.url; | |
| 49 | await person("olive"); | |
| 50 | ada = await person("ada"); | |
| 51 | bo = await person("bo"); | |
| 52 | }); | |
| 53 | afterAll(async () => { | |
| 54 | servers.stop(); | |
| 55 | app.close(); | |
| 56 | await rm(dataDir, { recursive: true, force: true }); | |
| 57 | }); | |
| 58 | ||
| 59 | test("the list shows only the caller's browsers, this one marked, and never a token", async () => { | |
| 60 | const here = tokenFor(ada); | |
| 61 | const there = tokenFor(ada); | |
| 62 | const bos = tokenFor(bo); | |
| 63 | const html = await page(here); | |
| 64 | expect(html).toContain("Firefox on macOS"); | |
| 65 | expect(html).toContain("This browser"); | |
| 66 | expect(html).toContain("Sign out everywhere else"); | |
| 67 | expect(html).toContain(encodeURIComponent("/settings/sessions/end-others")); | |
| 68 | const others = listSessions(app.db, ada.id, here).filter((s) => !s.current); | |
| 69 | expect(others.length).toBe(1); | |
| 70 | expect(html).toContain(others[0]!.id); | |
| 71 | for (const s of listSessions(app.db, bo.id)) expect(html).not.toContain(s.id); | |
| 72 | for (const t of [here, there, bos]) { | |
| 73 | expect(html).not.toContain(t); | |
| 74 | expect(html).not.toContain(t.slice(0, 8)); | |
| 75 | } | |
| 76 | }); | |
| 77 | ||
| 78 | test("the confirm page words the act before it happens", async () => { | |
| 79 | const here = tokenFor(ada); | |
| 80 | const r = await fetch(`${base}/-/confirm?to=${encodeURIComponent("/settings/sessions/end-others")}`, { | |
| 81 | headers: { cookie: `forge_session=${here}` }, | |
| 82 | }); | |
| 83 | expect(r.status).toBe(200); | |
| 84 | const body = await r.text(); | |
| 85 | expect(body).toContain("Sign out every other browser?"); | |
| 86 | expect(body).toContain('action="/settings/sessions/end-others"'); | |
| 87 | }); | |
| 88 | ||
| 89 | test("a cross-origin POST is refused and ends nothing", async () => { | |
| 90 | const here = tokenFor(ada); | |
| 91 | tokenFor(ada); | |
| 92 | const before = listSessions(app.db, ada.id).length; | |
| 93 | const r = await post("/settings/sessions/end-others", here, "https://evil.example"); | |
| 94 | expect(r.status).toBe(403); | |
| 95 | expect(listSessions(app.db, ada.id).length).toBe(before); | |
| 96 | }); | |
| 97 | ||
| 98 | test("signing out elsewhere ends the others, keeps this one and others' accounts, and leaves a receipt", async () => { | |
| 99 | const here = tokenFor(ada); | |
| 100 | tokenFor(ada); | |
| 101 | tokenFor(ada); | |
| 102 | const bosBefore = listSessions(app.db, bo.id).length; | |
| 103 | expect(bosBefore).toBeGreaterThan(0); | |
| 104 | const r = await post("/settings/sessions/end-others", here); | |
| 105 | expect(r.status).toBe(303); | |
| 106 | const left = listSessions(app.db, ada.id, here); | |
| 107 | expect(left.length).toBe(1); | |
| 108 | expect(left[0]!.current).toBe(true); | |
| 109 | expect(listSessions(app.db, bo.id).length).toBe(bosBefore); | |
| 110 | const receipt = listEvents(app.db, { kind: "account.signed_out_elsewhere" }); | |
| 111 | expect(receipt.length).toBe(1); | |
| 112 | expect(receipt[0]!.actorId).toBe(ada.id); | |
| 113 | expect(receipt[0]!.payload["sessionsEnded"]).toBeGreaterThanOrEqual(3); | |
| 114 | // This browser is still signed in; with no others, the button is gone. | |
| 115 | const html = await page(here); | |
| 116 | expect(html).toContain("This browser"); | |
| 117 | expect(html).not.toContain("Sign out everywhere else"); | |
| 118 | // A replayed confirm with no other browser left ends nothing and writes no receipt. | |
| 119 | const again = await post("/settings/sessions/end-others", here); | |
| 120 | expect(again.status).toBe(303); | |
| 121 | expect(listSessions(app.db, ada.id, here).length).toBe(1); | |
| 122 | expect(listEvents(app.db, { kind: "account.signed_out_elsewhere" }).length).toBe(1); | |
| 123 | }); | |
| 124 | ||
| 125 | test("the terminal's door with no browser signed in writes no receipt", async () => { | |
| 126 | const cy = await person("cy"); | |
| 127 | const before = listEvents(app.db, { kind: "account.signed_out_elsewhere" }).length; | |
| 128 | const done = signOutEveryBrowser(app, cy); | |
| 129 | expect(done.ok && done.value.ended).toBe(0); | |
| 130 | expect(listEvents(app.db, { kind: "account.signed_out_elsewhere" }).length).toBe(before); | |
| 131 | }); | |
| 132 | ||
| 133 | test("a browser of another account ends nothing", () => { | |
| 134 | tokenFor(ada); | |
| 135 | const before = listSessions(app.db, ada.id).length; | |
| 136 | expect(endOtherSessions(app.db, ada.id, tokenFor(bo))).toEqual([]); | |
| 137 | expect(endOtherSessions(app.db, ada.id, "not-a-session")).toEqual([]); | |
| 138 | expect(listSessions(app.db, ada.id).length).toBe(before); | |
| 139 | }); | |
| 140 | }); |
test/tui-signins.test.ts +35
| @@ -175,6 +175,41 @@ describe("where you are signed in, in the terminal", () => { | ||
| 175 | 175 | g.stop(); |
| 176 | 176 | }); |
| 177 | 177 | |
| 178 | test("o with a confirm signs out every browser of that account and no one else's (#14559)", async () => { | |
| 179 | const made = await createUser(forge.db, { | |
| 180 | origin: "seed", | |
| 181 | trusted: true, | |
| 182 | username: "dave", | |
| 183 | email: "[email protected]", | |
| 184 | password: "a-good-passphrase", | |
| 185 | adminIfFirst: false, | |
| 186 | }); | |
| 187 | if (!made.ok) throw new Error(made.error); | |
| 188 | const daveId = getUserByName(forge.db, "dave")!.id; | |
| 189 | const phone = createSession(forge.db, daveId).token; | |
| 190 | const laptop = createSession(forge.db, daveId).token; | |
| 191 | const h = boot("dave"); | |
| 192 | await h.open(); | |
| 193 | expect(h.text()).toContain("o signs out all browsers (this key stays)"); | |
| 194 | await h.press("o"); | |
| 195 | expect(h.text()).toContain("Sign out every browser"); | |
| 196 | expect(h.text()).toContain("all 2 browsers"); | |
| 197 | // Keep is the default. | |
| 198 | await h.press("enter"); | |
| 199 | expect(sessionOf(forge.db, phone).ok && sessionOf(forge.db, laptop).ok).toBe(true); | |
| 200 | await h.press("o", "right", "enter"); | |
| 201 | expect(h.text()).toContain("Signed out of 2 browsers."); | |
| 202 | expect(sessionOf(forge.db, phone).ok || sessionOf(forge.db, laptop).ok).toBe(false); | |
| 203 | expect(sessionOf(forge.db, bobs).ok).toBe(true); | |
| 204 | const receipts = forge.db | |
| 205 | .query<{ n: number }, [string]>("SELECT COUNT(*) AS n FROM event WHERE kind = 'account.signed_out_elsewhere' AND actor_id = ?1") | |
| 206 | .get(daveId); | |
| 207 | expect(receipts?.n).toBe(1); | |
| 208 | await h.press("o"); | |
| 209 | expect(h.text()).toContain("No browser is signed in."); | |
| 210 | h.stop(); | |
| 211 | }); | |
| 212 | ||
| 178 | 213 | test("bob sees only his own", async () => { |
| 179 | 214 | const h = boot("bob"); |
| 180 | 215 | await h.open(); |
The
account.signed_out_elsewherereceipt is appended even whenendedis empty. The web path reaches this with no other browsers, for example a replayed confirm POST, and each press then writes an audit event saying 0 sessions ended. That pollutes the log and gives a signed-in user a free way to write events. Fix: append the receipt only whenended.length > 0, and add a test that a no-op writes none.