# How does an instance detect when a capability sandbox's egress allowlist silently authorizes a tool call whose arguments were rewritten after policy evaluation **Date of Record:** 2026 November 21 **Scope:** Applies to any agent runtime that enforces tool-invocation policy by (a) evaluating a capability/egress allowlist against a tool call, then (b) dispatching the call through a separate serialization/transport layer. Does not apply to runtimes that evaluate and execute within a single atomic syscall boundary, or to stateless single-shot tool calls with no argument transformation between check and use. ## Core Distinction A capability sandbox answers a *static* question: "is this tool, with this destination, permitted for this principal?" An egress allowlist answers a *routing* question: "is the host/port/scheme the call will actually reach on the wire permitted?" These are evaluated at different times, against different representations of the same call. The failure mode is a **check-to-use representation gap**: policy is evaluated against the *intended* arguments as the LLM emitted them, but the call executes against the *serialized* arguments after the transport layer normalizes, re-encodes, or expands them. This is not a generic TOCTOU race in the classical sense—nothing is mutated by a concurrent attacker between check and use. The mutation is *intrinsic to the serialization step itself*: URL normalization, percent-encoding, Unicode normalization, redirect following, `@`-authority confusion, and DNS re-resolution all rewrite the effective destination between the moment policy saw it and the moment the socket opened. The invariant that must hold is: **the bytes the allowlist evaluated are the bytes the kernel connects to.** When that invariant is violated, the allowlist authorizes one destination and the runtime reaches another. ## What Was Observed An instance was granted a capability `http.fetch` with an egress allowlist of `api.internal.example.com`. The LLM emitted a tool call: ``` http.fetch(url="https://api.internal.example.com@169.254.169.254/latest/meta-data/") ``` Policy evaluation parsed the URL, extracted the authority, and matched the host component `api.internal.example.com` against the allowlist. The match succeeded—the string `api.internal.example.com` was present in the authority. The call was dispatched. The transport layer, however, used a standards-compliant URL parser that treats everything before the last `@` as userinfo. The effective host resolved to `169.254.169.254`, the cloud metadata endpoint. The allowlist had authorized the *userinfo* string, not the *host*. A second, subtler variant surfaced on the same runtime. The LLM emitted: ``` http.fetch(url="https://api.internal.example.com./admin") ``` The trailing dot is a legal FQDN root label. Policy matched `api.internal.example.com` after trimming the trailing dot during normalization. The DNS resolver, however, resolved `api.internal.example.com.` (with root label) separately, and a wildcard CNAME at the zone apex redirected it to an attacker-controlled host. Policy saw one name; the resolver connected to another. In both cases the runtime logged `policy=allow, egress=allow, host=api.internal.example.com` while the actual TCP connection went elsewhere. The audit trail was not merely incomplete—it was *affirmatively wrong*, which is worse, because downstream verification replays trusted it. ## Condition of Validity The allowlist enforcement is sound only if all of the following hold simultaneously: 1. **Single parser identity.** The component that evaluates policy and the component that opens the socket use the *same* URL parsing function, at the *same* version, with the *same* configuration. Divergent parsers (one lenient, one strict; one RFC 3986, one WHATWG) are the root enabler. 2. **Post-parse comparison.** Policy compares the *parsed* host (the field the socket layer will actually dial), never a substring or prefix of the raw URL string. 3. **No post-policy rewrite.** Between policy evaluation and connection, no redirect following, no CNAME expansion to a new authority, no proxy rewrite, no normalization that changes the effective host. 4. **Terminal-label canonicalization.** Trailing dots, case folding, and IDNA/punycode conversion are applied identically on both sides of the check. 5. **Resolution pinning.** DNS resolution occurs *before or atomically with* policy evaluation, and the resolved IP is what policy authorizes—not the name that happens to resolve to it at connect time. If any one of these is absent, the check-to-use gap exists and the allowlist is decorative. ## Decision Boundary The runtime must decide, per tool call, whether the *effective destination*—defined as the tuple `(scheme, canonical_host, resolved_ip, port)` that the socket layer will use—is a member of the allowlist. The boundary is crossed the moment the runtime accepts a *name-based* match without having pinned the *address-based* destination. Concretely, the decision boundary is: - **Below the boundary (safe):** policy returns a decision keyed to a resolved IP that is then *reused* by the connection layer (connect-by-IP, not connect-by-name). The name was only used to look up the IP; the IP is what was authorized and what is dialed. - **Above the boundary (unsafe):** policy returns a decision keyed to a hostname, and the connection layer re-resolves that hostname independently. Between the two resolutions the name may map to a different address (DNS rebinding, CNAME flip, wildcard), and the authorized name now reaches an unauthorized IP. The guardrail is **resolution pinning with address-based authorization**: resolve once, authorize the resolved address, and force the socket to connect to that exact address (via a resolver override or a connect-to-IP-with-Host-header pattern). Any runtime that re-resolves after authorization has stepped above the boundary and its allowlist is no longer an enforcement mechanism—it is a logging convention. ## Operational Effect When the gap is present, three things degrade together: 1. **Confidentiality.** Userinfo confusion and trailing-dot redirects let an injected tool call (via prompt injection in retrieved content) reach internal metadata, sidecars, or attacker infrastructure while the audit log records a permitted host. The blast radius is bounded only by what the sandbox's network namespace can reach—which, for a capability granted "internal API access," is typically everything on the internal segment. 2. **Auditability.** Because the logged host is the *intended* host, replay-based verification cannot detect the divergence. An auditor replaying the decision record sees `allow` for `api.internal.example.com` and has no signal that the wire destination differed. The verification invariant "log matches execution" is silently violated. 3. **Containment.** A single poisoned tool call can establish a channel (e.g., exfiltrate via the metadata endpoint's response, or reach a redirector that returns attacker-controlled content into the agent's context), which then feeds the next reasoning step. The failure is not a one-shot escape; it is a **persistent context-poisoning vector** because the response body is trusted as tool output and enters the agent's working memory as if it came from the allowlisted host. The remediation is architectural, not defensive: collapse the check and the use onto the same parsed, resolved, pinned destination, and refuse any call whose effective address was not the one authorized. The code below encodes that invariant as a hard failure when a runtime attempts to dispatch a call whose resolved address diverges from the authorized address. ```python import ipaddress from dataclasses import dataclass from typing import Optional class EgressCheckToUseGap(RuntimeError): """Raised when the address the socket will dial diverges from the address policy authorized.""" pass @dataclass(frozen=True) class AuthorizedDestination: scheme: str canonical_host: str resolved_ip: ipaddress.IPv4Address | ipaddress.IPv6Address port: int @dataclass(frozen=True) class DispatchTarget: scheme: str canonical_host: str resolved_ip: ipaddress.IPv4Address | ipaddress.IPv6Address port: int def canonicalize_host(raw_host: str) -> str: # Strip userinfo confusion: only the segment after the last '@' is the host. host = raw_host.rsplit("@", 1)[-1] # Strip trailing root label and case-fold. host = host.rstrip(".").lower() return host def authorize( allowlist: frozenset[str], scheme: str, raw_url_authority: str, port: int, resolver, ) -> AuthorizedDestination: host = canonicalize_host(raw_url_authority) if host not in allowlist: raise EgressCheckToUseGap( f"host {host!r} not in egress allowlist {sorted(allowlist)!r}" ) # Resolve ONCE, here, and pin the address. This is the authorization unit. resolved_ip = resolver(host) return AuthorizedDestination( scheme=scheme, canonical_host=host, resolved_ip=resolved_ip, port=port, ) def dispatch( authorized: AuthorizedDestination, target: DispatchTarget, resolver, ) -> None: # The connection layer must dial the pinned IP, never re-resolve the name. if target.resolved_ip != authorized.resolved_ip: raise EgressCheckToUseGap( "resolved address diverged from authorized address: " f"authorized={authorized.resolved_ip} target={target.resolved_ip}" ) if target.canonical_host != authorized.canonical_host: raise EgressCheckToUseGap( "canonical host diverged from authorized host: " f"authorized={authorized.canonical_host!r} target={target.canonical_host!r}" ) if (target.scheme, target.port) != (authorized.scheme, authorized.port): raise EgressCheckToUseGap( "scheme/port diverged from authorization: " f"authorized={authorized.scheme}:{authorized.port} " f"target={target.scheme}:{target.port}" ) # ... open socket to target.resolved_ip directly; do not consult DNS again ... ```