Strip client Authorization on upstreamswap custom header#5661
Open
jerm-dro wants to merge 1 commit into
Open
Conversation
The upstreamswap "custom" header strategy injected the upstream IdP token into a custom header but left the client's original Authorization header (the ToolHive-issued JWT) in place, forwarding it to the backend. That token is minted for the proxy, not the upstream, so passing it through is credential passthrough (#5504). The "replace" strategy avoids this implicitly because it overwrites Authorization; the strip-auth middleware from #4168 only runs on the DisableUpstreamTokenInjection path, so it never covered this case. Strip Authorization in the custom injector after setting the custom header, matching the "replace" behavior. When the custom header name is itself Authorization (case-insensitive), the Set already replaced the JWT with the upstream token, so the strip is skipped. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5661 +/- ##
==========================================
+ Coverage 69.93% 70.34% +0.40%
==========================================
Files 650 649 -1
Lines 66289 66188 -101
==========================================
+ Hits 46358 46558 +200
+ Misses 16588 16276 -312
- Partials 3343 3354 +11 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
With
header_strategy: custom, the upstreamswap auth middleware injected the upstream IdP token into a custom header but left the client's originalAuthorizationheader (the ToolHive-issued JWT) in place, forwarding it to the backend MCP server. That JWT is minted for the proxy, not the upstream — passing it through is credential passthrough of a token the backend was never meant to receive.replacestrategy avoids this implicitly: it callsSet("Authorization", ...), overwriting the client JWT with the upstream token.strip-authmiddleware from Allow MCPRemoteProxy to work without upstream or client auth #4168 (pkg/transport/middleware/strip_auth.go) does removeAuthorization, but it is only wired in on theDisableUpstreamTokenInjectionpath, so it never ran on the upstreamswap custom path.Authorizationafter setting the custom header, matchingreplace. When the custom header name is itselfAuthorization(case-insensitive, viahttp.CanonicalHeaderKey), theSetalready replaced the JWT with the upstream token, so the strip is skipped.Scoped to
Authorizationonly (not Cookie/Proxy-Authorization) to mirror thereplacereference path the issue identifies as non-leaking.Fixes #5504
Type of change
Test plan
task test)task lint-fix) — clean forpkg/auth/upstreamswap/...Added a regression test that fails on the buggy code (
Should be empty, but was Bearer toolhive-jwt) and passes after the fix, plus a case-insensitive edge-case test for a custom header namedAuthorization. Updated two existing tests that had been asserting the leaked-JWT behavior. Verified red→green by stashing the production change and re-running.Does this introduce a user-facing change?
Yes. When
header_strategy: customis configured, the backend MCP server no longer receives the client's ToolHive-issued JWT in theAuthorizationheader. The upstream token is still delivered in the configured custom header. Backends that (incorrectly) relied on the leaked JWT will no longer see it.Special notes for reviewers
Flagging this as a security-hardening fix for auth review — it closes a credential-passthrough path. Worth scrutinizing: the
Authorization-as-custom-header edge case (must NOT strip there), and the decision to scope the strip toAuthorizationonly to stay consistent withreplacerather than adopting the broader strip-auth header set (Cookie, Proxy-Authorization).Generated with Claude Code