SSRF

Your server will fetch anything, from anywhere it can reach

L320 MINSECURITY
PREDICT

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.

▶ SIMULATION
USER
public preview: —
LINK-PREVIEW FETCHER
guard: none: fetch whatever the user sends
metadata service: IMDSv1 allowed
internal responses returned: 0 · attacks blocked: 0
ATTACKER
holds: nothing
last—
✓ no response from an internal address goes back to the user✓ ordinary public previews still work
SCHEDULE
01[APP]deploy: guard = none: fetch whatever the user sends; metadata service answers IMDSv1 GETs
02[USER]user previews https://news.example/story
03[ATTACKER]attacker previews http://169.254.169.254/latest/meta-data/iam/security-credentials/app-role
04[APP]report
Start of no guard scenario. 4 steps.
// 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

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.

BREAK IT NOT YET

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

FIX IT: THE ADDRESS CHECK NOT YET

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.

FIX IT: THE FETCH GUARD NOT YET

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).

Mental model:

A URL is a request to act from inside your network, so judge where the bytes will actually go, not what the string says. The only address that matters is the one you connect to: resolve once, check that address, connect to that address, and do it again on every hop. IMDSv2 and an egress proxy are the net under the trapeze, not the act.

IN PRODUCTION

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.

IN INTERVIEWS

“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.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: TENANT ISOLATION →