Public: anyone can read this; the crew can change it.
Suspending or dropping an account ends its OAuth refresh tokens and codes #14996
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14960-suspending-or-dropping-an-account-ends-i → main 1 commit fork
2 files
changed +117
−3
src/core/accounts.ts +7 −3
| @@ -149,10 +149,14 @@ export function setSuspended( | ||
| 149 | 149 | forgetListings(db); |
| 150 | 150 | } |
| 151 | 151 | |
| 152 | /** Every way the account could prove it is itself, gone. */ | |
| 152 | /** | |
| 153 | * Every way the account could prove it is itself, gone: sessions, tokens, and | |
| 154 | * the OAuth refresh tokens and unexchanged codes that would mint new tokens | |
| 155 | * once the account is restored. | |
| 156 | */ | |
| 153 | 157 | export function dropCredentials(db: Database, userId: string): void { |
| 154 | db.query("DELETE FROM session WHERE user_id = ?1").run(userId); | |
| 155 | db.query("DELETE FROM token WHERE user_id = ?1").run(userId); | |
| 158 | for (const table of ["session", "token", "oauth_refresh", "oauth_code"]) | |
| 159 | db.query(`DELETE FROM ${table} WHERE user_id = ?1`).run(userId); | |
| 156 | 160 | } |
| 157 | 161 | |
| 158 | 162 | /** |
test/oauth-suspend-ends-refresh.test.ts added +110
| @@ -0,0 +1,110 @@ | ||
| 1 | /** | |
| 2 | * Suspending an account ends its OAuth refresh tokens and unexchanged codes | |
| 3 | * (#14960): restoring the account does not bring them back. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { suspendAccount, unsuspendAccount } from "../src/actions/accounts.ts"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { dropCredentials } from "../src/core/accounts.ts"; | |
| 12 | import { createClient, issueCode, parseAuthRequest, type OAuthClient } from "../src/core/oauth.ts"; | |
| 13 | import { createUser, type User } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | const REDIRECT = "https://claude.ai/api/mcp/auth_callback"; | |
| 20 | const verifier = "v".repeat(60); | |
| 21 | const challenge = new Bun.CryptoHasher("sha256").update(verifier).digest("base64url"); | |
| 22 | ||
| 23 | describe("suspending an account ends its OAuth grants", () => { | |
| 24 | let dataDir: string; | |
| 25 | let app: App; | |
| 26 | let servers: Servers; | |
| 27 | let admin: User; | |
| 28 | let bob: User; | |
| 29 | let client: OAuthClient; | |
| 30 | ||
| 31 | beforeAll(async () => { | |
| 32 | dataDir = await mkdtemp(join(tmpdir(), "forge-oauth-suspend-")); | |
| 33 | app = createApp({ dataDir, port: 0 }); | |
| 34 | servers = await serve(app); | |
| 35 | const users: User[] = []; | |
| 36 | for (const [username, isAdmin] of [["alice", true], ["bob", false]] as const) { | |
| 37 | const made = await createUser(app.db, { | |
| 38 | origin: "seed", | |
| 39 | trusted: true, | |
| 40 | username, | |
| 41 | email: `${username}@forge.local`, | |
| 42 | password: "a-good-passphrase", | |
| 43 | isAdmin, | |
| 44 | }); | |
| 45 | if (!made.ok) throw new Error(made.error); | |
| 46 | users.push(made.value); | |
| 47 | } | |
| 48 | [admin, bob] = users as [User, User]; | |
| 49 | client = createClient(app.db, { name: "Fake", redirectUris: [REDIRECT] }); | |
| 50 | }); | |
| 51 | afterAll(async () => { | |
| 52 | servers.stop(); | |
| 53 | app.close(); | |
| 54 | await rm(dataDir, { recursive: true, force: true }); | |
| 55 | }); | |
| 56 | ||
| 57 | const post = async (params: Record<string, string>) => { | |
| 58 | const res = await fetch(`${servers.url}/oauth/token`, { | |
| 59 | method: "POST", | |
| 60 | headers: { "content-type": "application/x-www-form-urlencoded" }, | |
| 61 | body: new URLSearchParams(params), | |
| 62 | }); | |
| 63 | return { status: res.status, body: (await res.json()) as Record<string, unknown> }; | |
| 64 | }; | |
| 65 | const code = (userId: string) => { | |
| 66 | const params: Record<string, string> = { | |
| 67 | client_id: client.id, | |
| 68 | redirect_uri: REDIRECT, | |
| 69 | response_type: "code", | |
| 70 | code_challenge_method: "S256", | |
| 71 | code_challenge: challenge, | |
| 72 | scope: "rw", | |
| 73 | }; | |
| 74 | const request = parseAuthRequest(app.db, (n) => params[n] ?? ""); | |
| 75 | if (!request.ok) throw new Error(request.error); | |
| 76 | return issueCode(app.db, userId, request.value, { scope: "rw", name: "Fake", lifetimeS: 86_400 }); | |
| 77 | }; | |
| 78 | const exchange = (c: string) => | |
| 79 | post({ grant_type: "authorization_code", code: c, client_id: client.id, redirect_uri: REDIRECT, code_verifier: verifier }); | |
| 80 | const refresh = (token: unknown) => | |
| 81 | post({ grant_type: "refresh_token", refresh_token: String(token), client_id: client.id }); | |
| 82 | const rows = (table: string, userId: string) => | |
| 83 | app.db.query<{ n: number }, [string]>(`SELECT COUNT(*) AS n FROM ${table} WHERE user_id = ?1`).get(userId)?.n; | |
| 84 | ||
| 85 | test("a refresh token held across suspend and unsuspend is refused", async () => { | |
| 86 | const granted = await exchange(code(bob.id)); | |
| 87 | expect(granted.status).toBe(200); | |
| 88 | const pending = code(bob.id); | |
| 89 | expect((await suspendAccount(app, admin, "bob")).ok).toBe(true); | |
| 90 | expect(unsuspendAccount(app, admin, "bob").ok).toBe(true); | |
| 91 | const again = await refresh(granted.body["refresh_token"]); | |
| 92 | expect(again.status).toBe(400); | |
| 93 | expect(again.body["error"]).toBe("invalid_grant"); | |
| 94 | expect((await exchange(pending)).body["error"]).toBe("invalid_grant"); | |
| 95 | }); | |
| 96 | ||
| 97 | test("dropCredentials deletes the account's refresh and code rows, and only its own", async () => { | |
| 98 | await exchange(code(bob.id)); | |
| 99 | code(bob.id); | |
| 100 | await exchange(code(admin.id)); | |
| 101 | code(admin.id); | |
| 102 | expect(rows("oauth_refresh", bob.id)).toBeGreaterThan(0); | |
| 103 | expect(rows("oauth_code", bob.id)).toBeGreaterThan(0); | |
| 104 | dropCredentials(app.db, bob.id); | |
| 105 | expect(rows("oauth_refresh", bob.id)).toBe(0); | |
| 106 | expect(rows("oauth_code", bob.id)).toBe(0); | |
| 107 | expect(rows("oauth_refresh", admin.id)).toBeGreaterThan(0); | |
| 108 | expect(rows("oauth_code", admin.id)).toBeGreaterThan(0); | |
| 109 | }); | |
| 110 | }); |
new above
offline