Skip to content

Commit 53155c7

Browse files
fix(server): one-click MCP approval needs the scopes it grants
The approval page offered one-click to any browser session with access:write, letting an access-only session hand an agent thread control it does not hold. It now also requires orchestration:read and orchestration:operate, matching the pairing-code path. Page errors now show their description instead of an empty alert. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent f33e340 commit 53155c7

2 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎apps/server/src/auth/McpOAuth.test.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import * as NodeCrypto from "node:crypto";
22

33
import * as NodeServices from "@effect/platform-node/NodeServices";
4-
import { EnvironmentHttpApi } from "@t3tools/contracts";
4+
import {
5+
AuthAdministrativeScopes,
6+
type AuthEnvironmentScope,
7+
EnvironmentHttpApi,
8+
} from "@t3tools/contracts";
59
import { expect, it } from "@effect/vitest";
610
import * as Context from "effect/Context";
711
import * as Crypto from "effect/Crypto";
812
import * as Effect from "effect/Effect";
913
import * as Layer from "effect/Layer";
14+
import * as Schema from "effect/Schema";
1015
import * as Etag from "effect/unstable/http/Etag";
1116
import * as HttpPlatform from "effect/unstable/http/HttpPlatform";
1217
import * as HttpRouter from "effect/unstable/http/HttpRouter";
@@ -55,6 +60,7 @@ const makeRoutesLayer = (capture: (auth: EnvironmentAuth.EnvironmentAuth["Servic
5560
Layer.provide(NodeServices.layer),
5661
);
5762

63+
const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
5864
const ORIGIN = "https://box.example.ts.net";
5965
const REDIRECT = "http://localhost/callback";
6066
const verifier = "a".repeat(43) + "-verifier-for-tests";
@@ -105,7 +111,7 @@ const register = (handler: Handler, redirect = REDIRECT) =>
105111
at("/oauth/mcp/register", {
106112
method: "POST",
107113
headers: { "content-type": "application/json" },
108-
body: JSON.stringify({
114+
body: encodeJson({
109115
client_name: "Claude Code",
110116
redirect_uris: [redirect],
111117
grant_types: ["authorization_code", "refresh_token"],
@@ -358,6 +364,50 @@ it.live("denies and refuses codes bound to another client's key or without threa
358364
),
359365
);
360366

367+
it.live("offers one-click only to a browser session that holds the scopes it would grant", () =>
368+
withRoutes((handler, auth) =>
369+
Effect.gen(function* () {
370+
const clientId = yield* registeredClientId(handler);
371+
const params = authorizeParams(clientId);
372+
// Signs a browser in through the real route and returns its session cookie.
373+
const browserCookie = (scopes: ReadonlyArray<AuthEnvironmentScope>) =>
374+
Effect.gen(function* () {
375+
const pairing = yield* auth.issuePairingCredential({ scopes });
376+
const response = yield* handler(
377+
at("/api/auth/browser-session", {
378+
method: "POST",
379+
headers: { "content-type": "application/json" },
380+
body: encodeJson({ credential: pairing.credential }),
381+
}),
382+
);
383+
return response.headers.getSetCookie()[0]!.split(";", 1)[0]!;
384+
});
385+
const page = (cookie: string) =>
386+
handler(
387+
at(`/oauth/mcp/authorize?${new URLSearchParams(params)}`, { headers: { cookie } }),
388+
).pipe(Effect.flatMap(text));
389+
390+
const admin = yield* browserCookie([...AuthAdministrativeScopes]);
391+
expect(yield* page(admin)).toContain('name="csrf_token"');
392+
393+
// access:write alone cannot hand an agent thread control it does not hold.
394+
const accessOnly = yield* browserCookie(["access:read", "access:write"]);
395+
expect(yield* page(accessOnly)).not.toContain('name="csrf_token"');
396+
const forged = yield* handler(
397+
at("/oauth/mcp/authorize", {
398+
...form({ ...params, decision: "approve", runtime_mode: "auto", csrf_token: "forged" }),
399+
headers: {
400+
"content-type": "application/x-www-form-urlencoded",
401+
cookie: accessOnly,
402+
},
403+
}),
404+
);
405+
expect(forged.status).toBe(400);
406+
expect(yield* text(forged)).toContain("Enter a pairing code instead");
407+
}),
408+
),
409+
);
410+
361411
it("matches loopback redirects on everything but the port", () => {
362412
expect(
363413
McpOAuth.loopbackRedirectMatches("http://localhost/callback", "http://localhost:9/callback"),

‎apps/server/src/auth/McpOAuth.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ export type McpOAuthError =
155155
export class McpOAuthPageError extends Schema.TaggedError<McpOAuthPageError>()(
156156
"McpOAuthPageError",
157157
{ description: Schema.String },
158-
) {}
158+
) {
159+
override get message(): string {
160+
return this.description;
161+
}
162+
}
159163

160164
/** Problems reported back to the client through its (validated) redirect URI. */
161165
export class McpOAuthRedirectError extends Schema.TaggedError<McpOAuthRedirectError>()(
@@ -409,7 +413,9 @@ const make = Effect.gen(function* () {
409413
) =>
410414
environmentAuth.authenticateBrowserSession(request).pipe(
411415
Effect.map((session) =>
412-
session.scopes.includes(AuthAccessWriteScope)
416+
// Approving manages access, and a session may only hand out scopes it holds.
417+
session.scopes.includes(AuthAccessWriteScope) &&
418+
EnvironmentAuth.MCP_CLIENT_SCOPES.every((scope) => session.scopes.includes(scope))
413419
? { csrfToken: csrfToken(session.sessionId, authorization) }
414420
: undefined,
415421
),

0 commit comments

Comments
 (0)