Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท Integrity, Logging and SSRF

Defending Against SSRF

Prevent SSRF with allow-lists, resolved-IP validation, redirect handling, network egress rules and cloud metadata hardening, with a runnable validator stand-in.

SSRF cannot be fixed by one clever regular expression. It is fixed by layers: decide what the server is allowed to contact, validate the real destination, limit what comes back and restrict the network so mistakes are contained. This lesson goes through each layer, then builds a validator you can adapt.

The code never opens a connection. Hostnames are resolved from a small fictional table so the examples are deterministic. In production, resolve with your platform's resolver and connect to the exact address you validated.

Layer 1: do you need to fetch at all?

The best fix is removing the feature or narrowing it. Ask:

  • Can the user pick from a list instead of typing a URL (for example a provider name that maps to a fixed endpoint)?
  • Can the browser do the fetch instead of the server (for example, loading an image in an <img> tag)?
  • Can you accept an uploaded file instead of a remote URL?

Layer 2: an allow-list

If a user URL is unavoidable, use an allow-list of permitted destinations. Compare the parsed host against exact names, not substrings. Enforce:

  • Scheme https (and http only if required); reject file, gopher, ftp and others.
  • Allowed ports (usually 443).
  • Host in an approved list, or a domain you control.
  • No credentials in the URL (the user:pass@host part).

Block-lists of "bad" addresses are a supplement, never the primary control.

Layer 3: validate the resolved IP address

A permitted-looking hostname may still resolve to a private address. After parsing, resolve the name and check every returned address, rejecting:

  • Loopback (127.0.0.0/8, ::1).
  • Private ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, IPv6 fc00::/7).
  • Link-local (169.254.0.0/16, which includes cloud metadata, and fe80::/10).
  • Unspecified, multicast and reserved addresses.
  • IPv4-mapped IPv6 forms of the above.

Then connect to the IP you validated (pinning it), while sending the original hostname in the Host header and for TLS. This defeats DNS rebinding, where a second lookup returns a different address from the one you checked.

Layer 4: redirects and responses

  • Disable automatic redirects, or follow them manually and run the full validation on each hop, with a small hop limit.
  • Set short timeouts and a maximum response size.
  • Do not return raw responses to the user. Parse what you need (an image, a title) and discard the rest. Return generic errors, so internal hosts cannot be mapped by error messages and timing.
  • Check the content type matches what you expect.

Layer 5: the network and the platform

Application checks can still have bugs, so contain the damage:

  • Put fetching code in a separate service or sandbox with an egress firewall that allows only the internet, and denies the private network and metadata address.
  • On cloud instances, require the session-token (IMDSv2-style) metadata mode, restrict hop limit, and give the instance role the least privilege.
  • Do not trust "internal" callers: protect internal services with authentication so that reaching them is not enough.
  • Log and alert on outbound requests to blocked ranges (see Logging and Monitoring Failures).

Defense in depth for SSRF

Reduce the feature Lists beat free text.
Allow-list Scheme, host, port.
Check resolved IPs Block private ranges.
Egress rules Network-level backstop.

Each layer catches what the others miss.

1 / 2

A validator with resolved-IP checks

The stand-in below combines the first three layers. FAKE_DNS is a dictionary standing in for a resolver.

Python Validate a user URL (allow-list plus IP checks)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note the fifth case: the host is allow-listed, yet resolves to a private address, and the resolved-IP check catches it. Note also the third: the @ makes the real host evil.example.net, and the URL is rejected because it carries credentials.

Handling redirects safely

Python Follow redirects manually and re-validate every hop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first URL looks harmless and even the second hop is fine, but the final redirect points inside and is blocked because every hop is validated.

Common mistakes

  • Validating the URL string, then letting the HTTP client resolve the name again (rebinding window).
  • Checking only the first IPv4 address when the name has several records, including IPv6.
  • Enabling redirects by default in the HTTP client.
  • Returning the fetched body or detailed errors to the user.
  • Running the fetcher in the same network zone and with the same credentials as the main app.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which is the strongest primary control for SSRF?

    Allow-lists and reducing the feature remove the problem; block-lists are incomplete.
  2. 2. Why validate the resolved IP address and then connect to that address?

    If the client resolves the name again, an attacker-controlled DNS server can return a different, internal address.
  3. 3. How should redirects be handled?

    A harmless URL can redirect to an internal one, so each hop needs the same checks.
  4. 4. What is a useful backstop when application checks have a bug?

    Network-level restrictions contain mistakes: even a bypassed filter cannot reach blocked ranges.

Summary

  • Fix SSRF in layers: remove or narrow the feature, allow-list, validate resolved IPs, handle redirects and responses, restrict the network.
  • Check parsed hosts and every resolved address, including IPv6 and mapped forms.
  • Pin the validated IP, cap time and size, and never echo raw responses.
  • Harden cloud metadata and use least-privilege roles.

Next: The Secure Development Lifecycle