fix: prevent bearer tokens from leaking to discovery endpoints#1135
Open
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Open
fix: prevent bearer tokens from leaking to discovery endpoints#1135Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Bortlesboat wants to merge 1 commit intomodelcontextprotocol:mainfrom
Conversation
Stop adding Authorization headers to requestInit.headers, which the SDK's createFetchWithInit merges into all HTTP requests including OAuth discovery and metadata endpoints (/.well-known/*). OAuth tokens are now handled exclusively by the authProvider via the transport's _commonHeaders() method, which only applies them to MCP server requests. Custom Authorization headers from the UI are applied through the fetch wrapper's mergeHeaders helper, which lets the SDK's _commonHeaders() take priority. Fixes modelcontextprotocol#1092
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
Fixes #1092. When an MCP server returns 401 for an expired token, the inspector was sending the expired bearer token to OAuth discovery and metadata endpoints (
/.well-known/*) during re-authentication. This caused failures in environments like Istio where sending a bad token to anonymous endpoints results in denial.Root cause: Authorization headers were added to
requestInit.headers, which the SDK'screateFetchWithInitwrapper merges into all HTTP requests — including discovery/metadata fetches made byauth(),discoverOAuthProtectedResourceMetadata(), anddiscoverAuthorizationServerMetadata().Fix:
requestInit.headersentirely. TheauthProvideralready handles bearer tokens dynamically via the transport's_commonHeaders()method, which only applies them to MCP server requests.requestInit.headers. Instead, apply them through amergeHeadershelper in the customfetchwrapper, where the SDK's_commonHeaders()Authorization takes priority.Test plan
requestInit.headers