fix: prevent duplicate Authorization header in SSEClientTransport#2087
Open
Christian-Sidak wants to merge 1 commit into
Open
fix: prevent duplicate Authorization header in SSEClientTransport#2087Christian-Sidak wants to merge 1 commit into
Christian-Sidak wants to merge 1 commit into
Conversation
When both `requestInit.headers` and `eventSourceInit.fetch` provided the same `Authorization` header, the SDK's internal SSE wrapper passed a `Headers` instance (which lowercases all keys) to the user's fetch. If that fetch spread the headers into a plain object and then overlaid its own closure headers (with original casing), the result had both `authorization` and `Authorization` as distinct keys. `new Headers()` joins them as "Bearer X, Bearer X", causing strict servers to reject the request with 401. Fix: use `opts.fetch ?? globalThis.fetch` (not `eventSourceInit.fetch`) as the underlying transport inside the SDK's internal SSE fetch wrapper. Users who relied on `eventSourceInit.fetch` for header injection should migrate to the top-level `fetch` option. Fixes modelcontextprotocol#1872 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 5da9740 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@modelcontextprotocol/client
@modelcontextprotocol/server
@modelcontextprotocol/express
@modelcontextprotocol/fastify
@modelcontextprotocol/hono
@modelcontextprotocol/node
commit: |
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.
Summary
requestInit.headersandeventSourceInit.fetchwith the sameAuthorizationheader caused the server to receiveBearer X, Bearer X(comma-joined duplicate) instead of a singleBearer Xvalue._startOrAuth()usedeventSourceInit.fetchas the underlying HTTP transport inside the SDK's internal SSE wrapper. The wrapper passed aHeadersinstance (which normalizes keys to lowercase) to the user's fetch. If the user's fetch spread those headers into a plain object and overlaid its own closure headers (with original casing), the result had bothauthorizationandAuthorizationas separate keys;new Headers()joins them as"X, X"per the HTTP multi-value spec.fetchImplfromthis._eventSourceInit?.fetch ?? this._fetch ?? fetchtothis._fetch ?? fetch. The SDK's wrapper fully replaceseventSourceInit.fetchfor the EventSource library, sofetchImplshould be the raw transport (opts.fetchorglobalThis.fetch), not the user's header-injecting wrapper.Test plan
does not duplicate Authorization header when requestInit.headers and eventSourceInit.fetch both set itthat reproduces the exact user pattern from the bug report (spreadHeadersthen overlay plain-object closure headers).uses custom fetch implementation from EventSourceInit to add auth headersto use the top-levelfetchoption (the correct post-fix migration path).pnpm --filter @modelcontextprotocol/client test).Fixes #1872