Sandboxing Untrusted MCP Tool Execution: MicroVMs, Seccomp Profiles, and Egress Allowlists for Enterprise Agents
Why Standard Docker Containers Aren't Enough for Agent Sandboxes
When an MCP server exposes run_python, execute_sql, or fetch_url, indirect prompt injection inside a retrieved PDF or webpage can trick the model into running arbitrary commands.
Running those tools inside a plain Docker container sharing the host kernel leaves you exposed to:
- Container escape / kernel syscall exploits
- Internal SSRF (
169.254.169.254cloud metadata endpoints or private VPC subnets) - Credential exfiltration via outbound DNS tunneling or arbitrary
curlPOSTs
Defense-in-Depth Sandbox Architecture
- MicroVM Boundary (Firecracker / gVisor / Kata): Give every untrusted agent session an ephemeral VM with a dedicated guest kernel and
< 150mssnapshot restore. - Strict Egress Proxy Allowlist: Drop all direct outbound packets (
iptables/ Cilium) and force HTTP(S) through an egress proxy that only permits approved domains (pypi.org,api.github.com) while blocking RFC1918 and link-local ranges. - Ephemeral Scoped Tokens: Never mount long-lived API keys inside the sandbox env; inject short-lived OAuth tokens via a host-side MCP proxy only when an approved tool call executes.
Minimal Python SSRF + Egress Guard for MCP Fetch Tools
pythonimport ipaddress import socket from urllib.parse import urlparse ALLOWED_HOSTS = {"api.github.com", "docs.python.org", "pypi.org"} def validate_outbound_url(raw_url: str) -> str: parsed = urlparse(raw_url) if parsed.scheme not in {"http", "https"} or not parsed.hostname: raise ValueError("Invalid URL scheme or host") host = parsed.hostname.lower() if host not in ALLOWED_HOSTS: raise PermissionError(f"Egress denied for host: {host}") for _, _, _, _, sockaddr in socket.getaddrinfo(host, None): ip = ipaddress.ip_address(sockaddr[0]) if ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved: raise PermissionError(f"Blocked internal IP resolution: {ip}") return raw_url
Takeaway
Treat every agent-invoked code runner as untrusted multi-tenant compute. Isolate the kernel, lock down egress DNS/IPs, and keep real secrets outside the sandbox boundary.