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.

● Medium🔒 PROBy ·Design problem·15 min read·Intermediate → Advanced
SSRFGonet.Dialer ControlDNS rebindingcloud metadatanetip

01Understand the problem

✓What you will learn
  • Explain why validating the hostname is not enough and the check must run on the dialled IP
  • Use Go's net.Dialer.Control hook and net/netip to 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
◎Interview radar

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.

  1. Only http and https, no userinfo, ports from an allow-list (80 and 443 by default).
  2. Every TCP connection, including those made for redirects, passes an address check in the dialer.
  3. Proxy settings from the environment are ignored.
  4. At most 3 redirects, each re-checked for scheme.
  5. Timeouts on dial, TLS, headers and the whole request; response bodies capped.
RangeWhat lives thereWhy block
127.0.0.0/8, ::1Local admin ports, sidecarsLoopback services trust local callers
10/8, 172.16/12, 192.168/16, fc00::/7Internal servicesPrivate networks
169.254.0.0/16, fe80::/10Cloud metadata (169.254.169.254)Credentials and instance identity
100.64.0.0/10Carrier-grade NAT, some cloud internalsNot public
0.0.0.0/8, ::'This host' on many stacksOften reaches localhost
64:ff9b::/96NAT64 to IPv4Embeds an IPv4 address, possibly private
::ffff:0:0/96IPv4-mapped IPv6Unmap 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.