SSRF allowlists after redirects and DNS rebinding
A server-side request forgery defense often begins with a URL allowlist. The first request looks correct, the allowlist check passes, and the application fetches the URL.
The problem is that the URL checked and the connection opened are not always the same address. Redirects, DNS changes, and parser differences can move the request into a network zone the allowlist was meant to protect.
OWASP's server-side request forgery prevention guidance stresses that filtering a user-supplied URL is only one part of the control. A safe design restricts the destination, validates the final connection target, controls redirects, and limits what the response can do. [1]
Parse once, authorize the actual destination
Do not compare a raw string against an allowlist and then hand that string to a general-purpose HTTP client. Parse it with the same library policy that will make the connection, reject credentials in the URL, normalize the host, and limit schemes to the protocols the feature needs.
Check the port separately. If the feature only needs HTTPS to an external partner API, it should not be able to reach arbitrary ports or file-style schemes.
The allowlist should describe destinations, not strings that merely contain an approved name. https://api.partner.example and https://api.partner.example.attacker.test are different hosts. If a wildcard is necessary, define its scope carefully and reject user-controlled labels that can escape it.
Resolve the address and use it
DNS rebinding can make a hostname resolve to an allowed address during validation and a blocked address during connection. A robust client resolves the name, rejects forbidden address ranges, and connects to the resolved address. It also verifies that the TLS certificate belongs to the requested hostname.
An allowlist based only on the hostname does not prove that the connection went to the intended network. Do not leave a gap between validation and use. Pin the resolved address for the request, or use a client hook that repeats the policy at connection time.
Check IPv4 and IPv6, including IPv4-mapped IPv6 addresses. Reject loopback, link-local, multicast, unspecified, and private ranges according to the service's network policy. A proxy or egress gateway should enforce the same rule if the application runs in a network where direct connections are possible.
Redirects are new requests
An HTTP redirect is not a harmless continuation. The client is choosing a new destination, often after the application has forgotten the original check. Disable automatic redirects for untrusted URLs and require every hop to pass the same policy.
Limit the number of hops and reject redirects to non-HTTP schemes. A redirect to a metadata service, an internal admin page, or a pre-signed object URL can be part of a legitimate workflow, but it needs an explicit allow decision.
If the feature truly needs redirects, preserve the original policy context and log each destination. Do not copy cookies or authorization headers to a different origin. Use a dedicated egress identity for outbound fetches so that a compromised application cannot borrow the worker's broader cloud role.
Prevent response-side abuse
SSRF is not only an inbound reachability issue. The response can act as an oracle. A service that returns line counts, image dimensions, status codes, or error text can reveal internal services even when it never returns the body.
Apply size limits, content-type checks, and timeouts. Redact sensitive upstream headers. Avoid accepting a URL from one user and returning the fetched body to another unless the destination and content are explicitly public.
The same rules apply to webhooks, image importers, PDF generators, link previews, package mirrors, and CI integrations. Shared libraries should expose one outbound-fetch function with consistent policy rather than letting each feature implement a slightly different check.
Verification plan
Create test destinations for:
- an allowed public host
- a denied private address
- a redirect chain
- a DNS name that changes between answers
- an IPv6 literal
- a URL containing embedded credentials
Run the tests from the same network namespace and proxy as production. Confirm that a blocked request fails before any connection is made and that the logs show the hostname, resolved address class, redirect count, and final decision without exposing credentials.
Finally, keep the egress policy outside the application configuration when possible. A network firewall that allows only required partner destinations is a stronger invariant than a regex in one service. The allowlist and the network policy should agree, and a change to one should trigger a review of the other.
The secure question is not whether the first URL looks familiar. It is whether the process actually connected to an approved address, through an approved path, with the smallest possible credential and response surface.
References
[1] OWASP, "Server-Side Request Forgery Prevention Cheat Sheet." https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html
[2] OWASP, "C10: Stop Server Side Request Forgery." https://top10proactive.owasp.org/the-top-10/c10-stop-server-side-request-forgery/
Comments
No comments yet. Start the discussion.