Skip to content

Commit 309522f

Browse files
fix(core): keep MCP OAuth login waiting after stray callback requests (anomalyco#58342)
Co-authored-by: Reynold Hu <88558425+reynold-hu@users.noreply.github.com>
1 parent e49157b commit 309522f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

‎packages/core/src/mcp/oauth.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,17 @@ export const authorize = (input: {
373373
}
374374
const error = url.searchParams.get("error_description") ?? url.searchParams.get("error")
375375
if (error) return fail(error, "authorization_error")
376-
if (url.searchParams.get("state") !== state) return fail("OAuth state mismatch", "state_mismatch")
376+
const returned = url.searchParams.get("state")
377377
const value = url.searchParams.get("code")
378+
// Browsers can reach the callback without any OAuth parameters (preflights, prefetches, reloads). Only a
379+
// redirect from the authorization server may end the login, so keep waiting for it.
380+
if (returned === null && !value) {
381+
response
382+
.writeHead(400, { "Content-Type": "text/html" })
383+
.end(OauthCallbackPage.error("Missing authorization code", { provider: input.name }))
384+
return
385+
}
386+
if (returned !== state) return fail("OAuth state mismatch", "state_mismatch")
378387
if (!value) return fail("Missing authorization code", "missing_code")
379388
// The page waits for the token exchange so the browser never reports success for a rejected code.
380389
if (

‎packages/core/test/mcp-oauth.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,37 @@ describe("MCP OAuth", () => {
160160
expect(tokenRequests[0]?.get("resource")).toBe(server.url.href)
161161
})
162162

163+
test("keeps waiting for the redirect after callback requests without OAuth parameters", async () => {
164+
const server = Bun.serve({
165+
port: 0,
166+
async fetch(request) {
167+
if (request.method !== "POST" || new URL(request.url).pathname !== "/token")
168+
return new Response(null, { status: 404 })
169+
return Response.json({ access_token: "access", token_type: "Bearer", refresh_token: "refresh", expires_in: 3600 })
170+
},
171+
})
172+
173+
const credential = await Effect.runPromise(
174+
Effect.scoped(
175+
Effect.gen(function* () {
176+
const { authorization, url: authorizationUrl } = yield* start(server, { client_id: "client" })
177+
const redirectValue = authorizationUrl.searchParams.get("redirect_uri")
178+
const state = authorizationUrl.searchParams.get("state")
179+
if (!redirectValue || !state) throw new Error("Missing OAuth redirect parameters")
180+
const callback = new URL(redirectValue)
181+
expect((yield* Effect.promise(() => fetch(callback, { method: "OPTIONS" }))).status).toBe(400)
182+
expect((yield* Effect.promise(() => fetch(callback))).status).toBe(400)
183+
callback.searchParams.set("code", "accepted")
184+
callback.searchParams.set("state", state)
185+
expect((yield* Effect.promise(() => fetch(callback))).status).toBe(200)
186+
return yield* authorization.callback
187+
}),
188+
),
189+
).finally(() => server.stop(true))
190+
191+
expect(credential.access).toBe("access")
192+
})
193+
163194
test("reports a rejected token exchange on the callback page", async () => {
164195
const server = Bun.serve({
165196
port: 0,

0 commit comments

Comments
 (0)