Some providers (PostHog, etc.) emit id_tokens on plain OAuth 2.0 token responses even when the client never requested `openid`. oauth4webapi then strict-validates the id_token against AS metadata, rejecting tokens whose access_token portion is valid. We never consume the id_token, so strip it before delegation. Removes the alg-mismatch tolerance which is now subsumed.
Unified · Split
| @@ -201,12 +201,13 @@ describe("exchangeAuthorizationCode", () => { · 200 unmodified lines | ||
| 201 | 201 | expect(body.has("client_secret")).toBe(false); |
| 202 | 202 | }); |
| 203 | 203 | |
| 204 | − it("validates ID tokens against an explicit issuer when token host differs", async () => { | |
| 204 | + it("strips id_tokens whose iss does not match AS metadata (PostHog-style OIDC backend behind plain OAuth 2.0 metadata)", async () => { | |
| 205 | 205 | captureFetch( |
| 206 | 206 | jsonResponse(200, { |
| 207 | 207 | ...validBody, |
| 208 | 208 | id_token: unsignedJwt({ |
| 209 | − iss: "https://accounts.google.com", | |
| 209 | + // Upstream OP issuer — does NOT match issuerUrl below | |
| 210 | + iss: "https://us.posthog.com", | |
| 210 | 211 | aud: "cid", |
| 211 | 212 | sub: "user-1", |
| 212 | 213 | exp: Math.floor(Date.now() / 1000) + 3600, |
| @@ -216,8 +217,8 @@ describe("exchangeAuthorizationCode", () => { · 3 unmodified lines | ||
| 216 | 217 | ); |
| 217 | 218 | const result = await Effect.runPromise( |
| 218 | 219 | exchangeAuthorizationCode({ |
| 219 | − tokenUrl: "https://oauth2.googleapis.com/token", | |
| 220 | − issuerUrl: "https://accounts.google.com", | |
| 220 | + tokenUrl: "https://oauth.posthog.com/oauth/token", | |
| 221 | + issuerUrl: "https://oauth.posthog.com", | |
| 221 | 222 | clientId: "cid", |
| 222 | 223 | redirectUrl: "https://app.example.com/cb", |
| 223 | 224 | codeVerifier: "verifier", |
| @@ -225,41 +226,77 @@ describe("exchangeAuthorizationCode", () => { · 1 unmodified line | ||
| 225 | 226 | }), |
| 226 | 227 | ); |
| 227 | 228 | expect(result.access_token).toBe("tok"); |
| 229 | + expect(result.refresh_token).toBe("rtok"); | |
| 228 | 230 | }); |
| 229 | 231 | |
| 230 | − it("accepts ID token signing algorithms advertised by authorization server metadata", async () => { | |
| 232 | + it("strips id_tokens whose aud does not match the client_id", async () => { | |
| 231 | 233 | captureFetch( |
| 232 | 234 | jsonResponse(200, { |
| 233 | 235 | ...validBody, |
| 234 | − id_token: unsignedJwt( | |
| 235 | − { | |
| 236 | − iss: "https://railway.com", | |
| 237 | − aud: "cid", | |
| 238 | − sub: "user-1", | |
| 239 | − exp: Math.floor(Date.now() / 1000) + 3600, | |
| 240 | − iat: Math.floor(Date.now() / 1000), | |
| 241 | − }, | |
| 242 | − "ES256", | |
| 243 | − ), | |
| 236 | + id_token: unsignedJwt({ | |
| 237 | + iss: "https://example.com", | |
| 238 | + // aud belongs to some other client | |
| 239 | + aud: "another-client", | |
| 240 | + sub: "user-1", | |
| 241 | + exp: Math.floor(Date.now() / 1000) + 3600, | |
| 242 | + iat: Math.floor(Date.now() / 1000), | |
| 243 | + }), | |
| 244 | 244 | }), |
| 245 | 245 | ); |
| 246 | − | |
| 247 | 246 | const result = await Effect.runPromise( |
| 248 | 247 | exchangeAuthorizationCode({ |
| 249 | − tokenUrl: "https://backboard.railway.com/oauth/token", | |
| 250 | − issuerUrl: "https://railway.com", | |
| 248 | + tokenUrl: "https://example.com/token", | |
| 249 | + issuerUrl: "https://example.com", | |
| 251 | 250 | clientId: "cid", |
| 252 | 251 | redirectUrl: "https://app.example.com/cb", |
| 253 | 252 | codeVerifier: "verifier", |
| 254 | 253 | code: "abc", |
| 255 | − idTokenSigningAlgValuesSupported: ["ES256"], | |
| 256 | 254 | }), |
| 257 | 255 | ); |
| 256 | + expect(result.access_token).toBe("tok"); | |
| 257 | + }); | |
| 258 | 258 | |
| 259 | + it("happy path: token endpoint with no id_token still parses normally", async () => { | |
| 260 | + captureFetch(jsonResponse(200, validBody)); | |
| 261 | + const result = await Effect.runPromise( | |
| 262 | + exchangeAuthorizationCode({ | |
| 263 | + tokenUrl: "https://example.com/token", | |
| 264 | + clientId: "cid", | |
| 265 | + redirectUrl: "https://app.example.com/cb", | |
| 266 | + codeVerifier: "verifier", | |
| 267 | + code: "abc", | |
| 268 | + }), | |
| 269 | + ); | |
| 259 | 270 | expect(result.access_token).toBe("tok"); |
| 271 | + expect(result.refresh_token).toBe("rtok"); | |
| 272 | + expect(result.expires_in).toBe(3600); | |
| 260 | 273 | }); |
| 261 | 274 | |
| 262 | − it("ignores unusable ID tokens when the access-token response is otherwise valid", async () => { | |
| 275 | + it("still surfaces RFC 6749 §5.2 error envelopes after the id_token strip", async () => { | |
| 276 | + captureFetch( | |
| 277 | + jsonResponse(400, { | |
| 278 | + error: "invalid_grant", | |
| 279 | + error_description: "authorization code expired", | |
| 280 | + }), | |
| 281 | + ); | |
| 282 | + const exit = await Effect.runPromiseExit( | |
| 283 | + exchangeAuthorizationCode({ | |
| 284 | + tokenUrl: "https://example.com/token", | |
| 285 | + clientId: "cid", | |
| 286 | + redirectUrl: "https://app.example.com/cb", | |
| 287 | + codeVerifier: "verifier", | |
| 288 | + code: "abc", | |
| 289 | + }), | |
| 290 | + ); | |
| 291 | + expect(Exit.isFailure(exit)).toBe(true); | |
| 292 | + if (!Exit.isFailure(exit)) return; | |
| 293 | + const failure = JSON.stringify(exit.cause); | |
| 294 | + expect(failure).toContain("OAuth2Error"); | |
| 295 | + expect(failure).toContain("invalid_grant"); | |
| 296 | + expect(failure).toContain("authorization code expired"); | |
| 297 | + }); | |
| 298 | + | |
| 299 | + it("strips id_tokens with algorithms not advertised in AS metadata (e.g. ES256 without supported list)", async () => { | |
| 263 | 300 | captureFetch( |
| 264 | 301 | jsonResponse(200, { |
| 265 | 302 | ...validBody, |
| @@ -420,12 +457,12 @@ describe("refreshAccessToken", () => { · 154 unmodified lines | ||
| 420 | 457 | expect(body.has("scope")).toBe(false); |
| 421 | 458 | }); |
| 422 | 459 | |
| 423 | − it("validates refreshed ID tokens against an explicit issuer", async () => { | |
| 460 | + it("strips refreshed id_tokens whose iss does not match AS metadata", async () => { | |
| 424 | 461 | captureFetch( |
| 425 | 462 | jsonResponse(200, { |
| 426 | 463 | ...validBody, |
| 427 | 464 | id_token: unsignedJwt({ |
| 428 | − iss: "https://accounts.google.com", | |
| 465 | + iss: "https://us.posthog.com", | |
| 429 | 466 | aud: "cid", |
| 430 | 467 | sub: "user-1", |
| 431 | 468 | exp: Math.floor(Date.now() / 1000) + 3600, |
| @@ -435,8 +472,8 @@ describe("refreshAccessToken", () => { · 3 unmodified lines | ||
| 435 | 472 | ); |
| 436 | 473 | const result = await Effect.runPromise( |
| 437 | 474 | refreshAccessToken({ |
| 438 | − tokenUrl: "https://oauth2.googleapis.com/token", | |
| 439 | − issuerUrl: "https://accounts.google.com", | |
| 475 | + tokenUrl: "https://oauth.posthog.com/oauth/token", | |
| 476 | + issuerUrl: "https://oauth.posthog.com", | |
| 440 | 477 | clientId: "cid", |
| 441 | 478 | refreshToken: "old", |
| 442 | 479 | }), |
| @@ -444,13 +481,13 @@ describe("refreshAccessToken", () => { · 1 unmodified line | ||
| 444 | 481 | expect(result.access_token).toBe("tok2"); |
| 445 | 482 | }); |
| 446 | 483 | |
| 447 | − it("accepts refreshed ID token signing algorithms advertised by authorization server metadata", async () => { | |
| 484 | + it("strips refreshed id_tokens with algorithms not advertised in AS metadata", async () => { | |
| 448 | 485 | captureFetch( |
| 449 | 486 | jsonResponse(200, { |
| 450 | 487 | ...validBody, |
| 451 | 488 | id_token: unsignedJwt( |
| 452 | 489 | { |
| 453 | − iss: "https://railway.com", | |
| 490 | + iss: "https://backboard.railway.com", | |
| 454 | 491 | aud: "cid", |
| 455 | 492 | sub: "user-1", |
| 456 | 493 | exp: Math.floor(Date.now() / 1000) + 3600, |
| @@ -464,43 +501,26 @@ describe("refreshAccessToken", () => { · 7 unmodified lines | ||
| 464 | 501 | const result = await Effect.runPromise( |
| 465 | 502 | refreshAccessToken({ |
| 466 | 503 | tokenUrl: "https://backboard.railway.com/oauth/token", |
| 467 | − issuerUrl: "https://railway.com", | |
| 504 | + issuerUrl: "https://backboard.railway.com", | |
| 468 | 505 | clientId: "cid", |
| 469 | 506 | refreshToken: "old", |
| 470 | − idTokenSigningAlgValuesSupported: ["ES256"], | |
| 471 | 507 | }), |
| 472 | 508 | ); |
| 473 | 509 | |
| 474 | 510 | expect(result.access_token).toBe("tok2"); |
| 475 | 511 | }); |
| 476 | 512 | |
| 477 | − it("ignores unusable refreshed ID tokens when the access-token response is otherwise valid", async () => { | |
| 478 | − captureFetch( | |
| 479 | − jsonResponse(200, { | |
| 480 | − ...validBody, | |
| 481 | − id_token: unsignedJwt( | |
| 482 | − { | |
| 483 | − iss: "https://backboard.railway.com", | |
| 484 | − aud: "cid", | |
| 485 | − sub: "user-1", | |
| 486 | − exp: Math.floor(Date.now() / 1000) + 3600, | |
| 487 | − iat: Math.floor(Date.now() / 1000), | |
| 488 | − }, | |
| 489 | − "ES256", | |
| 490 | − ), | |
| 491 | − }), | |
| 492 | − ); | |
| 493 | − | |
| 513 | + it("happy path: refresh response with no id_token parses normally", async () => { | |
| 514 | + captureFetch(jsonResponse(200, validBody)); | |
| 494 | 515 | const result = await Effect.runPromise( |
| 495 | 516 | refreshAccessToken({ |
| 496 | − tokenUrl: "https://backboard.railway.com/oauth/token", | |
| 497 | − issuerUrl: "https://backboard.railway.com", | |
| 517 | + tokenUrl: "https://example.com/token", | |
| 498 | 518 | clientId: "cid", |
| 499 | 519 | refreshToken: "old", |
| 500 | 520 | }), |
| 501 | 521 | ); |
| 502 | − | |
| 503 | 522 | expect(result.access_token).toBe("tok2"); |
| 523 | + expect(result.expires_in).toBe(3600); | |
| 504 | 524 | }); |
| 505 | 525 | }); |
| 506 | 526 | |
| @@ -224,59 +224,43 @@ const tokenResponseFrom = ( · 223 unmodified lines | ||
| 224 | 224 | scope: r.scope, |
| 225 | 225 | }); |
| 226 | 226 | |
| 227 | −const isUnexpectedIdTokenAlg = (cause: unknown): boolean => | |
| 228 | − cause instanceof Error && | |
| 229 | − cause.message.includes('unexpected JWT "alg" header parameter'); | |
| 230 | − | |
| 231 | −const looseTokenResponseFrom = (body: unknown): OAuth2TokenResponse => { | |
| 232 | − if (typeof body !== "object" || body === null) { | |
| 233 | − throw new Error("token endpoint response body is not an object"); | |
| 234 | − } | |
| 235 | − const record = body as Record<string, unknown>; | |
| 236 | − if (typeof record.access_token !== "string" || !record.access_token) { | |
| 237 | − throw new Error('token endpoint response is missing "access_token"'); | |
| 227 | +// MCP source connections are pure OAuth 2.0 — we never request `openid` and | |
| 228 | +// never consume `id_token`. Some providers (PostHog, etc.) front an OIDC | |
| 229 | +// backend and emit an `id_token` anyway; oauth4webapi then strict-validates | |
| 230 | +// its claims against the AS metadata and rejects mismatches we don't care | |
| 231 | +// about. Strip the field before delegation. | |
| 232 | +const stripIdToken = async (response: Response): Promise<Response> => { | |
| 233 | + const body = await response | |
| 234 | + .clone() | |
| 235 | + .json() | |
| 236 | + .catch(() => null); | |
| 237 | + if ( | |
| 238 | + !body || | |
| 239 | + typeof body !== "object" || | |
| 240 | + !("id_token" in (body as Record<string, unknown>)) | |
| 241 | + ) { | |
| 242 | + return response; | |
| 238 | 243 | } |
| 239 | − const expiresIn = | |
| 240 | − typeof record.expires_in === "number" | |
| 241 | − ? record.expires_in | |
| 242 | − : typeof record.expires_in === "string" | |
| 243 | − ? Number.parseFloat(record.expires_in) | |
| 244 | − : undefined; | |
| 245 | − if (expiresIn !== undefined && !Number.isFinite(expiresIn)) { | |
| 246 | − throw new Error('token endpoint response has invalid "expires_in"'); | |
| 247 | − } | |
| 248 | − return { | |
| 249 | − access_token: record.access_token, | |
| 250 | − token_type: | |
| 251 | − typeof record.token_type === "string" ? record.token_type : undefined, | |
| 252 | − refresh_token: | |
| 253 | − typeof record.refresh_token === "string" | |
| 254 | − ? record.refresh_token | |
| 255 | − : undefined, | |
| 256 | − expires_in: expiresIn, | |
| 257 | − scope: typeof record.scope === "string" ? record.scope : undefined, | |
| 258 | − }; | |
| 244 | + const { id_token: _ignored, ...rest } = body as Record<string, unknown>; | |
| 245 | + return new Response(JSON.stringify(rest), { | |
| 246 | + status: response.status, | |
| 247 | + statusText: response.statusText, | |
| 248 | + headers: response.headers, | |
| 249 | + }); | |
| 259 | 250 | }; |
| 260 | 251 | |
| 261 | 252 | const processTokenEndpointResponse = async ( |
| 262 | 253 | as: oauth.AuthorizationServer, |
| 263 | 254 | client: oauth.Client, |
| 264 | 255 | response: Response, |
| 265 | −): Promise<OAuth2TokenResponse> => { | |
| 266 | − const fallback = response.clone(); | |
| 267 | − try { | |
| 268 | − return tokenResponseFrom( | |
| 269 | − await oauth.processGenericTokenEndpointResponse(as, client, response), | |
| 270 | − ); | |
| 271 | − } catch (cause) { | |
| 272 | − if (!isUnexpectedIdTokenAlg(cause)) throw cause; | |
| 273 | − // Some OAuth-only providers include an ID token even when their metadata | |
| 274 | − // does not advertise enough OIDC signing metadata for strict validation. | |
| 275 | − // Executor stores only access/refresh tokens for source auth, so tolerate | |
| 276 | − // an unusable ID token after the access token response itself is valid. | |
| 277 | − return looseTokenResponseFrom(await fallback.json()); | |
| 278 | − } | |
| 279 | −}; | |
| 256 | +): Promise<OAuth2TokenResponse> => | |
| 257 | + tokenResponseFrom( | |
| 258 | + await oauth.processGenericTokenEndpointResponse( | |
| 259 | + as, | |
| 260 | + client, | |
| 261 | + await stripIdToken(response), | |
| 262 | + ), | |
| 263 | + ); | |
| 280 | 264 | |
| 281 | 265 | // --------------------------------------------------------------------------- |
| 282 | 266 | // Exchange authorization code → tokens |
| @@ -425,18 +409,12 @@ export const refreshAccessToken = ( · 142 unmodified lines | ||
| 425 | 409 | additionalParameters, |
| 426 | 410 | }, |
| 427 | 411 | ); |
| 428 | − const fallback = response.clone(); | |
| 429 | − try { | |
| 430 | − const result = await oauth.processRefreshTokenResponse( | |
| 431 | − as, | |
| 432 | − client, | |
| 433 | − response, | |
| 434 | − ); | |
| 435 | − return tokenResponseFrom(result); | |
| 436 | − } catch (cause) { | |
| 437 | − if (!isUnexpectedIdTokenAlg(cause)) throw cause; | |
| 438 | − return looseTokenResponseFrom(await fallback.json()); | |
| 439 | − } | |
| 412 | + const result = await oauth.processRefreshTokenResponse( | |
| 413 | + as, | |
| 414 | + client, | |
| 415 | + await stripIdToken(response), | |
| 416 | + ); | |
| 417 | + return tokenResponseFrom(result); | |
| 440 | 418 | }, |
| 441 | 419 | catch: toOAuth2Error, |
| 442 | 420 | }); |