Defeating SSRF and DNS Rebinding When Building a Live TLS Certificate Inspector
Cybersecurity · 8 min
Any endpoint that opens a socket to a user-supplied hostname is an SSRF primitive by default. Here is how I built /api/cert/fetch to inspect live TLS chains while blocking IPv4/IPv6 transition ranges and DNS-rebinding TOCTOU windows.
When I upgraded the SSL/TLS Certificate Assistant (ADR-010, ADR-023) so visitors could type any public domain (like example.com or expired.badssl.com) and inspect its live X.509 certificate chain and security grade, I had to solve a classic security problem: a server endpoint that connects to a user-supplied host is a Server-Side Request Forgery (SSRF) primitive by construction.
On Google Cloud Run, an naive tls.connect({ host, port }) would allow an attacker to probe the GCP instance metadata server (169.254.169.254), internal RFC 1918 VPC endpoints, or localhost ports. Worse, simply resolving the hostname first and checking the IP before calling tls.connect({ host }) leaves a Time-of-Check to Time-of-Use (TOCTOU) DNS rebinding window.
Here is the five-layer defense in server/certFetch.ts.
1. Synchronous Hostname & Port Policy Checks (Zero Network Activity)
Before touching DNS, assertPublicHost() and assertAllowedPort() enforce strict syntactic rules:
- No IP literals:
net.isIP(host)must be0. Raw IPv4 or IPv6 literals (and bracketed[::1]) are rejected immediately. - No single-label or internal suffixes:
localhost,.localhost,.local,*.internal, andmetadata.google.internalare blocked. - HTTPS Port Allowlist: Only ports
443and8443are permitted—blocking port-scanning of SSH (22), SMTP (25), or internal databases.
2. Comprehensive IPv4 + IPv6 Transition-Mechanism Denylist
Many SSRF filters check 10.0.0.0/8, 127.0.0.0/8, 169.254.0.0/16, 172.16.0.0/12, and 192.168.0.0/16, and stop there. Attackers bypass naive filters using IPv6 transition notations that embed a private IPv4 address inside an IPv6 address.
My isDisallowedIp() parser extracts embedded IPv4 octets from every RFC transition mechanism and validates them against the IPv4 denylist:
- IPv4-mapped IPv6 (
::ffff:169.254.169.254and hex form::ffff:a9fe:a9fe) - IPv4-compatible IPv6 (
::127.0.0.1) - NAT64 well-known prefix (
64:ff9b::/96and64:ff9b:1::/48, RFC 6052 / RFC 8215) - 6to4 (
2002::/16, RFC 3056, where bits 16–47 hold an IPv4 address) - Teredo (
2001:0000::/32, RFC 4380, where the client IPv4 is XOR-obfuscated with0xffffffffin the low 32 bits)
// Teredo (2001:0000::/32, RFC 4380): client IPv4 is in the low 32 bits, XOR'd with 0xffffffff
if (groups[0] === 0x2001 && groups[1] === 0x0000) {
const hi = groups[6] ^ 0xffff;
const lo = groups[7] ^ 0xffff;
const embeddedV4 = `${hi >> 8}.${hi & 0xff}.${lo >> 8}.${lo & 0xff}`;
if (isDisallowedIpv4(embeddedV4)) return true;
}
3. All-Record DNS Resolution + IP-Pinned TLS Handshake (No DNS Rebinding)
To eliminate DNS rebinding:
resolvePublicHost(host)callsdns.lookup(host, { all: true, verbatim: true })and checks every returnedAandAAAArecord againstisDisallowedAddress(). If even one record in a round-robin set points to a private IP, the entire request is rejected.fetchLiveCertificate(host, port, resolvedIp)connects the TCP socket directly toresolvedIpwhile settingservername: hostfor TLS SNI:
const socket = tls.connect({
host: resolvedIp, // Connect to the vetted IP — never re-resolve DNS!
port,
servername: host, // Send the original hostname in the TLS SNI extension
rejectUnauthorized: false, // Allow inspecting expired/self-signed certs (e.g. badssl.com)
});
Because tls.connect dials the vetted IP literal directly, socket.destroy() is called the instant the TLS handshake finishes—zero application bytes are ever sent over the connection, and the leaf + intermediate chain is parsed in memory via Node's native crypto.X509Certificate.