Track 08 · Lesson 14 of 20Attacks and defenses

Track 08 · Attacks and defenses

Server-side request forgery (SSRF)

Lesson 14 of 20· 8 min

Server-side request forgery makes your server fetch a URL of the attacker’s choosing. Your server sits inside your network, so it can reach things the attacker can’t: cloud metadata, admin panels, databases on localhost.

Check the address, not the words#

A blocklist of strings fails fast: 2130706433 is 127.0.0.1 written as one number, [::ffff:7f00:1] is 127.0.0.1 wrapped in IPv6, and a public-looking name can resolve to 10.0.0.12. Resolve the hostname, then refuse private and special ranges:

  • private: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
  • loopback 127.0.0.0/8, and link-local 169.254.0.0/16, home of cloud metadata
  • multicast and reserved: 224.0.0.0/4, 240.0.0.0/4
  • IPv6 loopback, unique-local fc00::/7, link-local fe80::/10, and IPv4 hidden inside IPv6 (::ffff:…, 6to4 2002::/16)

Then connect to the IP you checked, not a name looked up again: DNS can answer differently the second time.

Every hop, and a size cap#

fetch follows redirects by itself. Check only the first URL and an attacker routes through a public short link into your network. Set redirect: "manual", check each Location the same way, and stop after a few hops. Cap the response size and rate-limit the endpoint, or it becomes a free download mirror on your bill.

Open relay and redirect hops

The same attack as a sequence

Open relay and redirect hops

Attacker

/api/proxy

your server

Allowed host

Hidden target

internal / any

Step 1 of 7 · Attacker → /api/proxy

?url=allowed/redir

The first check passes.

Open relay / open proxy:
An endpoint that fetches anything for anyone and streams it back. Attackers use it to hide their origin, launder requests, or burn our bandwidth with large files.
Redirect hops:
fetch() follows redirects automatically: A → 302 → B → 302 → C. Checking only the first or last URL lets an attacker route through a forbidden middle hop, or downgrade from https to http.
SSRF (server-side request forgery):
Making our server fetch a URL of the attacker's choosing. Because the server sits inside our network, it can reach things the attacker cannot: cloud metadata at 169.254.169.254, internal admin panels, localhost services.
Private and special IP ranges:
Some address ranges should never be fetched by a server-side URL feature: private (10/8, 192.168/16), loopback, link-local 169.254/16 (cloud metadata), multicast 224.0.0.0/4, reserved 240.0.0.0/4, and IPv6 tricks that embed an IPv4 address: 6to4 2002::/16, translated ::ffff:0:a.b.c.d, and deprecated site-local fec0::/10.
CSP sandbox and SVG:
SVG is an image format that can contain script. Served from our origin, an SVG can behave like an HTML page. Content-Security-Policy: sandbox on a response makes the browser treat it as an isolated, script-less document.

In a code review

MediumApp

Fetch-anything proxy or SSRF

Fix: Handle redirects manually and check scheme, host and resolved IP on every hop; block private and special ranges; cap size; rate limit; refuse SVG or add CSP: sandbox.

0 of 3 checked

Check yourself

Your blocklist rejects “localhost” and “127.0.0.1”. Which URL still reaches localhost?