Engineering Notes · Local Tool Security · 2026-11-12

Stopping DNS-Rebinding and IPv6-Mapped Localhost Escapes in Local Tool Servers.

Local-first tool servers, model-context servers, dev harnesses, CLI daemons, trust the loopback interface by default. Two escape classes break that trust: browser-driven DNS rebinding into 127.0.0.1, and server-side SSRF into cloud metadata. Both are defeated by the same discipline: normalize every address after resolution, then pin the socket to the validated literal.

Posted 2026-11-12 · 14 min read · Companion library: Silvauren/mcp-egress-guard (staging)

TL;DR Never validate a hostname and then let your HTTP client resolve it again. Resolve once, parse the result with a real IP library (not string prefixes), reject loopback/private/link-local/v4-mapped ranges in both address families, and connect the socket directly to the surviving literal. On the server side, enforce a Host/Origin allowlist and bind explicitly to 127.0.0.1.

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 inputNormalizes toClass
127.1127.0.0.1loopback (implicit zero octets)
0x7f.1127.0.0.1loopback (hex component)
0177.0.0.1127.0.0.1loopback (octal component)
2130706433127.0.0.1loopback (packed uint32)
127.000.000.001127.0.0.1loopback (zero-padded)
[::ffff:127.0.0.1]127.0.0.1 (v4-mapped)loopback after mapping normalization
[::1]::1loopback (IPv6)
169.254.169.254169.254.169.254link-local metadata, block
fd00:ec2::254fd00:ec2::254ULA metadata, block
metadata.google.internal169.254.169.254resolves into blocked range only after resolution
0.0.0.0, 0.1.2.30.0.0.0/8unspecified, 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:

Server-side hardening for the local daemon

ControlImplementationDefends against
Bind explicitlyListen on 127.0.0.1, never 0.0.0.0; check the actual bind with ss -tlnpLAN exposure of a debug surface
Host/Origin allowlistReject requests whose Host header is not localhost:<port> / 127.0.0.1:<port>; reject Origin values outrightRebound browser requests (their Host is the attacker's domain)
Token handshakeServer generates a per-session token; client must present it on first requestAny cross-origin request that survives header checks
Egress guard on all fetchesRoute every outbound request through the pinned resolver above; deny by defaultSSRF 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.