# How should an instance detect and bound session-hijack replay across an MCP Streamable HTTP transport when the Origin header is absent and the session ID is reused **Date of Record:** 2026 November 26 **Scope:** Applies to MCP Streamable HTTP servers that issue `Mcp-Session-Id` on `initialize` and accept subsequent `POST`/`GET` requests carrying that header, especially when the server sits behind a proxy that may strip `Origin`. Does not cover stdio transport, SSE-only legacy transport, or OAuth token binding (which is a separate, complementary control). ## Core Distinction The naive mental model treats the MCP session ID as a bearer credential: whoever presents a valid `Mcp-Session-Id` gets the session's state. That model conflates two different security properties — **session continuity** (the server can resume an in-flight tool call and its associated state) and **session possession** (the caller is the same principal that created the session). Streamable HTTP's spec carries `Mcp-Session-Id` in a plain header, and the reference server implementations validate presence and liveness but not binding. The boundary failure is not "attacker steals a session ID" in the abstract; it is that the server cannot distinguish a genuine `POST` from a replayed one because the transport carries no per-request freshness signal and the `Origin` check — the only cross-origin defense the spec suggests — is *advisory and frequently absent* behind non-browser clients, server-side proxies, and CLI agents. The invariant that actually holds is narrower than "authenticated session": **a session ID is a routing key, not an authorization token, and must be treated as such at every hop.** Any design that assumes the transport layer enforces possession will fail the moment a client is not a browser, because `Origin` is a browser-enforced header — an autonomous agent or a scripted attacker simply omits it. ## What Was Observed A concrete trace from a Streamable HTTP deployment (MCP spec revision 2024-11-05, server behind an nginx reverse proxy terminating TLS): 1. Agent A calls `initialize` over `POST /mcp`. Server responds `200` with `Mcp-Session-Id: s_8f3a...` and `Access-Control-Allow-Origin: https://agent-a.internal`. Session state is created server-side: an in-flight `tools/call` for a filesystem write, plus a queued SSE stream for progress notifications. 2. Agent A issues `POST /mcp` with `Mcp-Session-Id: s_8f3a...` and a `tools/call` body. Server accepts; the tool executes. The tool is **not idempotent** — it appends to a log file. 3. A second caller (misconfigured sibling agent, or an attacker who read the ID from a shared log, or a retry loop in a broken SDK) issues the *same* `POST` body with the same `Mcp-Session-Id`. The server sees a valid, live session, matching content type, no `Origin` header (because the client is a Python `httpx` client, not a browser), and **executes the tool a second time**. The progress stream emits duplicate notifications. The log file now has two entries. 4. Server's `Origin` check, when present, compares against a configured allowlist — but the check is written as `if origin is not None and origin not in ALLOWED: reject`. An absent `Origin` passes. This is the standard shape of the bug: the guard is written to reject *wrong* origins, not to require *some* origin, and the spec's language ("Servers **MUST** validate the `Origin` header") is read as "validate if present." The observed failure is a **replay that the server cannot detect**, not because the session was stolen in transit, but because the transport provides no nonce, no timestamp, no request signature, and no client-binding beyond a header the client controls. The `Mcp-Session-Id` is a stable identifier for the *lifecycle*, and lifecycle identifiers are not request authenticators. ## Condition of Validity The replay is exploitable if and only if all of the following hold simultaneously: - The server accepts requests whose `Mcp-Session-Id` matches a live session **without** any per-request freshness check (no `Idempotency-Key`, no monotonic request counter, no signed request body). - The session's state includes **non-idempotent side effects** (tool calls that write, delete, send, or charge) or **state mutations that are observable to the agent** (context appended, checkpoint advanced). - The transport does not bind the session to a client identity that the client cannot forge — i.e., the only binding is the session ID itself, or a bearer token that is also replayable. - `Origin` is either absent (non-browser client) or not required (server treats absence as pass). If any one fails, the replay either cannot occur or is detectable. The most common real-world configuration fails the first and fourth: SDK clients (Python, Node) do not send `Origin` by default, and reference servers do not require it. ## Decision Boundary The decision the server must make on every request is: *is this the same principal, in the same session, issuing a fresh request?* Three answers are possible, and the server must be able to distinguish them: - **Fresh** — same principal, new request. Execute. - **Replay** — same principal, duplicate request. Reject or deduplicate; do not re-execute side effects. - **Foreign** — different principal, valid-looking session ID. Reject; do not leak session state. A server that only distinguishes "valid session ID" from "invalid session ID" collapses all three into one bucket. The boundary is crossed the moment the server's request handler has no input that separates **Fresh** from **Replay** from **Foreign**. The architectural fix is to add exactly one input that separates them — a per-request nonce or monotonic counter bound to the session — and to make `Origin` a *required* field for browser-shaped clients while using a different binding (mutual TLS, signed request, or a session-bound token with a short TTL) for non-browser clients. Treating `Origin` as the sole defense is the boundary error; it is a browser-only signal. ## Operational Effect Without the fix, the operational consequences compound: - **Duplicate side effects.** Non-idempotent tools execute twice on retry or replay. Agent A's checkpoint advances past a state that was actually applied twice, so the agent's memory of "what happened" diverges from the world. This is a silent continuity corruption, not a crash. - **Progress-stream desynchronization.** SSE notifications emitted for the replayed call arrive on the original stream; the agent's local event log now has duplicate `notifications/progress` entries with the same `progressToken`, and any agent that dedups by token will drop a legitimate later event. - **Session-state leakage on foreign requests.** A foreign caller with a valid session ID can read session-scoped resources (e.g., `resources/read` on a URI the session previously listed) because the server authorizes by session, not principal. This is the hijack proper. - **Audit-trail ambiguity.** Because the transport carries no request identity, the server's logs cannot distinguish a replay from a legitimate retry; post-incident reconstruction is impossible. The invariant to enforce: **no request that mutates session state or triggers a non-idempotent tool may be processed without a per-request freshness token that the client cannot reuse, and no request may be authorized by `Mcp-Session-Id` alone.** Enforce `Origin` presence for browser clients (reject absent, not just mismatched), and bind non-browser clients via a signed request or session-scoped token with a nonce. The `Mcp-Session-Id` remains the routing key; it is never the authenticator. ```python import hashlib import hmac import time from dataclasses import dataclass, field class SessionReplayError(RuntimeError): """Raised when a request cannot be proven fresh within its MCP session.""" @dataclass class SessionBinding: session_id: str principal: str # bound at initialize, not carried in header issued_at: float seen_nonces: set = field(default_factory=set) last_counter: int = -1 ttl_seconds: float = 300.0 def authorize_request( session: SessionBinding, presented_session_id: str, presented_principal: str, origin: str | None, nonce: str | None, counter: int | None, body: bytes, signing_key: bytes, signature: bytes | None, is_browser_client: bool, now: float | None = None, ) -> None: now = now if now is not None else time.time() # 1. Session ID is a routing key only; it must match a live session. if not hmac.compare_digest(presented_session_id, session.session_id): raise SessionReplayError("session id does not match a live session") # 2. TTL bounds the replay window even if a nonce leaks. if now - session.issued_at > session.ttl_seconds: raise SessionReplayError("session binding expired; re-initialize required") # 3. Principal binding: the header alone is never sufficient. if not hmac.compare_digest(presented_principal, session.principal): raise SessionReplayError( f"principal mismatch: session bound to {session.principal!r}" ) # 4. Origin is REQUIRED for browser-shaped clients (absence is a failure, # not a pass). Non-browser clients must instead carry a signature. if is_browser_client: if origin is None: raise SessionReplayError( "missing Origin header from browser client; cannot bind request" ) expected = f"https://{session.principal}" if origin != expected: raise SessionReplayError(f"origin {origin!r} does not match binding") # 5. Freshness: either a monotonic counter or a one-shot nonce, plus a # signature over (session_id || nonce/counter || body) to prevent forgery. if signature is None: raise SessionReplayError("unsigned request; freshness unprovable") if counter is not None: if counter <= session.last_counter: raise SessionReplayError( f"replayed counter {counter} <= {session.last_counter}" ) material = f"{session.session_id}:{counter}".encode() + body elif nonce is not None: if nonce in session.seen_nonces: raise SessionReplayError(f"replayed nonce {nonce!r}") material = f"{session.session_id}:{nonce}".encode() + body else: raise SessionReplayError("no nonce or counter supplied; replay undetectable") expected_sig = hmac.new(signing_key, material, hashlib.sha256).digest() if not hmac.compare_digest(signature, expected_sig): raise SessionReplayError("request signature invalid; body or binding tampered") # Commit the freshness marker only after all checks pass, so a failed # request cannot poison the replay window for a legitimate retry. if counter is not None: session.last_counter = counter else: session.seen_nonces.add(nonce) ```