Skip to content

Zero-Trust MCP Gateways: Blocking 4,000 Unsafe Tool Calls with OAuth 2.1, SPIFFE mTLS & 2.5 ms Sandboxing

Repositories: github.com/cuhawk/MCPShield · github.com/cuhawk/MCP-VulnLab


Why Naive MCP Deployments Fail Under Real Attackers

The Model Context Protocol (MCP) solved tool-integration ergonomics, but most early deployments treat MCP servers as trusted local plugins. In practice, connecting an autonomous LLM agent to 50+ MCP tools exposes four compounding failure classes:

  1. Over-privileged token forwarding: Agents forward broad bearer tokens directly to downstream tools, turning any single prompt injection into full account takeover.
  2. Indirect prompt injection & cross-tool exfiltration (the "Lethal Trifecta"): An agent reads untrusted external data (search, fetch, read_file), gets hijacked mid-context, and invokes a write or network-egress tool carrying secrets from earlier turns.
  3. DNS rebinding, Origin spoofing & redirect credential leaks on Streamable HTTP: Localhost/reverse-proxy MCP listeners accept arbitrary Host/Origin headers or leak Authorization headers across cross-origin redirects.
  4. Schema bloat & tool-shadowing attacks: Dumping 60+ full JSON tool schemas into every turn wastes context tokens and widens the surface for unicode/metadata poisoning (OWASP ASI01–ASI10).

MCPShield is a default-deny inline proxy (stdio and Streamable HTTP across MCP 2025-03-26, 2025-06-18, and 2025-11-25) built to enforce strict capability boundaries before any JSON-RPC call reaches an upstream server.


Architecture

flowchart LR
    Agent[AI Agent / MCP Host] -->|JSON-RPC + OAuth 2.1 RS256| Gate[MCPShield Proxy]
    Gate -->|1. Host/Origin & Protocol Check| Policy[YAML/JSON Policy Engine<br/>Atomic Hot-Reload]
    Policy -->|2. Scope Attenuation & Tier Check| Taint[Taint & Canary Engine<br/>Blocks Write After Untrusted Read]
    Taint -->|3. BM25 Tool Pruning| Pruner[ToolPruner<br/>91.6% Schema Token Cut]
    Taint -->|4a. HTTPS + SPIFFE X.509-SVID mTLS| Upstream[Remote MCP Server]
    Taint -->|4b. Linux User-NS + Net-NS| Bwrap[Bubblewrap SandboxRunner<br/>RO /usr/bin · 16 MiB /work · 0 Egress]
    Gate -->|Redacted JSONL / OTel| Audit[(Audit Log)]

Key Design Decisions & Security Controls

  • Zero Credential Passthrough + OAuth 2.1 Attenuation: Incoming Authorization headers are never forwarded upstream. HTTP mode verifies RS256 signatures, iss, resource aud, expiry, and scope syntax (mcp:write, mcp:privileged), mapping every permitted tool to an explicit JSON Schema with additionalProperties: false.
  • Conservative Taint Context & Canary Traps: Tools marked untrusted_output: true taint the active context. Once an instructional injection pattern or synthetic canary (MCPSHIELD_CANARIES) triggers, write-tier and side-effect tools are hard-disabled for the remainder of that context.
  • Streamable HTTP & Upstream SPIFFE mTLS Hardening:
  • Rejects duplicate or unlisted Host and Origin authorities with HTTP 403.
  • Validates upstream TLS chains against a pinned trust bundle and asserts the exact --upstream-spiffe-id URI SAN before transmitting any MCP request.
  • Strips Authorization, Cookie, and X-API-Key on cross-origin redirects, blocks HTTPS-to-HTTP downgrades, and caps cumulative response/SSE reads at 1 MiB (--max-response-bytes).
  • Atomic Policy Hot-Reload: Polls policy files on an interval (--policy-reload-interval), validates replacement policies completely before atomic swap, keeps in-flight requests pinned to their starting snapshot, and preserves the last valid policy if an operator pushes a broken edit.
  • One-Shot Linux Bubblewrap Isolation (SandboxRunner): Launches executables under /usr/bin with a fresh environment, read-only system binaries/libraries, a private 16 MiB /work filesystem, and a private network namespace with zero direct egress.

Measured Benchmarks & Failure-Mode Honesty

All measurements come from the committed Python 3.12.14 Linux test & benchmark suite (pytest -q, ruff check ., python benchmarks/run_bench.py):

Metric Measured Result Methodology / Notes
Non-compliant requests blocked 4,000 / 4,000 (100%) Scope escalation, unlisted tools, invalid schemas, tainted writes, canary leaks
Compliant requests allowed 2,000 / 2,000 (100%) 0 false blocks across valid tool calls
Regression test suite 128 pytest passed Includes defensive regressions mapped to OWASP Agentic ASI01–ASI10
BM25 schema token reduction 91.6% 6 labelled queries over 60 tools, top-5 selection (compact JSON token estimate)
ToolPruner Recall@5 100.0% (12 / 12) BM25 + x-tags input schema boost; policy still enforced per call
Bubblewrap launch overhead (p50) 2.525 ms Paired runs against direct /usr/bin/true
Bubblewrap launch overhead (p95 / p99) 3.595 ms / 5.697 ms Includes private user-ns, mount-ns, and isolated net-ns setup

Explicit Scope Boundaries & Tradeoffs

  • Heuristic vs. cryptographic enforcement: Canary matching is textual and prompt-injection detection uses deterministic heuristics—meaning it catches known patterns cheaply without an extra LLM hop, while real containment comes from capability attenuation, default-deny write locks, and Bubblewrap network-namespace isolation.
  • Shared taint domain per instance: A single proxy instance intentionally maintains a conservative shared taint state; multi-tenant deployments should run one isolated proxy instance per trust domain.
  • Static pre-deployment counterpart: To catch tool-shadowing and schema bugs before runtime, MCP-VulnLab runs interprocedural Python & JS/TS static analysis (104 pytest, 74 fixtures at 100% precision/recall) and emits SARIF 2.1.0 directly into CI.

Reproduce in 60 Seconds

git clone https://github.com/cuhawk/MCPShield.git && cd MCPShield
pip install -e '.[test]'
ruff check . && pytest -q
python benchmarks/run_bench.py

Run an HTTP proxy authenticated to an upstream SPIFFE workload:

mcpshield proxy policy.yml \
  --listen 8000 \
  --upstream-url https://mcp.example.org/mcp \
  --upstream-ca trust-bundle.pem \
  --upstream-cert client-chain.pem \
  --upstream-key client-key.pem \
  --upstream-spiffe-id spiffe://example.org/mcp

View MCPShield on GitHub Try Interactive Security Demos