Skip to content

fix(mcp): validate Origin and MCP-Protocol-Version; document the exact mount URL - #685

Merged
jimhoyd merged 1 commit into
mainfrom
fix/mcp-origin-protocol-version
Sep 24, 2026
Merged

jimhoyd merged 1 commit into
mainfrom
fix/mcp-origin-protocol-version

Conversation

@jimhoyd

@jimhoyd jimhoyd commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Fixes #676
Fixes #672

What changed

#676, packages/mcp/src/mcp.ts handle()

  • Origin. If an Origin header is present and is not exactly the site's canonical origin (context.origin, i.e. --origin), the request gets 403 Forbidden before its content type, size or body is read. A request with no Origin is still admitted, because non-browser MCP clients don't send one. This uses the same exact-match rule as packages/forms and packages/store.
  • MCP-Protocol-Version. Every message after initialize is checked, requests and notifications alike. A missing header is treated as 2025-03-26, as the transport spec says. A value outside SUPPORTED_PROTOCOL_VERSIONS gets 400. initialize still negotiates from params.protocolVersion and ignores the header. No protocol versions were added.

#672, docs only (runtime behavior unchanged). The client endpoint is the declared mount, exactly: POST https://site.example/mcp. /mcp/* is route syntax that core requires. A request to /mcp/ or to any subpath gets 404. This is now stated in the README "Declare a server" section, llms.txt, the scaffold note in src/extension.ts, and docs/EXTENSIONS.md.

Docs updated in this PR: README ("Declare a server"; "What this implements", where this also removes a duplicated "protocol version negotiation" bullet), SECURITY.md, llms.txt, CHANGELOG (Unreleased), the MCP paragraph in docs/EXTENSIONS.md, and the regenerated llms-full.txt.

Evidence

  • New tests in packages/mcp/test/mcp.test.ts:
    • Origin: a foreign Origin gets 403, including on a malformed body, so the check runs before parsing. Other schemes, other ports and the literal null also get 403. A matching Origin and no Origin both get 200.
    • Protocol version: an unsupported header gets 400 on a request and on a notification. A missing header and each supported version get 200. initialize ignores the header.
    • Mount path: /mcp/ gets 404.
  • The test helper call() used to drop content-type whenever a caller passed init.headers, because of the spread order. No existing test passed headers, so nothing depended on that. It's fixed here.
  • npm run typecheck: passes.
  • mcp package tests: 22 of 22 pass.
  • npm run verify: exit 0 on macOS.

Limitations and follow-up

  • There is no allowlist for other origins. A browser-based MCP client served from a different origin can't reach a mount. Whether to add a per-server allowedOrigins setting (or a general core route option) is still undecided, so it's left as a follow-up to [Request]: Origin allowlist and MCP-Protocol-Version check for @jimhoyd/urlcode-mcp mounts #676 and not added here.
  • Checking the Origin is not authentication. To require an identified caller, put the mount behind auth.
  • Passing CI is not an independent security review.

🤖 Generated with Claude Code

…t mount URL

- Refuse a present Origin that is not exactly the site's canonical origin
  with 403 before parsing (DNS-rebinding check the Streamable HTTP transport
  requires); an absent Origin is still admitted. Mirrors the forms/store idiom.
- On every message after initialize, treat a missing MCP-Protocol-Version as
  2025-03-26 and answer 400 for an unsupported value.
- Document that the client endpoint is the declared mount exactly: /mcp/* is
  required route syntax, and /mcp/ or any subpath answers 404. Add a test.

Fixes #676
Fixes #672

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jimhoyd
jimhoyd merged commit 1ed366a into main Sep 24, 2026
23 checks passed
@jimhoyd
jimhoyd deleted the fix/mcp-origin-protocol-version branch September 24, 2026 20:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant