Full answer
Why it works
- The server sits inside a trusted network. A "fetch this URL" feature (webhooks, image import, link previews, PDF generators) lets the user aim the server at internal addresses. In cloud environments the metadata endpoint can hand out credentials.
Bypass tricks to expect
- Alternate IP spellings: http://2130706433/ (decimal), http://0x7f.1/, http://127.1/, IPv6 ::1, IPv4-mapped ::ffff:127.0.0.1.
- A public name that resolves to a private IP, or a name that resolves to a public IP first and a private one later (DNS rebinding).
- Redirects from an allowed URL to an internal one. Different parsers (validation and request libraries) reading the same URL differently.
Defences (use several)
- Prefer an allowlist of hosts or domains. If users must supply arbitrary URLs, use a blocklist of address ranges as a second step.
- Parse the URL with the standard URL class. Allow http and https only, and disallow credentials in the URL and unusual ports.
- Resolve the hostname, check every resolved address against blocked ranges (10/8, 172.16/12, 192.168/16, 127/8, 169.254/16, 0/8, ::1, fc00::/7, fe80::/10, and mapped IPv4), then connect to that checked IP (pin it) with the original Host header, so a second resolution cannot change the target.
- Re-validate on each redirect, or disable redirects.
- Set timeouts and a response size limit, and do not return raw responses to users.
- Egress firewall rules, a separate network zone or proxy for outgoing fetches, and metadata service hardening (IMDSv2 needs a header and a hop limit).
const net = require('node:net');
const blocked = new net.BlockList();
for (const [addr, bits] of [['0.0.0.0', 8], ['10.0.0.0', 8], ['100.64.0.0', 10], ['127.0.0.0', 8], ['169.254.0.0', 16], ['172.16.0.0', 12], ['192.168.0.0', 16]]) blocked.addSubnet(addr, bits, 'ipv4');
blocked.addAddress('::1', 'ipv6'); blocked.addSubnet('fc00::', 7, 'ipv6'); blocked.addSubnet('fe80::', 10, 'ipv6');
function isBlockedIp(ip) {
const mapped = /^::ffff:(\d+\.\d+\.\d+\.\d+)$/i.exec(ip);
if (mapped) return blocked.check(mapped[1], 'ipv4');
const hexMapped = /^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i.exec(ip); // the URL class writes mapped addresses in hex
if (hexMapped) { const n = (parseInt(hexMapped[1], 16) << 16) | parseInt(hexMapped[2], 16); return blocked.check([n >>> 24, (n >> 16) & 255, (n >> 8) & 255, n & 255].join('.'), 'ipv4'); }
return blocked.check(ip, net.isIPv6(ip) ? 'ipv6' : 'ipv4');
}
// resolve is injectable so the demo needs no network. A real version would use dns.promises.lookup(host, { all: true }).
async function checkUrl(input, resolve) {
let url; try { url = new URL(input); } catch { return 'rejected: not a URL'; }
if (!['http:', 'https:'].includes(url.protocol)) return 'rejected: scheme ' + url.protocol;
if (url.username || url.password) return 'rejected: credentials in URL';
const host = url.hostname.replace(/^\[|\]$/g, '');
const addresses = net.isIP(host) ? [host] : await resolve(host);
for (const ip of addresses) if (isBlockedIp(ip)) return `rejected: ${host} -> ${ip} is internal`;
return `allowed: connect to ${addresses[0]} (pinned)`;
}
const fakeDns = { 'cdn.example.com': ['93.184.216.34'], 'localhost': ['127.0.0.1'], 'internal.corp.example': ['10.1.2.3'], 'sneaky.example.com': ['93.184.216.34', '169.254.169.254'] };
const resolve = async (host) => fakeDns[host] ?? [];
(async () => {
for (const u of ['https://cdn.example.com/logo.png', 'http://localhost:8080/admin', 'http://169.254.169.254/latest/meta-data/', 'http://2130706433/', 'http://0x7f.1/',
'http://[::1]/', 'http://[::ffff:127.0.0.1]/', 'http://internal.corp.example/', 'http://sneaky.example.com/', 'file:///etc/passwd', 'http://user:pw@cdn.example.com/', 'http://192.168.1.10:9200/'])
console.log(u.padEnd(46), await checkUrl(u, resolve));
// DNS rebinding: a second lookup returns a different answer than the one that was checked.
let calls = 0;
const rebinding = async () => (++calls === 1 ? ['93.184.216.34'] : ['127.0.0.1']);
console.log('\nrebinding host, validated lookup :', await checkUrl('http://rebind.example.com/', rebinding));
console.log('a second lookup at connect time :', (await rebinding())[0], '(so connect to the validated IP, do not resolve again)');
})();Address checks use Node's net.BlockList, and DNS is replaced by a fake table so no network is needed. A real fetch adds the pinned connection, redirects, timeouts and a size limit. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): https://cdn.example.com/logo.png allowed: connect to 93.184.216.34 (pinned) http://localhost:8080/admin rejected: localhost -> 127.0.0.1 is internal http://169.254.169.254/latest/meta-data/ rejected: 169.254.169.254 -> 169.254.169.254 is internal http://2130706433/ rejected: 127.0.0.1 -> 127.0.0.1 is internal http://0x7f.1/ rejected: 127.0.0.1 -> 127.0.0.1 is internal http://[::1]/ rejected: ::1 -> ::1 is internal http://[::ffff:127.0.0.1]/ rejected: ::ffff:7f00:1 -> ::ffff:7f00:1 is internal http://internal.corp.example/ rejected: internal.corp.example -> 10.1.2.3 is internal http://sneaky.example.com/ rejected: sneaky.example.com -> 169.254.169.254 is internal file:///etc/passwd rejected: scheme file: http://user:pw@cdn.example.com/ rejected: credentials in URL http://192.168.1.10:9200/ rejected: 192.168.1.10 -> 192.168.1.10 is internal rebinding host, validated lookup : allowed: connect to 93.184.216.34 (pinned) a second lookup at connect time : 127.0.0.1 (so connect to the validated IP, do not resolve again)
Key points
- SSRF aims your server at internal or metadata addresses.
- Check every resolved IP, including IPv6 and mapped forms.
- Connect to the checked IP to prevent DNS rebinding.
- Handle redirects, timeouts and size limits.
- Add network-level controls as well.
Common mistakes
- Blocklisting a few strings such as 'localhost'.
- Checking the hostname but not the resolved IP.
- Resolving the name twice (check, then connect).
- Following redirects to internal addresses.
- Returning fetched content or errors that reveal internal details.
Follow-ups
- How does IMDSv2 reduce the SSRF risk?
- How would you build a safe link preview service?
- Why is an allowlist better than a blocklist?
Tips
- Start with a cloud metadata example.
- List the bypass tricks.
- Say 'check the IP and connect to that IP'.