Public: anyone can read this; the crew can change it.
The MCP's OAuth consent says in words what the agent may do and offers read-only in one tap #15769
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15748-the-mcp-s-oauth-consent-says-in-words-wh → main 0 commits fork 3 threads · 3 unresolved
3 files
changed +129
−1
docs/features/oauth-consent.md +8
| @@ -12,3 +12,11 @@ The sentences live in one table, `SCOPE_WORDS` in `src/core/oauth.ts`, which | ||
| 12 | 12 | the page, and the Read/Read and act choices on it, read from. Scopes the forge does not offer are ignored, as before. |
| 13 | 13 | |
| 14 | 14 | The client's name is escaped wherever it is shown. |
| 15 | ||
| 16 | ## Read-only says what it refuses | |
| 17 | ||
| 18 | The read sentence in `SCOPE_WORDS` says that every tool that writes, tells, | |
| 19 | merges or deletes is refused. The "What it may do" choice starts on read-only | |
| 20 | unless the client asked for more, and Allow grants exactly the ticked choice; | |
| 21 | any other decision value is a denial. `/settings/grants` lists the grant as | |
| 22 | `ro` with a Revoke. |
src/core/oauth.ts +1 −1
| @@ -194,7 +194,7 @@ export const isOAuthScope = (v: string): v is OAuthScope => | ||
| 194 | 194 | * are what an OpenID sign-in tells the app about the person. |
| 195 | 195 | */ |
| 196 | 196 | export const SCOPE_WORDS: Readonly<Record<OAuthScope | OidcScope, string>> = { |
| 197 | ro: "Read: see your apps, issues and runs", | |
| 197 | ro: "Read: see your apps, issues and runs; every tool that writes, tells, merges or deletes is refused", | |
| 198 | 198 | rw: "Read and act: tell the crew, plan, and change things as you", |
| 199 | 199 | openid: "who you are on this forge (an identifier that stays the same)", |
| 200 | 200 | profile: "your username and name", |
test/oauth-consent.test.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | /** | |
| 2 | * What the consent page grants: the chosen scope is the token's scope, an MCP writing tool refuses | |
| 3 | * a read-only token, the grants page lists it with a revoke, and any decision but "allow" is a | |
| 4 | * denial. | |
| 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 { createApp, type App } from "../src/app.ts"; | |
| 11 | import { SCOPE_WORDS } from "../src/core/oauth.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { createUser } from "../src/core/users.ts"; | |
| 14 | import { READ_ONLY_REFUSAL } from "../src/http/mcp/hints.ts"; | |
| 15 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 16 | import { mcpTool } from "./helpers/mcp.ts"; | |
| 17 | ||
| 18 | const REDIRECT = "https://claude.ai/api/mcp/auth_callback"; | |
| 19 | const verifier = "v".repeat(60); | |
| 20 | const challenge = new Bun.CryptoHasher("sha256").update(verifier).digest("base64url"); | |
| 21 | let dir = ""; | |
| 22 | let app: App; | |
| 23 | let servers: Servers; | |
| 24 | let cookie = ""; | |
| 25 | let clientId = ""; | |
| 26 | ||
| 27 | interface Registered { | |
| 28 | client_id: string; | |
| 29 | } | |
| 30 | interface Issued { | |
| 31 | scope: string; | |
| 32 | access_token: string; | |
| 33 | } | |
| 34 | ||
| 35 | beforeAll(async () => { | |
| 36 | dir = await mkdtemp(join(tmpdir(), "forge-oauth-consent-")); | |
| 37 | app = createApp({ dataDir: dir, port: 0 }); | |
| 38 | servers = await serve(app); | |
| 39 | const made = await createUser(app.db, { | |
| 40 | origin: "seed", | |
| 41 | trusted: true, | |
| 42 | username: "alice", | |
| 43 | email: "[email protected]", | |
| 44 | password: "a-good-passphrase", | |
| 45 | adminIfFirst: true, | |
| 46 | }); | |
| 47 | if (!made.ok) throw new Error(made.error); | |
| 48 | cookie = `forge_session=${createSession(app.db, made.value.id).token}`; | |
| 49 | const registered = await fetch(`${servers.url}/oauth/register`, { | |
| 50 | method: "POST", | |
| 51 | headers: { "content-type": "application/json" }, | |
| 52 | body: JSON.stringify({ client_name: "Car Agent", redirect_uris: [REDIRECT] }), | |
| 53 | }); | |
| 54 | clientId = ((await registered.json()) as Registered).client_id; | |
| 55 | }); | |
| 56 | afterAll(async () => { | |
| 57 | servers.stop(); | |
| 58 | await rm(dir, { recursive: true, force: true }); | |
| 59 | }); | |
| 60 | ||
| 61 | const params = { | |
| 62 | response_type: "code", | |
| 63 | redirect_uri: REDIRECT, | |
| 64 | code_challenge: challenge, | |
| 65 | code_challenge_method: "S256", | |
| 66 | state: "s", | |
| 67 | scope: "ro", | |
| 68 | }; | |
| 69 | ||
| 70 | const submit = (fields: Record<string, string>) => | |
| 71 | fetch(`${servers.url}/oauth/authorize`, { | |
| 72 | method: "POST", | |
| 73 | redirect: "manual", | |
| 74 | headers: { cookie, origin: servers.url }, | |
| 75 | body: new URLSearchParams({ ...params, client_id: clientId, ...fields }), | |
| 76 | }); | |
| 77 | ||
| 78 | test("the read sentence says what read-only refuses, once, with no second explanation", async () => { | |
| 79 | expect(SCOPE_WORDS.ro).toContain("every tool that writes, tells, merges or deletes is refused"); | |
| 80 | const query = new URLSearchParams({ ...params, client_id: clientId }); | |
| 81 | const page = await fetch(`${servers.url}/oauth/authorize?${query}`, { headers: { cookie } }); | |
| 82 | const body = (await page.text()).replace(/\s+/g, " "); | |
| 83 | expect(body).toContain("every tool that writes, tells, merges or deletes is refused"); | |
| 84 | expect(body.split("merges or deletes").length).toBe(2); | |
| 85 | expect(body).toMatch(/value="ro"\s+checked/); | |
| 86 | }); | |
| 87 | ||
| 88 | test("an ro choice issues an ro token, a write tool refuses it, and the grant is listed with a revoke", async () => { | |
| 89 | const res = await submit({ decision: "allow", grant: "ro", name: "car", lifetime: "never" }); | |
| 90 | expect(res.status).toBe(303); | |
| 91 | const code = new URL(res.headers.get("location")!).searchParams.get("code")!; | |
| 92 | const issued = await fetch(`${servers.url}/oauth/token`, { | |
| 93 | method: "POST", | |
| 94 | body: new URLSearchParams({ | |
| 95 | grant_type: "authorization_code", | |
| 96 | client_id: clientId, | |
| 97 | redirect_uri: REDIRECT, | |
| 98 | code_verifier: verifier, | |
| 99 | code, | |
| 100 | }), | |
| 101 | }); | |
| 102 | const token = (await issued.json()) as Issued; | |
| 103 | expect(token.scope).toBe("ro"); | |
| 104 | const refused = await mcpTool(servers.url, token.access_token, "tell", { text: "hello", dryRun: false }); | |
| 105 | expect(refused.error).toBe(READ_ONLY_REFUSAL); | |
| 106 | const grants = await (await fetch(`${servers.url}/settings/grants`, { headers: { cookie } })).text(); | |
| 107 | expect(grants).toContain("Car Agent"); | |
| 108 | expect(grants).toMatch(/>ro</); | |
| 109 | expect(grants).toContain("Revoke"); | |
| 110 | }); | |
| 111 | ||
| 112 | test("a decision that is not allow is denied, never granted", async () => { | |
| 113 | for (const decision of ["bogus", "allow-ro", "", "ALLOW"]) { | |
| 114 | const res = await submit({ decision, grant: "rw", name: "car", lifetime: "never" }); | |
| 115 | expect(res.status).toBe(303); | |
| 116 | const back = new URL(res.headers.get("location")!); | |
| 117 | expect(back.searchParams.get("error")).toBe("access_denied"); | |
| 118 | expect(back.searchParams.get("code")).toBeNull(); | |
| 119 | } | |
| 120 | }); |
new above
offline
Formatting and
as anycasts: type the JSON and reuse existing test helpers (similarity 0.94 with apps-template-doors). Add a case that decision=bogus redirects access_denied.