Part 5 of 17 · 3 min read
A URL supplied by a stranger must be checked, not trusted
Every address a hostile server hands your client -- for discovery, for metadata, for a redirect -- gets fetched from your network position, not theirs.
Think of a courier who will deliver to any address written on the parcel, no questions asked. If a stranger can write the address, they can have the courier walk straight into a building. The courier's employer would never otherwise let them enter it.
In plain words: server-side request forgery (SSRF) happens when a hostile party supplies a URL that your own client then fetches from your network position, and because the same IP address has many spellings (decimal, hex, IPv4-mapped IPv6) a guard must resolve a name to one canonical address with a real parser rather than hand-rolled string checks, then pin the connection to the exact address it just checked so a later re-lookup (DNS rebinding) can't swap in something internal.
A worked example. Against a simulated internal network (a metadata service, an admin panel, a local service), the unguarded client reaches the internal machine through seven different spellings of the same address: the plain decimal form, the decimal-encoded loopback 2130706433, the short hex form 0x7f.1, the IPv4-mapped IPv6 form, and a redirect to the metadata address, among others. The guarded client blocks every one of the seven, and still fetches an ordinary public address successfully. Separately, a guard that checks an address and then looks the name up again before connecting reaches the admin panel anyway; a guard that connects to the address it already checked (pinning) does not.
The key lines. From code/mini-auth/net.py:
# from code/mini-auth/net.py
ips = net.resolve(host)
loop = all(ipaddress.ip_address(i).is_loopback for i in ips)
if any(is_internal(i) for i in ips) and not (allow_loopback and loop):
raise Blocked(f"{host} resolves to an internal address ({ips[0]})")
if p.scheme == "http" and not (allow_loopback and loop):
raise Blocked("plain http is only allowed to loopback during development")
ip = ips[0] if pin else net.resolve(host)[0] # connect to the address we just checked, not to a second lookup
Read it as: the comment on the last line names the exact bug pinning fixes. The code resolves once, decides, and then connects to a second fresh lookup. That is precisely the gap where an address can change between the check and the connection. ips[0] reuses the already-checked value instead of asking the network again.
SSRF, DNS rebinding
Server-side request forgery is a hostile party supplying a URL that your own server or client then fetches from its own network position. DNS rebinding is a name that answers with a public address when checked and an internal one when connected to moments later.
Example: the guarded client blocking all seven spellings of the same internal address while still reaching an ordinary public one is SSRF defence working as intended -- it is blocking destinations, not blocking the request itself, which is why an honest fetch to a genuinely public address still succeeds.