SSRF: when your 'fetch this URL' feature attacks your own network
Webhooks, link previews and 'import from URL' let users make your server send requests, from inside your network. How server-side request forgery reaches cloud metadata and internal services, and the layered defence: egress boundaries, address validation that survives DNS tricks, redirects and IMDSv2.
Every “fetch this URL for me” feature is a request your server makes on a user’s behalf: webhooks, link previews, “import image from URL”, PDF and screenshot renderers, OAuth and OpenID discovery endpoints, RSS readers.
The catch is where your server makes that request from. It sits inside your network. It can reach things the user can’t: internal admin panels, other services’ private APIs, a database’s HTTP interface, and, in the cloud, the instance metadata service at 169.254.169.254, which hands out temporary credentials for the machine’s role. A URL feature lends that reach to every user.
That’s server-side request forgery (SSRF). It’s in the OWASP Top 10, and it was a key step in some very public cloud breaches, including the 2019 Capital One incident, where SSRF was used to obtain credentials from the metadata service. This post is how to build URL-fetching features that can’t be turned around like that.


Why “just block localhost” doesn’t work
The first fix everyone writes is a string check: reject URLs containing localhost or 127.0.0.1. Attackers have a long list of ways around it:
- Other spellings of the same address:
127.1,0177.0.0.1(octal),2130706433(decimal),[::1],[::ffff:127.0.0.1]. - Other internal ranges you forgot:
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16, link-local169.254.0.0/16, carrier-grade NAT100.64.0.0/10, IPv6 unique-localfc00::/7. - Hostnames that resolve internally:
metadata.internal, your service discovery names, or any public domain whose DNS record points at127.0.0.1. - DNS rebinding: a domain that resolves to a public IP when you check it, and to an internal IP a second later when you connect.
- Redirects: a perfectly public URL that answers
302 Location: http://169.254.169.254/….
So the check must be on the resolved IP address, at connection time, on every hop, and ideally enforced by the network rather than only by your code.
Layer 1: an egress boundary
The strongest defence is structural: send all user-directed fetches through an egress proxy that can only reach the public internet. The proxy runs in a network segment with no route to your private ranges, and its rules deny private, loopback and link-local destinations. Your application talks to the proxy; the proxy talks to the world.
This works even when the application code has a bug, because the application is no longer the thing deciding what’s reachable. It also gives you one place to log every outbound request a user caused, which is exactly what you want during an investigation.
If a proxy isn’t possible yet, run the fetching code as a separate service in a locked-down network segment. The principle is the same: the component that fetches arbitrary URLs shouldn’t be able to reach anything private.
Layer 2: validate the resolved address, and connect to it
Validate in code as well, as defence in depth and to give users a clear error. The important detail is to check the IP addresses at the moment of connection, so a DNS answer can’t change between the check and the connect. Apache HttpClient 5 lets you plug in your own DNS resolver for exactly this:
static boolean isBlocked(InetAddress a) {
if (a.isAnyLocalAddress() || a.isLoopbackAddress() || a.isLinkLocalAddress()
|| a.isSiteLocalAddress() || a.isMulticastAddress()) {
return true; // 0.0.0.0, 127/8, 169.254/16, 10/8, 172.16/12, 192.168/16…
}
byte[] b = a.getAddress();
if (b.length == 4) {
int first = b[0] & 0xFF, second = b[1] & 0xFF;
return first == 0 || (first == 100 && second >= 64 && second <= 127); // 0/8, 100.64/10
}
return (b[0] & 0xFE) == 0xFC; // fc00::/7, IPv6 unique-local
}
DnsResolver validating = new DnsResolver() {
@Override public InetAddress[] resolve(String host) throws UnknownHostException {
InetAddress[] addresses = SystemDefaultDnsResolver.INSTANCE.resolve(host);
for (InetAddress a : addresses) {
if (isBlocked(a)) throw new UnknownHostException("Destination not allowed: " + host);
}
return addresses; // the client connects to exactly these
}
@Override public String resolveCanonicalHostname(String host) throws UnknownHostException {
return SystemDefaultDnsResolver.INSTANCE.resolveCanonicalHostname(host);
}
};
CloseableHttpClient client = HttpClients.custom()
.setConnectionManager(PoolingHttpClientConnectionManagerBuilder.create()
.setDnsResolver(validating)
.build())
.disableRedirectHandling()
.build();
Because the client connects to the addresses the resolver returned, a rebinding domain gets checked on the answer actually used. (If you route through a proxy, the proxy does the resolving, so this check belongs in the proxy’s rules too.)
Also restrict the scheme to http and https. Libraries that accept file://, gopher:// or ftp:// turn SSRF into local file reads or raw protocol injection.
Layer 3: redirects are new requests
disableRedirectHandling() above is deliberate. An automatic redirect is a second request to a destination you haven’t checked. Either don’t follow redirects, or follow them yourself, a small fixed number of times, running every hop through the same validation.
Layer 4: harden the metadata service
On AWS, require IMDSv2 for instances. It only hands out credentials to callers that first obtain a session token with a PUT request carrying a special header, which a typical SSRF (a simple GET to a URL the attacker controls) can’t do. Setting the response hop limit to 1 also stops containers from reaching the host’s metadata through an extra network hop. Other clouds have equivalents, such as required headers on metadata requests.
And give machines least-privilege roles. If credentials do leak, they should open as little as possible.
Layer 5: return less
- Don’t return raw responses. A link preview needs a title, a description and an image URL, not the full body. Parse what you need and drop the rest.
- Don’t return detailed errors. “Connection refused” vs “timed out” vs “404” tells an attacker which internal hosts and ports exist, even when they can’t see responses (so-called blind SSRF). Return one generic failure.
- Limit size and time: a cap on response bytes and a short timeout, so the feature can’t be used to download huge files or hold connections open.
Where SSRF hides
| Feature | Why it’s a fetcher |
|---|---|
| Webhooks | Users register a URL; you POST to it, often with retries |
| Link previews and unfurling | You fetch whatever was pasted |
| Import from URL (images, CSV, calendars) | You download a user-chosen file |
| HTML or Markdown to PDF renderers | The renderer loads every <img> and <link> in the document |
| OAuth / OpenID / SAML metadata URLs | Configurable discovery endpoints |
| XML parsers with external entities enabled | <!ENTITY x SYSTEM "http://…"> is a fetch too |
The PDF renderer is the one teams most often miss: the user doesn’t supply a URL directly, but the HTML they control does.
Takeaways
- Any feature that fetches a user-supplied URL makes requests from inside your network.
- Put an egress boundary around the fetcher: a proxy or network segment that can only reach the public internet.
- Validate the resolved IP at connection time, not the URL string, and treat redirects as new requests.
- Require IMDSv2 (or your cloud’s equivalent) and keep machine roles least-privilege.
- Return parsed, minimal results and generic errors.