Under what conditions must an instance treat an MCP tool's declared egress policy as void because the sandbox resolves DNS before the policy predicate is evaluated
**Date of Record:** 2026 December 2 **Scope:** Applies to agentic runtimes that host untrusted MCP servers inside a network sandbox and enforce egress allow-lists per tool invocation. Does not apply to MCP servers executed in-process with the orchestrator (no network namespace boundary) or to purely local stdio tools with no socket capability.
Core Distinction
The failure mode is not "the sandbox leaked" and not "the allow-list was wrong." It is a **TOCTOU collapse between name resolution and policy evaluation**. An MCP server's tool manifest declares an egress policy (e.g., `allowed_hosts: ["api.vendor.example"]`). The runtime's egress proxy is expected to enforce that policy *at connect time*. But the sandbox's resolver runs first, inside the same untrusted process, and the resolution result is what the proxy later sees.
The invariant that is silently assumed is: *the policy predicate is evaluated against the same identifier the agent intended to reach*. When resolution happens inside the sandboxed process rather than in a trusted resolver shim, the identifier the proxy enforces on (`93.184.216.34`) is no longer the identifier the policy was written against (`api.vendor.example`). The policy is not violated in the sense of a bypass; it is **evaluated against a different subject than the one it was authored for**. This is a continuity invariant failure, not a perimeter breach.
What Was Observed
A tool declared:
egress: { allowed_hosts: ["api.vendor.example"], deny_private_ranges: true }
The runtime wrapped the MCP server in a network namespace with an egress proxy at `10.0.0.1:3128` and an iptables rule redirecting all TCP to the proxy. The proxy received a CONNECT to `93.184.216.34:443`. It checked the IP against `deny_private_ranges` (public — pass) and against the host allow-list. Because the allow-list was hostname-keyed and the request was IP-keyed, the proxy's fallback logic matched on *any* public IP that had a reverse-DNS PTR record — a permissive fallback that existed to support tools that connected by IP literal.
Trace of the failure:
- Tool calls `getaddrinfo("api.vendor.example")` inside the sandbox.
- Sandbox `resolv.conf` points at the runtime's trusted resolver — but the tool ships its own `libnss` shim that intercepts `getaddrinfo` and returns an attacker-controlled A record (`203.0.113.9`).
- Tool connects to `203.0.113.9:443`. iptables redirects to the proxy. The proxy sees only the IP.
- Proxy's IP-literal fallback path evaluates `deny_private_ranges` (pass) and skips hostname matching because no SNI/hostname was asserted at CONNECT.
- Egress succeeds to an attacker endpoint that was never in the allow-list.
The declared policy was never *violated*; it was *never reached*. The subject of evaluation had been substituted before the predicate ran.
A second, subtler variant: even without a malicious `libnss`, a tool that connects by IP literal (common for gRPC fallback, for hardcoded CDN edges, or for retry-after-resolve patterns) causes the same subject substitution. The runtime cannot distinguish "the agent resolved the allowed name and got this IP" from "the agent typed this IP." Both arrive at the proxy as bare IPs.
Condition of Validity
The egress policy is only sound when **all three** hold:
- **Resolution is trusted.** Name-to-address resolution is performed by a resolver the sandboxed process cannot intercept — either a sidecar resolver that the namespace routes to and the process cannot override via `nsswitch.conf`, `LD_PRELOAD`, or a bundled resolver library, or resolution performed *outside* the namespace (the proxy itself resolves).
- **The policy subject is asserted at connect.** The connection carries the original hostname (SNI for TLS, or `Host`/`:authority` for HTTP, or an explicit proxy CONNECT hostname) and the proxy enforces on *that* identifier, not on the resolved IP.
- **IP-literal connections are refused or explicitly policy-bound.** A bare-IP CONNECT must not fall through to a permissive branch. It must either be denied or matched against an explicit IP allow-list that the tool manifest declared separately.
If any one fails, the declared `allowed_hosts` is decorative. The runtime is enforcing a *different* policy than the one it recorded.
Decision Boundary
The runtime must decide, per invocation, whether to **enforce** or **reject-and-renegotiate** the declared policy. The boundary:
- If the MCP server's transport is TLS and the proxy terminates or inspects SNI → enforce on SNI. Sound.
- If the transport is plaintext (HTTP, raw TCP, gRPC without TLS) → there is no hostname assertion. The proxy can only see IP. **The declared hostname allow-list is void.** The runtime must reject the invocation unless the manifest also declared `allowed_ips` or the tool is renegotiated to TLS.
- If the tool connects by IP literal → same as above. Reject unless IP-literal is explicitly allowed.
- If the sandbox allows the process to influence resolution (`/etc/resolv.conf` writable, bundled resolver, `LD_PRELOAD` permitted) → the hostname allow-list is void regardless of transport, because the hostname the tool *believes* it is connecting to may not be the one the proxy sees.
The boundary is not "is the tool trusted." It is "**is the identifier the proxy enforces on the same identifier the policy was authored against, and can the untrusted process change it?**" If the answer is no or unknown, the policy is void and the invocation must fail closed — not fall back to IP-range checks.
Operational Effect
When this boundary is crossed silently, three downstream effects follow:
- **Audit divergence.** The runtime's egress log records `203.0.113.9` (or `93.184.216.34`), which reconciles against nothing in the tool manifest. Incident review concludes "the allow-list was bypassed," but the actual mechanism is subject substitution, so the fix (tighten the allow-list) does not close the hole.
- **Replay non-determinism.** A replay of the same tool call may resolve to a different IP (DNS rotation, attacker rotation), so the failure is intermittent and does not reproduce in the verification harness. The verifier sees "allowed" on replay and marks the original as a false positive.
- **Policy drift across MCP versions.** A tool that upgrades from plaintext to TLS mid-session changes whether the policy is enforceable, but the manifest's `allowed_hosts` field is unchanged. The runtime continues to *claim* enforcement it is no longer performing.
The guardrail: the runtime must record, per egress decision, the **enforcement subject** (SNI, hostname, or IP) alongside the decision. Any decision whose subject is an IP while the manifest declared hostnames must be flagged as `policy_void` and surfaced to the orchestrator, not logged as a normal allow.
class EgressPolicyVoid(RuntimeError):
"""Raised when the sandbox cannot assert the policy subject at connect time."""
pass
def enforce_egress(manifest: dict, connect: dict) -> str:
"""
manifest: {"allowed_hosts": [...], "allowed_ips": [...], "transport": "tls"|"plain"}
connect: {"sni": str|None, "host_header": str|None, "dst_ip": str}
Returns the enforcement subject if the decision is sound.
Raises EgressPolicyVoid if the manifest's host allow-list cannot be enforced.
"""
allowed_hosts = set(manifest.get("allowed_hosts", []))
allowed_ips = set(manifest.get("allowed_ips", []))
transport = manifest.get("transport")
# The only sound subject is one the untrusted process cannot substitute.
subject = connect.get("sni") or connect.get("host_header")
if subject is None:
# Plaintext or IP-literal connect: no hostname assertion reached the proxy.
# The hostname allow-list is void. Only an explicit IP allow-list can bind.
if connect["dst_ip"] in allowed_ips:
return f"ip:{connect['dst_ip']}"
raise EgressPolicyVoid(
f"host allow-list {sorted(allowed_hosts)} is unenforceable: "
f"connect to {connect['dst_ip']} carried no SNI/Host and no matching "
f"allowed_ips entry; resolution may have been substituted in-sandbox"
)
if subject not in allowed_hosts:
# Subject is asserted but not permitted.
raise EgressPolicyVoid(
f"asserted subject {subject!r} not in allow-list {sorted(allowed_hosts)}"
)
if transport != "tls" and connect.get("sni") is None:
# Host header on plaintext is attacker-controlled; not a sound subject.
raise EgressPolicyVoid(
f"plaintext Host {subject!r} is not a trustworthy subject; "
f"require TLS SNI or an explicit allowed_ips entry"
)
return f"host:{subject}"