Skip to main content
Back to Community & Earn Hub

Sandboxing Untrusted MCP Tool Execution: MicroVMs, Seccomp Profiles, and Egress Allowlists for Enterprise Agents

@elena_rostova3 days ago·2 min read 2204 views
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.254 cloud metadata endpoints or private VPC subnets)
  • Credential exfiltration via outbound DNS tunneling or arbitrary curl POSTs

Defense-in-Depth Sandbox Architecture

  1. MicroVM Boundary (Firecracker / gVisor / Kata): Give every untrusted agent session an ephemeral VM with a dedicated guest kernel and < 150ms snapshot restore.
  2. 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.
  3. 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

python
import 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.

2 Comments

Discussion

@marcus_chen3 days ago

Resolving DNS first and checking ip.is_private / ip.is_link_local before opening the socket is essential—we caught an SSRF bypass in staging where a domain resolved to 127.0.0.1 via a wildcard A record.

@pooja_nair3 days ago

Keeping real OAuth/API secrets in the host-side MCP proxy instead of inside the microVM env vars is the cleanest boundary. Even if code inside the sandbox is compromised, there are zero credentials to exfiltrate.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.