fix(opencode): preserve valid MCP OAuth state

This commit is contained in:
Aiden Cline
2026-06-24 16:59:34 -05:00
parent ed486f0b49
commit bb388efae6
6 changed files with 25 additions and 25 deletions
+2 -3
View File
@@ -147,11 +147,10 @@ export const layer = Layer.effect(
return yield* Effect.gen(function* () {
const data = yield* read()
const entry = data[mcpName]
if (!entry?.oauthState) return false
const matches = entry.oauthState === oauthState
if (entry?.oauthState !== oauthState) return false
delete entry.oauthState
yield* fs.writeJson(filepath, { ...data, [mcpName]: entry }, 0o600).pipe(Effect.orDie)
return matches
return true
}).pipe(flock.withLock(lockKey), Effect.orDie)
})
+6 -4
View File
@@ -951,14 +951,16 @@ export const layer = Layer.effect(
oauthState: string,
) {
yield* requireMcpConfig(mcpName)
const transport = pendingOAuthTransports.get(mcpName)
if (!transport) return yield* new OAuthError({ message: `No pending OAuth flow for MCP server: ${mcpName}` })
if (!(yield* auth.consumeOAuthState(mcpName, oauthState))) {
yield* cleanupAuth(mcpName)
return yield* new OAuthError({ message: "Invalid or expired OAuth state - potential CSRF attack" })
}
const transport = pendingOAuthTransports.get(mcpName)
if (!transport) {
yield* cleanupAuth(mcpName)
return yield* new OAuthError({ message: `No pending OAuth flow for MCP server: ${mcpName}` })
}
const result = yield* Effect.tryPromise({
try: () => transport.finishAuth(authorizationCode).then(() => true as const),
catch: (error) => {
@@ -88,7 +88,7 @@ export const McpApi = HttpApi.make("mcp")
identifier: "mcp.auth.callback",
summary: "Complete MCP OAuth",
description:
"Complete OAuth authentication for a Model Context Protocol (MCP) server using the authorization code.",
"Complete OAuth authentication for a Model Context Protocol (MCP) server using the authorization code and state returned by the start endpoint.",
}),
),
HttpApiEndpoint.post("authAuthenticate", McpPaths.authAuthenticate, {