Design an SSRF-safe URL fetcher in Go
Design a Go HTTP client for user-supplied URLs (webhooks, link previews, imports) that checks the IP address it actually connects to, through net.Dialer's Control hook, so DNS rebinding, redirects, IPv4-mapped IPv6 and environment proxies cannot route it to internal services or cloud metadata.
01Understand the problem
- Explain why validating the hostname is not enough and the check must run on the dialled IP
- Use Go's
net.Dialer.Controlhook andnet/netipto enforce an address policy - Disable environment proxies, bound redirects, time and response size
- Cover the special ranges: loopback, private, link-local metadata, CGNAT, NAT64, IPv4-mapped IPv6
What they probe: the time-of-check to time-of-use gap between resolving and connecting (DNS rebinding), redirects to internal addresses, http://[::ffff:169.254.169.254]/, decimal IPs such as http://2130706433/, and what happens if HTTPS_PROXY is set. Where candidates slip: resolving the name, checking it, then letting the client resolve it again; deny-lists of hostname strings; following redirects without re-checking; forgetting IPv6.
Server-side request forgery turns your server into the attacker's proxy. A link preview that fetches http://169.254.169.254/latest/meta-data/iam/security-credentials/ on an AWS instance without IMDSv2 can return temporary cloud credentials; the 2019 Capital One breach began this way. The robust design does not try to judge URLs by their text. It lets the client resolve normally and checks every address at connect time, in the dialer, where there is no gap between check and use.
- Only
httpandhttps, no userinfo, ports from an allow-list (80 and 443 by default). - Every TCP connection, including those made for redirects, passes an address check in the dialer.
- Proxy settings from the environment are ignored.
- At most 3 redirects, each re-checked for scheme.
- Timeouts on dial, TLS, headers and the whole request; response bodies capped.
| Range | What lives there | Why block |
|---|---|---|
| 127.0.0.0/8, ::1 | Local admin ports, sidecars | Loopback services trust local callers |
| 10/8, 172.16/12, 192.168/16, fc00::/7 | Internal services | Private networks |
| 169.254.0.0/16, fe80::/10 | Cloud metadata (169.254.169.254) | Credentials and instance identity |
| 100.64.0.0/10 | Carrier-grade NAT, some cloud internals | Not public |
| 0.0.0.0/8, :: | 'This host' on many stacks | Often reaches localhost |
| 64:ff9b::/96 | NAT64 to IPv4 | Embeds an IPv4 address, possibly private |
| ::ffff:0:0/96 | IPv4-mapped IPv6 | Unmap and check the IPv4 inside |
This is an advanced module
Problems: parsing and sandboxing components is part of the Pro pass. 4 more sections — the deep dives, the final design, follow-ups and the test — unlock with it.
Already a cyart.io or studio.cyart.io member? Your pass code is in your Studio account.