SSRF (Server-Side Request Forgery) Explained and How to Prevent It
How SSRF lets attackers make your server fetch internal URLs and cloud metadata, bypass tricks (redirects, DNS rebinding, IP encodings) and layered defences.
Published December 6, 2026
Many features fetch URLs on the user's behalf: link previews, webhooks, "import from URL", PDF generators, image proxies. If the server fetches whatever URL it is given, an attacker can point it at internal services that are not exposed to the internet, such as admin panels, databases or the cloud metadata endpoint that hands out credentials. That is Server-Side Request Forgery.
The concept
The impact can be severe. On cloud VMs, the instance metadata service at 169.254.169.254 has historically exposed temporary credentials, and SSRF has been used to steal them in major breaches. Internal HTTP services often assume only trusted callers can reach them and skip authentication. Even port scanning the internal network through error messages and timing is possible.
Naive filters fail. Attackers bypass blocklists with alternate IP encodings (decimal 2130706433, octal, IPv6-mapped addresses), DNS names that resolve to internal IPs, DNS rebinding (a name resolves to a public IP at validation time and an internal one at fetch time), and redirects from an allowed URL to an internal one. Robust defences resolve DNS yourself, check every resolved address against an allow or block policy, pin the connection to the validated IP, re-validate each redirect, and add network-level controls.
Classic SSRF
Bypass techniques
| Bypass technique | Example | Defence |
|---|---|---|
| Alternate IP encodings | http://2130706433/, http://0x7f.1/ | Parse and canonicalise; reject non-canonical numeric hosts |
| DNS pointing inward | internal.evil.example → 10.0.0.5 | Resolve and check every address |
| DNS rebinding | Public IP at check, private at fetch | Pin connection to validated IP |
| Open redirect chain | Allowed URL 302 → internal URL | Follow redirects manually, re-check each hop |
| IPv6 tricks | [::ffff:127.0.0.1] | Normalise IPv6, block mapped/special ranges |
Layered defences
Practical examples
Check every resolved address (Python sketch)
Reject private, loopback, link-local and other special ranges.
import ipaddress, socket
from urllib.parse import urlsplit
def safe_target(url: str) -> tuple[str, list[str]]:
parts = urlsplit(url)
if parts.scheme not in ("http", "https") or parts.port not in (None, 80, 443):
raise ValueError("scheme/port not allowed")
infos = socket.getaddrinfo(parts.hostname, parts.port or (443 if parts.scheme == "https" else 80))
ips = sorted({info[4][0] for info in infos})
for ip in ips:
addr = ipaddress.ip_address(ip)
if not addr.is_global:
raise ValueError(f"blocked address {ip}")
return parts.hostname, ips # then connect ONLY to these IPs (pinning)Pin the connection with curl
Connect to the validated IP while keeping the Host/SNI.
curl --resolve example.com:443:93.184.215.14 --max-redirs 0 --max-time 8 https://example.com/pageRequire IMDSv2 on AWS
Session-token metadata requests are much harder to abuse via SSRF.
aws ec2 modify-instance-metadata-options --instance-id i-0123456789abcdef0 \
--http-tokens required --http-put-response-hop-limit 1Common mistakes
- Blocklisting "localhost" and "127.0.0.1" strings only.
- Validating the hostname but letting the HTTP client resolve DNS again later.
- Letting the HTTP library follow redirects automatically.
- Returning raw fetch errors and responses to the user.
Key takeaways
- SSRF makes your server request attacker-chosen internal URLs.
- Cloud metadata and internal admin services are prime targets.
- Resolve DNS yourself, check all IPs, pin connections, re-check redirects.
- Add network egress controls and IMDSv2.
FAQ
Is an allow-list of domains enough?
It is the strongest option when feasible, but still resolve and check IPs, since an allowed domain could point inward or redirect.
Do webhooks have SSRF risk?
Yes. Webhook delivery to user-supplied URLs must use the same protections.
Can SSRF happen without HTTP?
Yes, via other URL schemes (gopher, file, ftp) if the client supports them; restrict schemes to http and https.