feat: add Origin header validation to MapMcp endpoints - #1819
Open
ez-lbz wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds default
Originheader validation to the MCP endpoints mapped byMapMcp, so the ASP.NET Core server rejects cross-origin browser requests by default instead of relying on external mitigation.The Streamable HTTP spec requires servers to validate the
Originheader for browser clients. Currently the C# ASP.NET Core integration performs no origin validation —MapMcp()only registers routes, and the docs point users at external CORS/Kestrel configuration, leaving the default posture fail-open.Behavior
Requests are handled as follows:
Originheader (SDK clients,curl, other non-browser callers) — always allowed.Hostheader (same-origin) — allowed.localhost,127.0.0.1,[::1]) — allowed by default, so browsers running on the same machine (e.g. a frontend dev server) work without configuration.403 Forbidden.HttpServerTransportOptions.AllowedOrigins, and validation can be turned off entirely viaHttpServerTransportOptions.DisableOriginValidation.The scheme is intentionally not compared when matching the request host, since TLS is commonly terminated at a reverse proxy.
Implementation
HttpServerTransportOptionsgainsAllowedOriginsandDisableOriginValidation.OriginValidationEndpointFilteris registered on theMapMcproute group as an endpoint filter when validation is enabled, applying to both the Streamable HTTP and (when enabled) legacy SSE endpoints. CORS preflightOPTIONSrequests are unaffected — they are answered by the CORS middleware before endpoint execution.Tests
Added
OriginValidationTestscovering requests with noOrigin, same-origin, loopback (localhost,127.0.0.1,[::1]), cross-origin (403), malformed origin, explicitly allowed origins, and the disabled opt-out.Checklist