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

AttackerYour server (URL fetcher)Metadata service 169.254.169.254preview URL: http://169.254.169.254/latest/meta-data/…GET (from inside the network)temporary credentialspreview shows credentials
SSRF turns your server into a proxy into your network.

Bypass techniques

Bypass techniqueExampleDefence
Alternate IP encodingshttp://2130706433/, http://0x7f.1/Parse and canonicalise; reject non-canonical numeric hosts
DNS pointing inwardinternal.evil.example → 10.0.0.5Resolve and check every address
DNS rebindingPublic IP at check, private at fetchPin connection to validated IP
Open redirect chainAllowed URL 302 → internal URLFollow redirects manually, re-check each hop
IPv6 tricks[::ffff:127.0.0.1]Normalise IPv6, block mapped/special ranges

Layered defences

Applicationscheme/port allow-list, DNS resolve + IP checks, pinned connect, manual redirectsRuntimeegress proxy for outbound fetchesNetworkblock metadata IP and internal ranges from fetcher hostsCloudIMDSv2 (session tokens), least-privilege instance roles
Defence in depth against SSRF.

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/page

Require 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 1

Common 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.

More Security articles