What Is SSRF? How to Restrict Server-Side Requests Safely
Server-side request forgery (SSRF) is a vulnerability in which user-controlled input causes an application server to send a network request to an unintended destination. A server may reach internal services or cloud metadata that a user's browser cannot, so an attacker can misuse it as a request proxy. The best defense is to avoid arbitrary URLs and let users select only approved destinations. When a feature must accept arbitrary external addresses, combine application validation with outbound network restrictions.
Why is a request sent by the server risky?
A server sends requests from a different network position and with different privileges than a user's device. For example, imagine a preview feature that accepts a URL and fetches its content. If the feature fetches any URL without restrictions, an attacker may make the server request an internal admin page, private API, or cloud instance metadata address that the user cannot access. Even if the attacker cannot read the response, reaching an internal service may trigger an action or change state. The actual impact depends on server privileges, how internal services establish trust, and network boundaries.
SSRF differs from CSRF. CSRF tricks a victim's browser into sending a request with the user's authenticated session. SSRF tricks a server-side feature into making a request to another destination using the server's network location and privileges. Both involve requests, but the sender and trust boundary differ. (OWASP CSRF Prevention guidance)
Which features should you review for server-side URL requests?
Review any feature that fetches or checks a URL or host supplied by a user. Examples include link previews, image or document import, webhook delivery, remote resource checks, and URL-based converters. Even if there is no visible URL field, components that follow URLs—such as file importers, feed processors, and document parsers—may make requests. First trace external input to the actual HTTP client.
The right controls depend on which destinations a feature needs to reach.
| Feature purpose | Primary defense | What to watch |
|---|---|---|
| Call a few known internal or partner services | Use a destination ID mapped on the server; allow only approved hosts and addresses | Do not accept a full URL from the user; validate redirects and the address used after DNS resolution |
| Connect to many public Internet hosts, as with webhooks | Allow only approved protocols and public addresses; also enforce firewall or egress-proxy policy | A denylist may miss new internal addresses or alternative representations |
| No business need for arbitrary destinations | Provide a fixed function or destination ID instead of a URL | Removing unnecessary network capability reduces validation burden |
How do you restrict requests to known destinations?
When target systems are known, avoid accepting a complete URL. Let the user select a business-level destination such as “HR service,” then map that identifier on the server to a configured scheme, host, port, and path. This reduces the scope for request input to choose an arbitrary host or path.
If input is still needed, do not compare URL strings by prefix. Use a URL parser to inspect scheme, host, port, user information, and path. For example, a URL like https://approved.example@attacker.example/ may appear to contain an approved host, but its actual host is different. Compare the normalized host or address exactly against the allowlist, and make sure the parser and policy agree on details such as case handling and trailing dots.
When allowing hostnames, control the DNS result at connection time too. DNS rebinding can make a domain resolve to a public address during validation and an internal address during connection; a time-of-check/time-of-use gap can have the same effect. Check both A and AAAA results, then use a safe client feature that pins the connection to a validated IP while retaining the original hostname for the HTTP Host header and TLS certificate verification. Resolving DNS once for validation and allowing the client to resolve it again separately may leave the connection unprotected.
What if arbitrary public URLs are required?
Features such as webhooks or public-content import may need to contact changing destinations, so a domain allowlist alone may not work. Define a validation sequence and enforce default-deny at the network layer as well.
**Narrow the request format.** Parse the URL and allow only necessary schemes. Unless the use case requires them, reject user information, unexpected ports, and unnecessary URL schemes. Do not try to validate a complex URL with one regular expression.
**Validate destination addresses.** Resolve the domain's A and AAAA records and block loopback, private, link-local, unspecified, and other non-public or special-use ranges. Handle both IPv4 and IPv6, and normalize address representations with a security-reviewed library. A domain that looks public as text is not enough.
**Bind the connection to the validated address.** Ensure the address checked is the one actually used. Include DNS re-resolution, connection retries, and proxy routes in the policy.
**Control redirects.** Disable automatic redirects or validate every redirect as a new request. A redirect from a public host to an internal address must not bypass a policy that checked only the first URL.
**Limit responses and resources.** Set connection and read timeouts, response-size limits, and allowed content types. Apply separate content-security handling when rendering fetched HTML or documents; SSRF validation does not automatically prevent XSS in the output.
**Restrict outbound networking.** Use a firewall or egress proxy to allow only the services and ports the application must reach. This reduces paths to internal addresses and metadata services if the request feature is compromised.
Address blocking is error-prone because private and special-use ranges vary by environment and must be updated as networks change. OWASP recommends allowlists for known destinations and combines public-address validation with network-layer defense when arbitrary external destinations are required. Cloud metadata protections such as AWS IMDSv2 can add defense in depth, but they do not replace application URL validation or egress policy.
How can you review and verify the controls safely?
In a code review, follow user input through the HTTP client, URL parser, redirect handling, DNS, and proxy configuration. For every code path that fetches an external URL, confirm that the approved destination, resolved address, and actual connection agree. Review automatic redirects, parser differences, IPv6 handling, DNS caching or re-resolution, and fallback paths after errors.
Run dynamic tests only in an isolated environment you own or are explicitly authorized to test. First document the destinations and expected allow/deny outcomes. Use a controlled internal test server and a unique test domain to verify allowlisting, address checks, redirects, DNS handling, and outbound policy. Do not test against production admin addresses, metadata services, or arbitrary internal systems. Verify success using the test server's request log, application rejection logs, and network-policy block logs; do not rely only on seeing response content.
Core principle
SSRF defense does not stop at checking how a URL looks. Decide which destinations the application may reach, keep the destination consistent from DNS validation through the actual connection, and control redirects and outbound networking. For a few approved destinations, a server-side mapping and allowlist are simplest. If a business feature needs arbitrary public destinations, combine address validation, connection pinning, redirect revalidation, response limits, and outbound firewall rules.
References
[OWASP Server-Side Request Forgery Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html)
[OWASP Web Security Testing Guide: Server-Side Request Forgery](https://wstg.owasp.org/latest/11-Injection/18-Server-Side_Request_Forgery/)
[MITRE CWE-918: Server-Side Request Forgery](https://cwe.mitre.org/data/definitions/918.html)
[OWASP Top 10 2021: A10 Server-Side Request Forgery](https://top10.owasp.org/2021/A10_2021-Server-Side_Request_Forgery_%28SSRF%29/)
[OWASP Cross-Site Request Forgery Prevention Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html)