SSRF
Your server will fetch anything, from anywhere it can reach
Your link-preview feature resolves the user's hostname, checks that the address is public, and then calls fetch(url). The attacker's domain has a DNS TTL of 0. Can the attacker make your server fetch http://169.254.169.254/?
Server-side request forgery is any feature where your server fetches a URL the user chose: a link preview, an “import from URL”, a webhook test. Your server sits inside the network. It can reach the cloud metadata service at 169.254.169.254, which hands the instance’s IAM credentials to anyone who asks over plain HTTP (IMDSv1), and it can reach internal admin services nobody put behind a login.
The fix checks the address, not the string. Resolve the name once, check the resolved address against private, loopback and link-local ranges, connect to that exact address, and refuse redirects or check every hop. Behind that, an egress proxy that applies the same rule, and IMDSv2 tokens on every instance.
// Link preview: fetch whatever the user pasted
app.post('/preview', async (req, res) => {
const r = await fetch(req.body.url); // resolves the name itself, follows redirects
res.json({ title: titleOf(await r.text()) });
});
// 'Fixed' with a check, then a second, independent lookup
const { address } = await dns.lookup(new URL(url).hostname);
if (!isAllowed(address)) throw new Error('blocked');
await fetch(url); // looks the name up again: rebinding// Resolve once, check that address, connect to that address, re-check every hop
async function safeFetch(url, hops = 0) {
if (hops > 3) throw new Error('too many redirects');
const u = new URL(url); // canonicalises 2852039166 -> 169.254.169.254
const { address } = await dns.lookup(u.hostname);
if (!isAllowed(address)) throw new Error(`blocked ${address}`);
const r = await request(u, { lookup: pinTo(address), redirect: 'manual' });
if (r.status >= 300 && r.status < 400) return safeFetch(new URL(r.headers.location, u).href, hops + 1);
return r;
}
// Defence in depth: send it through an egress proxy that applies the same rule,
// and require IMDSv2 tokens on every instance. Watch the trace on each row: every dns answer and every connect. Each naive guard checks one thing and the fetch connects to another. The invariant fails the moment an internal response goes back to the attacker.
This fetcher resolves the host, checks the address, and only then calls fetch(url). On a quiet day it works. Inject one attacker preview that gets an internal response past the check.
GOAL: an internal response goes back to the attacker
Write isAllowed(ip): given the address a lookup returned, return true only for a public address. Deny 0.0.0.0/8, 10/8, 100.64/10 (some clouds serve metadata on 100.100.100.200), 127/8, 169.254/16, 172.16/12, 192.168/16, ::, ::1, fc00::/7 (AWS serves IMDS on fd00:ec2::254), fe80::/10, 2002::/16 (6to4) and 64:ff9b::/96 (NAT64), which can wrap any IPv4 address, and IPv4-mapped IPv6 (::ffff:…) that wraps any of those, in dotted or hex form. Anything that is not a canonical dotted quad or IPv6 address must be denied: fail closed. The grader tries internal addresses in several spellings and public ones that must pass.
Write safeFetch(url, net). net.parse(url) gives { host, path } (or null) with the host canonicalised; net.resolve(host) does a fresh DNS lookup every call; net.isAllowed(ip) is a correct address check; net.connect(ip, host, path) connects to exactly that IP and returns { status, location?, body } without following redirects; net.fetch(url) is the usual library call that resolves on its own and follows redirects. Return the response, or null to refuse. The grader watches every connection: one to an internal address fails you, even if you drop the response. A public page that redirects to another public page should still work (following it, or returning the 3xx, both pass).
Link previews and unfurls, import-from-URL, webhook endpoints customers register (and your “send test event” button), image and PDF renderers that load remote assets, OAuth and OIDC discovery URLs, and any agent tool that fetches a page. On AWS, set the instance metadata option to require IMDSv2 tokens and keep the PUT response hop limit at 1 unless a container needs more. Put outbound fetches of user-supplied URLs through an egress proxy that resolves and checks addresses itself, so a guard someone forgot in one service is caught in one place.
“We let users register a webhook URL. What could go wrong?” Say SSRF, name the metadata service and internal admin ports, then walk the bypasses: alternative IP spellings, DNS rebinding between check and connect, and redirects. Give the guard (resolve once, check the resolved address, pin the connection, re-check redirects), then the layers: egress proxy, IMDSv2, least-privilege instance roles.
USED IN
Chapters that build on this lesson.
- Authentication and authorisation for a multi-tenant SaaS and its public APISecurity
- Cloud and infrastructure securitySecurity
- Kubernetes and edge security: clusters, containers, CDN and WAFSecurity
- Secure code review and AppSecSecurity
- Triaging researcher reports: running a disclosure programme day to daySecurity
- Security testing: fuzzing, scanners and abuse casesSoftware Testing and Quality