跳到正文
DEV Community· Nexus Foundry·· 2 小時前AI 評分52

MCP 伺服器的 fetch_url 可能造成 SSRF,常見範本卻漏掉防護層

Your MCP server's fetch_url tool is an SSRF hole — the guard layer most templates skip

AI 導讀

Nexus Foundry 指出,MCP 伺服器的 fetch_url 可能成為 SSRF 入口,並提供最小化 stdio 範本,示範如何為 fetch_url 和 read_file 加入防護。

正文

Every "build an MCP server in 50 lines" tutorial ships the same two tools: fetch_url and read_file. Almost none of them guard them. That means the demo server you copied into your client config last weekend will happily fetch http://169.254.169.254/latest/meta-data/ on EC2, or read ../../.ssh/id_rsa if you pointed read_file at the wrong root.

I got burned enough by config footguns to start selling lint kits, so when MCP tooling took off I built the thing I actually wanted: a minimal stdio MCP server where the guard layer is the product, not an afterthought.

The two tools everyone ships, and what they actually expose

fetch_url looks harmless — it's a GET request. But an MCP client will call it with whatever URL the model produces, and models produce URLs from tool output, docs, and — increasingly — attacker-controlled content. That's a Server-Side Request Forgery primitive handed to an LLM:

  • http://127.0.0.1:8080/admin — internal services
  • http://169.254.169.254/ — cloud metadata credentials
  • http://[::1]/, 10.x, 192.168.x, link-local, multicast — the whole private range
  • file:///etc/passwd — scheme confusion if you don't whitelist

read_file is the quieter one: path traversal (../../), symlink escapes, and unbounded reads that will happily slurp a 4GB log into the model's context.

What the guard actually has to do

The naive fix — a regex blocklist of "localhost" and "127.0.0.1" — misses decimal IP notation (2130706433), hex (0x7f000001), IPv6 loopback, DNS names that resolve to private addresses, and every redirect target. The check that works resolves the hostname via getaddrinfo before connecting and validates every returned address against private/loopback/link-local/multicast/reserved ranges.

For read_file: resolve the real path, reject .. escapes, refuse symlinks that point outside the allowed root, cap file size.

The honest part: resolve-time ≠ connect-time

I want to be straight about the residual gap, because it's the one that matters in hostile environments: checking the resolved IP and then connecting are two different moments. A hostname can pass the check and re-resolve to a private IP before the connection opens — DNS rebinding. For most internal tooling the resolve-time check is enough; for anything facing adversarial input you pin egress or re-validate at connect time. Any vendor claiming "SSRF-proof" without mentioning this is selling something.

Proof, not promises

The thing I cared about most: the guards should be tested, not asserted. The kit ships a 5-case unittest suite — including one that stands up a real localhost HTTP server and proves fetch_url refuses to touch it. If you add tools, the suite shows you exactly what a guard test looks like.

What's inside:

  • server/mcp_server.py — dependency-free JSON-RPC 2.0 over stdio (Python 3.8+, no FastMCP, no Node)
  • tests/test_server.py — 5 tests, all passing
  • examples/client-config.json — Claude Desktop / Cursor stdio snippet
  • DEPLOY.md + Dockerfile — because "works on my machine" isn't a deployment story

It's a template, not audited infrastructure — tools you add inherit nothing automatically, and it covers initialize/ping/tools only (no resources, prompts, or sampling). $19, BTC or EVM checkout:

https://wesleygitari2-cloud.github.io/nexus-store-redirect/p/mcp-secure-server-kit

If you just want to poke at the guard behavior first, the test file is the fastest way — it demonstrates the SSRF block live without installing anything.

來源:DEV Community · dev.to