Skip to content

Commit 9630793

Browse files
fix: a superseded sign-in start never overwrites a newer flow's OAuth state
`beginAuth` stores its state only while it still owns the server, checked inside the auth file lock (`McpAuth.updateOAuthState(name, state, current)`), and stops before connecting when a later call has claimed the server. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0172qrhMa5TQgETASi5hxMqD
1 parent 21e02dc commit 9630793

4 files changed

Lines changed: 88 additions & 4 deletions

File tree

‎packages/opencode/src/mcp/auth.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ export interface Interface {
4949
readonly updateClientInfo: (mcpName: string, clientInfo: ClientInfo, serverUrl?: string) => Effect.Effect<void>
5050
readonly updateCodeVerifier: (mcpName: string, codeVerifier: string) => Effect.Effect<void>
5151
readonly clearCodeVerifier: (mcpName: string) => Effect.Effect<void>
52-
readonly updateOAuthState: (mcpName: string, oauthState: string) => Effect.Effect<void>
52+
// altimate_change start — with `current`, stores the state only while it still holds, checked inside the lock
53+
readonly updateOAuthState: (mcpName: string, oauthState: string, current?: () => boolean) => Effect.Effect<void>
54+
// altimate_change end
5355
readonly getOAuthState: (mcpName: string) => Effect.Effect<string | undefined>
5456
// altimate_change start — with `expected`, clears only while the stored state is still that one
5557
readonly clearOAuthState: (mcpName: string, expected?: string) => Effect.Effect<void>
@@ -140,7 +142,20 @@ export const layer = Layer.effect(
140142
const updateTokens = updateField("tokens", "updateTokens")
141143
const updateClientInfo = updateField("clientInfo", "updateClientInfo")
142144
const updateCodeVerifier = updateField("codeVerifier", "updateCodeVerifier")
143-
const updateOAuthState = updateField("oauthState", "updateOAuthState")
145+
// altimate_change start — see Interface.updateOAuthState
146+
const updateOAuthState = Effect.fn("McpAuth.updateOAuthState")(function* (
147+
mcpName: string,
148+
oauthState: string,
149+
current?: () => boolean,
150+
) {
151+
yield* mutate((data) => {
152+
if (current && !current()) return undefined
153+
const entry = data[mcpName] ?? {}
154+
entry.oauthState = oauthState
155+
return { ...data, [mcpName]: entry }
156+
})
157+
})
158+
// altimate_change end
144159
const clearCodeVerifier = clearField("codeVerifier", "clearCodeVerifier")
145160
// altimate_change start — compare-and-clear inside the lock: a newer flow for the same server
146161
// may have stored its own state while this caller waited for the lock

‎packages/opencode/src/mcp/index.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,15 @@ export const layer = Layer.effect(
13901390
const oauthState = Array.from(crypto.getRandomValues(new Uint8Array(32)))
13911391
.map((b) => b.toString(16).padStart(2, "0"))
13921392
.join("")
1393-
yield* auth.updateOAuthState(mcpName, oauthState)
1393+
// altimate_change start — the state is stored only while this call still owns the server,
1394+
// checked inside the auth file lock, so a superseded call never overwrites a newer flow's
1395+
// state. A call superseded before it connects stops here.
1396+
yield* auth.updateOAuthState(mcpName, oauthState, () => isCurrent(lifecycle, mcpName, token))
1397+
if (!isCurrent(lifecycle, mcpName, token)) {
1398+
yield* auth.clearOAuthState(mcpName, oauthState)
1399+
return { authorizationUrl: "", oauthState } satisfies AuthResult
1400+
}
1401+
// altimate_change end
13941402
let capturedUrl: URL | undefined
13951403
const authProvider = new McpOAuthProvider(
13961404
mcpName,

‎packages/opencode/test/mcp/auth.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ test("clearOAuthState with an expected state leaves a newer state in place", asy
100100
})
101101
// altimate_change end
102102

103-
// altimate_change start — a sign-out that a later call superseded removes nothing (codex)
103+
// altimate_change start — a superseded sign-out removes nothing, a superseded sign-in start stores nothing
104104
test("remove with a condition removes only while it still holds", async () => {
105105
const file = authFile()
106106

@@ -117,5 +117,22 @@ test("remove with a condition removes only while it still holds", async () => {
117117
}),
118118
)
119119
})
120+
121+
test("updateOAuthState with a condition stores only while it still holds", async () => {
122+
const file = authFile()
123+
124+
await Effect.runPromise(
125+
Effect.gen(function* () {
126+
const auth = yield* authService(file.layer)
127+
yield* auth.updateOAuthState("server", "newer")
128+
129+
yield* auth.updateOAuthState("server", "older", () => false)
130+
expect(yield* auth.getOAuthState("server")).toBe("newer")
131+
132+
yield* auth.updateOAuthState("server", "latest", () => true)
133+
expect(yield* auth.getOAuthState("server")).toBe("latest")
134+
}),
135+
)
136+
})
120137
// altimate_change end
121138

‎packages/opencode/test/mcp/oauth-auto-connect.test.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ const { McpAuth } = await import("../../src/mcp/auth")
172172
const { McpOAuthProvider } = await import("../../src/mcp/oauth-provider")
173173
const { FSUtil } = await import("@opencode-ai/core/fs-util")
174174
const { CrossSpawnSpawner } = await import("@opencode-ai/core/cross-spawn-spawner")
175+
// altimate_change start — the callback server, held to order two sign-in starts
176+
const { McpOAuthCallback } = await import("../../src/mcp/oauth-callback")
177+
const { spyOn } = await import("bun:test")
178+
// altimate_change end
175179

176180
const mcpTest = testEffect(
177181
Layer.mergeAll(
@@ -539,4 +543,44 @@ mcpTest.instance(
539543
}),
540544
{ config: config("test-oauth-superseded-start") },
541545
)
546+
547+
mcpTest.instance(
548+
"an older startAuth that resumes after a newer one leaves the newer flow's state and flow",
549+
() =>
550+
Effect.gen(function* () {
551+
const mcp = yield* MCP.Service
552+
const auth = yield* McpAuth.Service
553+
const name = "test-oauth-older-start"
554+
yield* mcp.add(name, { type: "remote", url: "https://example.com/mcp" })
555+
556+
// Hold the older call before it stores its state; the newer call runs to completion.
557+
let taken!: () => void
558+
const wasTaken = new Promise<void>((resolve) => (taken = resolve))
559+
let release!: () => void
560+
const held = new Promise<void>((resolve) => (release = resolve))
561+
const ensureRunning = McpOAuthCallback.ensureRunning
562+
const spy = spyOn(McpOAuthCallback, "ensureRunning").mockImplementationOnce(async (uri) => {
563+
taken()
564+
await held
565+
return ensureRunning(uri)
566+
})
567+
try {
568+
const older = yield* Effect.forkChild(mcp.startAuth(name))
569+
yield* Effect.promise(() => wasTaken)
570+
const newer = yield* mcp.startAuth(name)
571+
expect(newer.authorizationUrl).not.toBe("")
572+
release()
573+
const olderResult = yield* Fiber.join(older)
574+
575+
expect(olderResult.authorizationUrl).toBe("")
576+
expect(yield* auth.getOAuthState(name)).toBe(newer.oauthState)
577+
connectSucceedsImmediately = true
578+
expect((yield* mcp.finishAuth(name, "code")).status).toBe("connected")
579+
expect(tokenExchanges).toBe(1)
580+
} finally {
581+
spy.mockRestore()
582+
}
583+
}),
584+
{ config: config("test-oauth-older-start") },
585+
)
542586
// altimate_change end

0 commit comments

Comments
 (0)