# Which invariants prevent cross-stream token substitution in HTTP-native MCP server multiplexing **Date of Record:** 2026 November 23 **Scope:** HTTP-native MCP servers multiplexing multiple logical client streams (SSE or chunked bidirectional transport) over a single TCP or pooled HTTP/2 connection, where each stream carries an authorization token bound at stream-open time. Excludes stdio-transport servers and deployments where each stream terminates at a distinct TLS connection with per-connection auth. ## Core Distinction Multiplexing is a *transport* property. Authorization is a *stream* property. The boundary failure occurs when the server treats an authenticated connection as authority for every stream it carries, rather than requiring each stream to independently present and validate a token bound to that stream's identity at open time. A connection-level auth check is necessary but not sufficient; the invariant is per-stream and must be re-asserted on every inbound frame, not merely at stream establishment. ## What Was Observed An MCP server accepted an `initialize` request on stream A with token `T_A` (scope: `tools:read`). Stream B opened on the same HTTP/2 connection with token `T_B` (scope: `tools:write`). A subsequent `tools/call` frame arrived on stream B carrying a `Mcp-Session-Id` minted during stream A's handshake. The server resolved the session by ID, looked up the *connection's* principal (`T_A`'s identity, recorded at connection accept), and executed the write. The authorization decision was made against the connection principal, not the stream's declared token, and not the session's originating token. Result: a read-only principal caused a write. The non-obvious part: no error was raised. The session ID was valid. The token on stream B was valid. The scope check passed—against the wrong token. The failure is silent because every individual check succeeded; only the *binding* between them was missing. ## Condition of Validity The per-stream token binding invariant holds only when all four of the following are simultaneously true: 1. The session identifier is minted with an immutable reference to the token fingerprint that created it, never overwritten. 2. Every inbound frame is re-authorized against the *session's* bound token fingerprint, not the connection's, and not the frame's transport-level credential. 3. A stream may not attach to a session whose bound token fingerprint differs from the token presented on that stream, even if both tokens are individually valid and the presenting principal is a superset of the session's scope. 4. Session IDs are opaque and unguessable, and the server rejects any frame where `Mcp-Session-Id` is present but the stream carries no token (rather than falling back to connection auth). Violation of (3) is the most common and most dangerous, because it is where "valid token" is conflated with "the right token." ## Decision Boundary The server must reject—not downgrade, not fall back—when the session's bound token fingerprint and the stream's presented token fingerprint diverge. There is no legitimate case for cross-token session attachment in a correctly designed MCP deployment: if a client needs to escalate, it opens a new session with the escalated token. Falling back to connection auth on a missing stream token is likewise a rejection condition, not a convenience. The decision boundary is sharp: fingerprint match → proceed; mismatch or absence → 403 with no side effects, and the session is not mutated. ## Operational Effect When the invariant is enforced, token substitution attacks become impossible by construction, and the failure mode shifts from silent privilege escalation to loud, per-frame 403s that are trivially observable in logs. When it is not enforced, the system exhibits *authorization confusion*: correct-looking audit trails (every token valid, every session valid) that misattribute actions to the wrong principal. Debugging requires reconstructing the connection→stream→session→token graph, which is usually not logged. The cost of enforcement is one fingerprint comparison per frame—negligible—against the cost of a silent cross-tenant write, which is unbounded. ```python import hashlib from dataclasses import dataclass class StreamTokenBindingError(RuntimeError): """Raised when a stream's token does not match its session's bound token.""" @dataclass(frozen=True) class Session: session_id: str bound_token_fingerprint: str @dataclass(frozen=True) class InboundFrame: session_id: str stream_token: str | None def fingerprint(token: str) -> str: return hashlib.sha256(token.encode("utf-8")).hexdigest() def authorize_frame(frame: InboundFrame, session: Session) -> None: # Invariant 4: no token on the stream is a rejection, not a fallback. if frame.stream_token is None: raise StreamTokenBindingError( f"stream carries no token for session {frame.session_id!r}; " "refusing connection-level fallback" ) presented = fingerprint(frame.stream_token) # Invariant 3: the stream's token must be the session's token. if presented != session.bound_token_fingerprint: raise StreamTokenBindingError( f"token substitution on session {frame.session_id!r}: " f"session bound to {session.bound_token_fingerprint[:12]}..., " f"stream presented {presented[:12]}..." ) # Frame is authorized against the session's bound principal. return None def handle_tools_call(frame: InboundFrame, session: Session) -> str: authorize_frame(frame, session) return "ok" ```