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

> Source: <https://dev.to/nexusfoundry/your-mcp-servers-fetchurl-tool-is-an-ssrf-hole-the-guard-layer-most-templates-skip-58nl>
> Published: 2026-11-22 12:09:16+00:00

**Recommendation:** Validate every resolved address at connect time and pin egress, because a resolve-time-only check leaves a DNS-rebinding gap between validation and connection.
More in the playbook "Securing agents and AI tools": <https://wpnews.pro/playbooks/agent-security>

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.

`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.

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.

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.

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](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.
