|
1 | 1 | import * as NodeCrypto from "node:crypto"; |
2 | 2 |
|
3 | 3 | 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"; |
5 | 9 | import { expect, it } from "@effect/vitest"; |
6 | 10 | import * as Context from "effect/Context"; |
7 | 11 | import * as Crypto from "effect/Crypto"; |
8 | 12 | import * as Effect from "effect/Effect"; |
9 | 13 | import * as Layer from "effect/Layer"; |
| 14 | +import * as Schema from "effect/Schema"; |
10 | 15 | import * as Etag from "effect/unstable/http/Etag"; |
11 | 16 | import * as HttpPlatform from "effect/unstable/http/HttpPlatform"; |
12 | 17 | import * as HttpRouter from "effect/unstable/http/HttpRouter"; |
@@ -55,6 +60,7 @@ const makeRoutesLayer = (capture: (auth: EnvironmentAuth.EnvironmentAuth["Servic |
55 | 60 | Layer.provide(NodeServices.layer), |
56 | 61 | ); |
57 | 62 |
|
| 63 | +const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown)); |
58 | 64 | const ORIGIN = "https://box.example.ts.net"; |
59 | 65 | const REDIRECT = "http://localhost/callback"; |
60 | 66 | const verifier = "a".repeat(43) + "-verifier-for-tests"; |
@@ -105,7 +111,7 @@ const register = (handler: Handler, redirect = REDIRECT) => |
105 | 111 | at("/oauth/mcp/register", { |
106 | 112 | method: "POST", |
107 | 113 | headers: { "content-type": "application/json" }, |
108 | | - body: JSON.stringify({ |
| 114 | + body: encodeJson({ |
109 | 115 | client_name: "Claude Code", |
110 | 116 | redirect_uris: [redirect], |
111 | 117 | 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 |
358 | 364 | ), |
359 | 365 | ); |
360 | 366 |
|
| 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 | + |
361 | 411 | it("matches loopback redirects on everything but the port", () => { |
362 | 412 | expect( |
363 | 413 | McpOAuth.loopbackRedirectMatches("http://localhost/callback", "http://localhost:9/callback"), |
|
0 commit comments