Repository navigation
fix: give caller-derived upstreams their own session per principal - #67
Closed
blakebauman wants to merge 1 commit into
Closed
blakebauman wants to merge 1 commit into
blakebauman wants to merge 1 commit into
Conversation
Attaching a credential per outgoing request is necessary but not sufficient. The MCP session itself is established by whichever caller happens to open it, and that identity is durable: the SDK detaches the connection context but deliberately preserves its values, so the initialize, the standalone SSE stream, and every reconnect of that stream carry the opening caller's credential for as long as the connection lives. A shared root session under passthrough or token-exchange therefore presents one caller's identity to the upstream while carrying another's bearer on the request. An upstream that scopes anything to the session it minted — the normal thing for a server that authenticates — is entitled to resolve that either way: serve the wrong caller's view, or reject the mismatch, which guardedDo records as a breaker failure until the circuit opens for everyone. It also means fold keeps re-minting a departed caller's exchanged token from the credential captured in that detached context, indefinitely. Key the root session by (issuer, subject) — the same key the exchanged-token cache uses — whenever the credential is caller-derived, and keep the single shared session otherwise, so nothing changes for an upstream whose identity is the same whoever asks. The subscription set moves onto the session entry, which now outlives its session so a reconnect replays what that caller established. Per-caller entries age out on the idle sweep that already bounds bridged sessions, since they are keyed by an identifier the gateway does not choose; the shared root is never swept. The regression gate asserts at the upstream, which is the only vantage point where the crossing is visible: no upstream session may ever see two different caller credentials. Added p50 is unchanged at 182µs (gate 5ms) — the shared path returns its key without touching the context — and conformance stays 40/40. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
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.
The bug
Attaching a credential per outgoing request is necessary but not sufficient, and the gateway's own bookkeeping looks correct either way — which is why this survived.
credentialTransport.RoundTripreally does resolve credentials fromreq.Context()on every request (gateway/upstream.go:339-361). But the sharedrootSessionconnects lazily under whichever caller arrived first (:612-618), and the SDK detaches that context withxcontext.Detach, which preserves context values and drops only cancellation. Its own comment says why:So the connection context permanently holds the first caller's
*auth.Principal:initializeis credentialed by caller A, so the upstream mints a session bound to AEverything through
u.do→rootSessioninherits it: all four list methods,resources/read,subscribe,completion/complete,tasks/*. On the wire the upstream sees a session established by A carrying POSTs bearing B's token, and resolves it either way:*jsonrpc.Error, soguardedDorecords a breaker failure (:850) and five of them open the circuit for every caller of that upstreamThe tell that this was unfinished rather than intended:
UpstreamCredentials.PerRequest()(auth/upstream.go:291) — the one function that asks "is this credential caller-derived, so it must be attached per request rather than per session?" — was dead code until #66. The property only ever gated the list-cache kill and anauth.modevalidation. The session question it names was never wired up.The fix
Key the root session by
(issuer, subject)— the same key the exchanged-token cache already uses — whenever the credential is caller-derived; keep the single shared session otherwise, so nothing changes for an upstream whose identity is the same whoever asks.u.session+u.subscribedbecomeu.roots map[string]*rootEntry, withsharedRootKey("") for the shared case, so both paths are one mechanism rather than two.dropSessionclearssessionbut keeps the entry, because itssubscribedset is what replays that caller's subscriptions onto the reconnect.subscribedURIsunions across entries (reload carry-over) andisSubscribedreports whether any session holds the URI, which is the question the downstream ref-count actually asks.Tests
TestCallerDerivedUpstreamsGetTheirOwnSession— the gate. It asserts at the upstream, which is the only vantage point where the crossing is observable: no upstream session may ever see two caller credentials. Verified to fail on the pre-fix behavior:TestConfiguredCredentialUpstreamsStillShareOneSession— the partitioning is confined to caller-derived credentials; astaticupstream still opens exactly one.TestPerCallerSessionsAreSweptWhenIdle— the map is bounded and the shared root survives.Gate
go test -race ./...clean,golangci-lint0 issues, conformance 40/40 (includingresources-subscribe/resources-unsubscribe, which exercise the refactored subscription state), and the latency gate is unmoved:The shared path returns its key without touching the context, so the hot path for an ordinary federation is unchanged.
Docs
docs/security-model.mdcredential-containment section anddocs/configuration.md's upstream-auth notes.🤖 Generated with Claude Code