mcp-oauth-callback.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { afterEach, describe, expect, it } from "vitest";
  2. import {
  3. createLocalhostOAuthCallbackServer,
  4. MCP_OAUTH_CALLBACK_DENIED_ERROR_CODE,
  5. type LocalhostOAuthCallbackServer,
  6. } from "../src/auth/localhost-callback.js";
  7. const CALLBACK_PATH = "/oauth/callback/mcp/callback-test";
  8. const CORRECT_STATE = "correct-transaction-state";
  9. const openServers: LocalhostOAuthCallbackServer[] = [];
  10. afterEach(async () => {
  11. await Promise.all(openServers.splice(0).map((server) => server.close()));
  12. });
  13. async function createServer(): Promise<LocalhostOAuthCallbackServer> {
  14. const server = await createLocalhostOAuthCallbackServer({
  15. callbackPath: CALLBACK_PATH,
  16. state: CORRECT_STATE,
  17. });
  18. openServers.push(server);
  19. return server;
  20. }
  21. describe("localhost OAuth callback server", () => {
  22. it("keeps waiting after a mismatched state request and still accepts the correct callback", async () => {
  23. const server = await createServer();
  24. // Bug 回归:陌生 state(并发事务、浏览器里残留的旧授权 URL)过去会 reject 本事务的
  25. // callback promise,随后到达的正确回调再也无法成功。
  26. const mismatched = await fetch(
  27. `${server.callbackUrl}?state=some-other-transaction&code=other-code`,
  28. );
  29. expect(mismatched.status).toBe(400);
  30. const accepted = await fetch(`${server.callbackUrl}?state=${CORRECT_STATE}&code=good-code`);
  31. expect(accepted.status).toBe(200);
  32. const callback = await server.waitForCallback();
  33. expect(callback.code).toBe("good-code");
  34. });
  35. it("does not settle on requests for another path", async () => {
  36. const server = await createServer();
  37. const wrongPath = await fetch(`${server.callbackUrl}-other?state=${CORRECT_STATE}&code=x`);
  38. expect(wrongPath.status).toBe(404);
  39. const pending = await Promise.race([
  40. server.waitForCallback().then(() => "settled" as const),
  41. new Promise<"pending">((resolve) => setTimeout(() => resolve("pending"), 50)),
  42. ]);
  43. expect(pending).toBe("pending");
  44. });
  45. it("fails immediately when the authorization server reports access_denied", async () => {
  46. const server = await createServer();
  47. // 先挂 catch 再发请求:handler 必须在 reject 之前就绪,否则 Node 会先报 unhandled rejection。
  48. const captured = server.waitForCallback().catch((error: unknown) => error);
  49. const denied = await fetch(
  50. `${server.callbackUrl}?state=${CORRECT_STATE}&error=access_denied&error_description=User%20denied%20the%20request`,
  51. );
  52. expect(denied.status).toBe(400);
  53. await expect(captured).resolves.toMatchObject({
  54. code: MCP_OAUTH_CALLBACK_DENIED_ERROR_CODE,
  55. oauthError: "access_denied",
  56. oauthErrorDescription: "User denied the request",
  57. });
  58. });
  59. it("fails immediately when the matching-state callback carries neither code nor error", async () => {
  60. const server = await createServer();
  61. const captured = server.waitForCallback().catch((error: unknown) => error);
  62. const malformed = await fetch(`${server.callbackUrl}?state=${CORRECT_STATE}`);
  63. expect(malformed.status).toBe(400);
  64. await expect(captured).resolves.toMatchObject({
  65. message: expect.stringContaining("missing an authorization code"),
  66. });
  67. });
  68. });