How does an instance detect confused-deputy tool invocation when MCP streamable-HTTP transport collapses the Origin/Authorization boundary

**Date of Record:** 2026 December 2 **Scope:** Applies to MCP servers exposed over streamable-HTTP (SSE + POST) where the server both (a) honors a `Host`/`Origin` allowlist for DNS-rebinding defense and (b) forwards caller-supplied `Authorization` or `Mcp-Session-Id` into downstream tool invocations. Does not apply to stdio transport, nor to servers that terminate auth at a gateway and pass a signed, audience-bound token to the tool layer.

Core Distinction

The failure is not SSRF in the classic sense (server fetches attacker URL) and not header smuggling in the classic sense (CL/TE desync). It is a **confused-deputy collapse**: the MCP server, acting as a deputy for an authenticated client, re-uses the *client's* credential context when it invokes an internal tool that itself makes an outbound HTTP call. Two independent hardening controls—the `Origin`/`Host` allowlist and the per-session bearer binding—are each correct in isolation but are evaluated against *different* request objects. The allowlist checks the inbound transport request; the credential binding is attached to the *tool invocation* derived from that request. When a reverse proxy or an MCP "gateway" rewrites `Host` but preserves `Authorization`, the two checks disagree about who the caller is. The deputy then invokes a privileged tool with a credential whose audience was never the tool's downstream target.

The non-obvious part: the server *passes* its own SSRF guard (the outbound URL is on the internal allowlist) and *passes* its own auth check (the bearer is valid for the MCP endpoint). Confused-deputy is the residual when both invariants hold but the *composition* of the two is unsound.

What Was Observed

A streamable-HTTP MCP server behind an ingress that terminates TLS and rewrites `Host: mcp.internal.example` → `Host: mcp.svc.cluster.local`. The server's rebinding guard compares `Origin` against a static allowlist and `Host` against a second allowlist. Both pass. The `Authorization: Bearer <client-token>` header is preserved verbatim by the ingress.

The client token is a user-scoped OIDC token with `aud: mcp.internal.example`. The server validates it with a JWKS lookup and checks `aud` against the *inbound* host it believes it is serving. Because the ingress rewrote `Host`, the server's own `aud` check is performed against `mcp.svc.cluster.local`, which the token does *not* contain—so the naive server either rejects (safe) or, in the observed configuration, falls back to a "trusted ingress" mode that skips `aud` validation entirely.

The server then handles a `tools/call` for `fetch_url`, a tool that performs an outbound HTTP GET. The tool layer builds an outbound request and forwards the *same* `Authorization` header "for end-to-end tracing," a common but unjustified pattern. The outbound target is `http://169.254.169.254/latest/meta-data/iam/security-credentials/` — the cloud metadata endpoint, which the server's SSRF allowlist permitted because it was configured to trust the link-local range for "internal health checks."

The metadata service returns credentials scoped to the *node instance role*, not the client. The tool returns them in the MCP `tools/call` response. The client—an LLM agent—now has a credential it was never issued.

The invariant that failed: **the credential presented to the deputy must be audience-bound to the deputy's downstream target, and the deputy must never forward a caller credential to a resource outside the caller's authorization domain.** Neither the SSRF allowlist nor the `Origin`/`Host` check encodes this. They are orthogonal controls that compose unsoundly.

Condition of Validity

The guardrail holds only when all three of the following are simultaneously true:

  1. **Audience binding is evaluated against the *pre-rewrite* host.** The server must validate `aud` against the value the client used to address it, not the value the ingress rewrote it to. If the ingress rewrites `Host`, the server must either (a) receive the original via `X-Forwarded-Host` and validate against that, or (b) reject any request whose `Host` differs from the `aud` claim. The observed config did neither.
  2. **The tool layer never inherits the caller's `Authorization` header.** Tool invocations must carry a *deputy-issued* credential scoped to the tool's downstream target, minted by the server after the caller's authorization is established. Forwarding the caller's bearer is only safe if the downstream target is in the caller's audience domain—which the server cannot prove without a second audience check.
  3. **The SSRF allowlist is disjoint from the credential domain.** Link-local (`169.254.0.0/16`), loopback, and RFC1918 ranges must be excluded from any tool that forwards caller credentials, regardless of "internal health check" exemptions. The exemption is the hole.

When any one fails, the deputy is confused: it acts with authority it was not granted, against a resource it was not authorized to reach, on behalf of a principal that did not consent.

Decision Boundary

The boundary is between **"the request is authenticated"** and **"the request is authorized to cause this specific tool to make this specific outbound call."** A server that conflates these is exploitable. The decision procedure an instance must run before every `tools/call` that triggers an outbound fetch:

The boundary is *not* "did the SSRF filter pass" and *not* "did the Origin check pass." It is "does the credential's audience match the resource the deputy is about to touch." Any server that skips this composition check is one ingress rewrite away from credential exfiltration.

Operational Effect

An agent operating against such a server cannot distinguish a legitimate `tools/call` response from an exfiltrated credential until it inspects the *content* of the response for shape (e.g., a JSON blob with `AccessKeyId`/`SecretAccessKey`/`Token` keys). The correct operational posture for an instance is:

The subtle failure for the *agent* (as opposed to the server) is that the exfiltrated credential arrives as a *successful* tool result. The agent's error-compounding loop has no reason to distrust it—the tool returned 200, the schema validated, the content is well-formed JSON. The invariant the agent must enforce is: **a tool result whose content matches a credential schema is never a valid input to a subsequent tool call, regardless of status code.** This is the agent-layer mirror of the server-layer confused-deputy check, and it is the only guardrail that survives a server that has already been compromised.

import re
from typing import Any

CREDENTIAL_SHAPE = re.compile(
    r'"(AccessKeyId|SecretAccessKey|SessionToken|Token)"\s*:'
)
LINK_LOCAL = ("169.254.", "127.", "10.", "192.168.", "172.16.", "172.17.",
              "172.18.", "172.19.", "172.2", "172.30.", "172.31.")


class ConfusedDeputyError(RuntimeError):
    """Raised when a tool invocation would act outside the caller's authority."""


def guard_tool_call(
    caller_aud: str,
    inbound_host: str,
    tool_target: str,
    forwards_caller_auth: bool,
    response_body: Any,
) -> None:
    if caller_aud != inbound_host:
        raise ConfusedDeputyError(
            f"audience/host mismatch: aud={caller_aud!r} host={inbound_host!r}; "
            "refusing to invoke deputy tool (possible ingress rewrite)"
        )
    if forwards_caller_auth and any(
        tool_target.startswith(p) for p in LINK_LOCAL
    ):
        raise ConfusedDeputyError(
            f"tool target {tool_target!r} is link-local/private and the tool "
            "forwards caller Authorization; refusing (SSRF+credential leak)"
        )
    if isinstance(response_body, str) and CREDENTIAL_SHAPE.search(response_body):
        raise ConfusedDeputyError(
            "tool response contains credential-shaped JSON; halting reasoning "
            "loop and quarantining session"
        )