Threat model, precisely
Escape 1, rebinding into the loopback. A victim visits attacker.example. The first DNS answer points at the attacker's server and serves the page; a second answer, with a short TTL, points at 127.0.0.1. The page's fetch/XHR/WebSocket then reaches the local tool server while the browser treats it as same-origin to attacker.example. Even without readable responses (CORS), state-changing GETs and simple-request side effects land on your daemon.
Escape 2, egress into metadata. The tool server itself fetches user-supplied URLs (document retrieval, webhook verification, icon scraping). Any URL acceptance is an SSRF primitive. The crown jewels are the instance-metadata endpoints 169.254.169.254 and its IPv6 kin, which hand out workload credentials to anything that can reach them. Vendor mitigations like required metadata headers reduce exposure but are not a boundary you control; block the route yourself.
Why string checks on hostnames fail
Every denylist written against 127.0.0.1 or localhost as text is bypassable by spelling the same address differently. The parser in your OS resolves all of these to the loopback:
| User-supplied input | Normalizes to | Class |
|---|---|---|
127.1 | 127.0.0.1 | loopback (implicit zero octets) |
0x7f.1 | 127.0.0.1 | loopback (hex component) |
0177.0.0.1 | 127.0.0.1 | loopback (octal component) |
2130706433 | 127.0.0.1 | loopback (packed uint32) |
127.000.000.001 | 127.0.0.1 | loopback (zero-padded) |
[::ffff:127.0.0.1] | 127.0.0.1 (v4-mapped) | loopback after mapping normalization |
[::1] | ::1 | loopback (IPv6) |
169.254.169.254 | 169.254.169.254 | link-local metadata, block |
fd00:ec2::254 | fd00:ec2::254 | ULA metadata, block |
metadata.google.internal | 169.254.169.254 | resolves into blocked range only after resolution |
0.0.0.0, 0.1.2.3 | 0.0.0.0/8 | unspecified, block |
The last two rows are the whole argument for post-resolution validation: hostname-based denylists cannot see what the resolver will do, and wildcards like *.internal are unenforceable at the public-suffix scale.
Post-resolution socket pinning
The core race in naive SSRF filters is check-then-connect: validate hostname, then hand hostname to the HTTP client, which resolves it again. Between the two resolutions an attacker with DNS control swaps the answer. The fix is structural, resolve, filter, and connect the socket to a literal yourself:
import dns from "node:dns/promises";
import net from "node:net";
import { isIP } from "node:net";
function classify(ip) {
// operate on parsed bytes, never on strings
if (isV4(ip)) {
const [a, b] = v4Octets(ip);
if (a === 127 || a === 0) return "deny"; // loopback, unspecified
if (a === 10 || (a === 172 && b >= 16 && b <= 31)) return "deny"; // RFC1918
if (a === 192 && b === 168) return "deny";
if (a === 169 && b === 254) return "deny"; // link-local + IMDS
if (a === 100 && b >= 64 && b <= 127) return "deny"; // CGNAT
if (a >= 224) return "deny"; // multicast/reserved
return "allow";
}
const v6 = normalizeV6(ip);
if (v6 === "::1" || v6 === "::") return "deny";
if (isV4Mapped(ip)) return classify(v4Of(ip)); // ::ffff:0:0/96 -> re-check as v4
if (v6.startsWith("fe8") || v6.startsWith("fe9") ||
v6.startsWith("fea") || v6.startsWith("feb")) return "deny"; // link-local fe80::/10
if (v6.startsWith("f") ) return "deny"; // unique-local fc00::/7
return "allow";
}
async function resolvePinned(hostname) {
const addrs = await dns.lookup(hostname, { all: true, verbatim: true });
const ok = addrs.map(a => a.address).filter(ip => classify(ip) === "allow");
if (ok.length === 0) throw new Error(`egress denied: ${hostname} resolves only to blocked ranges`);
return ok[0]; // pin: the socket below connects to THIS literal, nothing re-resolves
}
function connectPinned(ip, port) {
// Explicit family + explicit literal: no Happy-Eyeballs re-resolution,
// no second DNS query between validation and connect.
return net.connect({ host: ip, port, family: isIP(ip), autoSelectFamily: false });
}
Three details carry the security weight:
- Resolve with
{ all: true }and classify every answer. A hostname with one public A record and one loopback AAAA record must fail, not pass on the first hit. - Disable Happy Eyeballs (
autoSelectFamily: falsein Node ≥ 20). The v4/v6 racing logic re-resolves and re-orders addresses per attempt, a second rebinding window you do not need. - Normalize v4-mapped IPv6 first (
::ffff:127.0.0.1→127.0.0.1) before range checks, then re-run the v4 classifier on the extracted address. Dual-stack sockets make mapped literals reachable on most platforms.
Server-side hardening for the local daemon
| Control | Implementation | Defends against |
|---|---|---|
| Bind explicitly | Listen on 127.0.0.1, never 0.0.0.0; check the actual bind with ss -tlnp | LAN exposure of a debug surface |
| Host/Origin allowlist | Reject requests whose Host header is not localhost:<port> / 127.0.0.1:<port>; reject Origin values outright | Rebound browser requests (their Host is the attacker's domain) |
| Token handshake | Server generates a per-session token; client must present it on first request | Any cross-origin request that survives header checks |
| Egress guard on all fetches | Route every outbound request through the pinned resolver above; deny by default | SSRF into IMDS, internal services, neighbor containers |
Testing the guard
The test corpus for our runner is literally the normalization table above plus live rebinding simulations: a local authoritative DNS server that flips answers between a public IP and 127.0.0.1 on a short TTL, asserted to produce a denied connection 100% of the time across 1,000 trials. A guard that passes a static unit test but races on the second resolution will still pass 90% of such trials, which is exactly why the socket, not the filter, must be pinned.
Status
The classifier, pinned-connect helpers, and Host-allowlist middleware are being packaged as Silvauren/mcp-egress-guard for Node-based local tool servers. If your tool server accepts URLs from model output, treat this as table stakes.