|
14 | 14 | * negotiates that era, including the `Mcp-Param-Region` header the client |
15 | 15 | * mirrors from `get-header-params`'s `x-mcp-header` argument and the change |
16 | 16 | * notifications published on the handler's subscription bus. |
| 17 | + * |
| 18 | + * A browser request is served only from a localhost origin: the tests pin |
| 19 | + * that a foreign `Origin` is refused with a 403 and no CORS grant at every |
| 20 | + * route, preflights included, and that a localhost one is served. |
17 | 21 | */ |
18 | 22 | import type { Server } from "node:http"; |
19 | 23 | import { once } from "node:events"; |
@@ -404,20 +408,150 @@ describe("Streamable HTTP transport", () => { |
404 | 408 | ); |
405 | 409 | }); |
406 | 410 |
|
407 | | - it("answers a CORS preflight for any origin and exposes the MCP headers", async () => { |
| 411 | + it("answers a CORS preflight from a localhost origin, echoing that origin, and exposes the MCP headers", async () => { |
408 | 412 | const response = await fetch(mcp, { |
409 | 413 | method: "OPTIONS", |
410 | 414 | headers: { |
411 | | - origin: "http://elsewhere.example", |
| 415 | + origin: "http://localhost:6274", |
412 | 416 | "access-control-request-method": "DELETE", |
413 | 417 | }, |
414 | 418 | }); |
415 | 419 | expect(response.status).toBe(204); |
416 | | - expect(response.headers.get("access-control-allow-origin")).toBe("*"); |
| 420 | + expect(response.headers.get("access-control-allow-origin")).toBe( |
| 421 | + "http://localhost:6274", |
| 422 | + ); |
417 | 423 | expect(response.headers.get("access-control-allow-methods")).toBe( |
418 | 424 | "GET,POST,DELETE", |
419 | 425 | ); |
420 | 426 | }); |
| 427 | + |
| 428 | + it.each(["legacy", "modern"] as const)( |
| 429 | + "serves a %s-era client whose requests carry a localhost Origin", |
| 430 | + async (era) => { |
| 431 | + // Every request the transport makes carries the Origin a browser page |
| 432 | + // served from localhost would send. |
| 433 | + const withOrigin = (url: string | URL, init?: RequestInit) => { |
| 434 | + const headers = new Headers(init?.headers); |
| 435 | + headers.set("origin", "http://[::1]:6274"); |
| 436 | + return fetch(url, { ...init, headers }); |
| 437 | + }; |
| 438 | + const client = new Client( |
| 439 | + { name: "http-origin-test", version: "0.0.0" }, |
| 440 | + era === "modern" |
| 441 | + ? { versionNegotiation: { mode: { pin: "2026-07-28" } } } |
| 442 | + : {}, |
| 443 | + ); |
| 444 | + await client.connect( |
| 445 | + new StreamableHTTPClientTransport(new URL(mcp), { fetch: withOrigin }), |
| 446 | + ); |
| 447 | + expect(client.getProtocolEra()).toBe(era); |
| 448 | + const result = await client.callTool({ |
| 449 | + name: "echo", |
| 450 | + arguments: { message: "from localhost" }, |
| 451 | + }); |
| 452 | + expect(result.content).toEqual([ |
| 453 | + { type: "text", text: "Echo: from localhost" }, |
| 454 | + ]); |
| 455 | + await client.close(); |
| 456 | + }, |
| 457 | + ); |
| 458 | + |
| 459 | + describe("a request from a foreign origin", () => { |
| 460 | + const FOREIGN = "http://elsewhere.example"; |
| 461 | + const CALL_GET_ENV = { |
| 462 | + jsonrpc: "2.0", |
| 463 | + id: 2, |
| 464 | + method: "tools/call", |
| 465 | + params: { name: "get-env", arguments: {} }, |
| 466 | + }; |
| 467 | + |
| 468 | + /** POST `message` with a foreign Origin, and expect it refused. */ |
| 469 | + async function expectRefused(message: object, headers = {}) { |
| 470 | + const response = await fetch(mcp, { |
| 471 | + method: "POST", |
| 472 | + headers: { ...JSON_HEADERS, ...headers, origin: FOREIGN }, |
| 473 | + body: JSON.stringify(message), |
| 474 | + }); |
| 475 | + expect(response.status).toBe(403); |
| 476 | + expect(response.headers.get("access-control-allow-origin")).toBeNull(); |
| 477 | + expect(await response.json()).toMatchObject({ |
| 478 | + jsonrpc: "2.0", |
| 479 | + error: { code: -32000 }, |
| 480 | + id: null, |
| 481 | + }); |
| 482 | + } |
| 483 | + |
| 484 | + it("is refused at preflight, with no CORS grant", async () => { |
| 485 | + const response = await fetch(mcp, { |
| 486 | + method: "OPTIONS", |
| 487 | + headers: { |
| 488 | + origin: FOREIGN, |
| 489 | + "access-control-request-method": "POST", |
| 490 | + }, |
| 491 | + }); |
| 492 | + expect(response.status).toBe(403); |
| 493 | + expect(response.headers.get("access-control-allow-origin")).toBeNull(); |
| 494 | + }); |
| 495 | + |
| 496 | + it("cannot start a 2025-era session", async () => { |
| 497 | + await expectRefused({ |
| 498 | + jsonrpc: "2.0", |
| 499 | + id: 1, |
| 500 | + method: "initialize", |
| 501 | + params: { |
| 502 | + protocolVersion: "2025-11-25", |
| 503 | + capabilities: {}, |
| 504 | + clientInfo: { name: "raw", version: "0" }, |
| 505 | + }, |
| 506 | + }); |
| 507 | + expect(log).not.toHaveBeenCalledWith( |
| 508 | + expect.stringMatching(/^Session initialized/), |
| 509 | + ); |
| 510 | + }); |
| 511 | + |
| 512 | + it("cannot call a tool on an open 2025-era session", async () => { |
| 513 | + const { sessionId } = await rawInitialize(); |
| 514 | + await expectRefused(CALL_GET_ENV, { |
| 515 | + "mcp-session-id": sessionId, |
| 516 | + "mcp-protocol-version": "2025-11-25", |
| 517 | + }); |
| 518 | + }); |
| 519 | + |
| 520 | + it("cannot call a tool as a 2026-07-28 client", async () => { |
| 521 | + const foreign = (url: string | URL, init?: RequestInit) => { |
| 522 | + const headers = new Headers(init?.headers); |
| 523 | + headers.set("origin", FOREIGN); |
| 524 | + return fetch(url, { ...init, headers }); |
| 525 | + }; |
| 526 | + const client = new Client( |
| 527 | + { name: "http-foreign-test", version: "0.0.0" }, |
| 528 | + { versionNegotiation: { mode: { pin: "2026-07-28" } } }, |
| 529 | + ); |
| 530 | + const attempt = async () => { |
| 531 | + await client.connect( |
| 532 | + new StreamableHTTPClientTransport(new URL(mcp), { fetch: foreign }), |
| 533 | + ); |
| 534 | + return client.callTool({ name: "get-env", arguments: {} }); |
| 535 | + }; |
| 536 | + await expect(attempt()).rejects.toThrow(/403|Invalid Origin/); |
| 537 | + await client.close(); |
| 538 | + }); |
| 539 | + |
| 540 | + it.each(["GET", "DELETE"])("cannot %s a session", async (method) => { |
| 541 | + const { sessionId } = await rawInitialize(); |
| 542 | + const response = await fetch(mcp, { |
| 543 | + method, |
| 544 | + headers: { |
| 545 | + accept: "text/event-stream", |
| 546 | + "mcp-session-id": sessionId, |
| 547 | + "mcp-protocol-version": "2025-11-25", |
| 548 | + origin: FOREIGN, |
| 549 | + }, |
| 550 | + signal: AbortSignal.timeout(2000), |
| 551 | + }); |
| 552 | + expect(response.status).toBe(403); |
| 553 | + }); |
| 554 | + }); |
421 | 555 | }); |
422 | 556 |
|
423 | 557 | describe("shutdown", () => { |
|
0 commit comments