Preventing SSRF: check the destination before sending, and require IMDSv2
On services like GitHub, a user registers a notification URL in the settings page, and when a push happens, the server sends a notification (a webhook) to that URL. When a user enters an image URL, the server fetches tha
On services like GitHub, a user registers a notification URL in the settings page, and when a push happens, the server sends a notification (a webhook) to that URL. When a user enters an image URL, the server fetches that image. In features like these, the user decides where the server sends its requests. An attacker registers the URL of an internal server, makes the server send requests to it, and reads internal data or operates admin functions. This attack is called SSRF (Server-Side Request Forgery).
Even when the application checks that the destination is not internal, some requests get past the check. For example, the DNS result can change after the check, or an external URL can redirect to an internal one.1
Working from published documentation, this article sorts out the features where user input becomes a request destination and the damage that follows. It then covers how to check the destination so that these requests can't get past the check, and how to require IMDSv2 on EC2.
Features where user input becomes the request destination
SSRF happens in features users work with every day, such as notifications, images, XML files, and AI chat.
Registering a notification URL
To post notifications to a chat, a user registers a notification URL in the settings page. Each time an event occurs, the server sends a notification to that URL. Some internal servers let any request that comes from inside the network use their admin functions without authentication.2 If the user registers the URL of an internal admin function as the notification URL, the server calls that admin function from inside the network. The user who registered it can now operate the internal admin function.
Entering an image URL
To set a profile picture, a user enters the URL of an image on the web. The server downloads the image from that URL and stores it. Some internal servers let any request that comes from inside the network use their admin functions without authentication. If the user enters the URL of an internal admin function as the image URL, the server calls that admin function from inside the network. The user who entered it can now operate the internal admin function.
Uploading an XML file
To exchange data with business partners, a user uploads an XML file. The server parses the XML and imports the data in it. Some internal servers let any request that comes from inside the network use their admin functions without authentication. When the setting that pulls in the contents of URLs written in the XML (external entity references) is enabled, a user can write the URL of an internal admin function into the XML and upload it, and the server calls that admin function from inside the network. The user who uploaded it can now operate the internal admin function.
Entering a URL in an AI chat
To get a web page summarized, a user gives the page URL to an AI chat. The server fetches the page from that URL and passes it to the AI. Some internal servers let any request that comes from inside the network use their admin functions without authentication. If the user gives the URL of an internal admin function in the chat, the server calls that admin function from inside the network.3 The user who gave it can now operate the internal admin function.
Finding these features in your service
In your own service, find the places that call an HTTP client and check whether the destination uses user input. Where XML is parsed, check whether external entity references are enabled.
AWS credentials can be read
On EC2, a request sent from inside an instance to 169.254.169.254 reaches the service that returns information about the instance (IMDS). On an instance with an IAM role attached, IMDS returns temporary credentials for that role. These credentials allow the operations the role permits.4
If this URL can be put into an image feature that displays the stored content as-is, or into an AI chat that returns a summary, the credentials go back to the attacker. The attacker can then use the permissions of the IAM role attached to the server. If the role can read an S3 bucket, the data in that bucket can be read.
Mitigations
Block requests to internal destinations by checking the destination before sending. Put this check where the HTTP client is called. Requests that a library sends on its own don't go through this check, so configure the library not to send them. For XML, disable external entity references. On EC2, also require IMDSv2.
Restrict destinations with an allowlist
For features where the destinations are fixed, such as limiting notifications to Slack, keep a list of allowed hostnames and don't send to any hostname not on the list. An allowed host can still redirect to an internal address, so in this case too, don't follow redirects.
Don't send to internal IP ranges
For features where users can enter any URL, you can't build a list of hostnames. In that case, resolve the hostname in the URL to an IP and don't send if the IP falls in any of these ranges.
- Private IPs: the ranges internal servers use.
10.0.0.0/8,172.16.0.0/12,192.168.0.0/16, andfc00::/7for IPv6 - Loopback: the ranges that point to the server itself.
127.0.0.0/8, and::1for IPv6 - Link-local: the ranges that include IMDS at
169.254.169.254.169.254.0.0/16, andfe80::/10for IPv6
IPv6 has a notation that embeds an IPv4 address after ::ffff:. A connection to such an IPv6 address reaches the embedded IPv4 address. If you compare only against the IPv4 ranges, internal addresses written this way get through. So convert them back to IPv4 before comparing.
Don't let the HTTP client resolve the hostname again
When an HTTP client is given a URL, it resolves the hostname to an IP again on its own when it connects. An attacker can enter a URL on a domain they control and set up that domain's DNS server to return an external IP for the first query and an internal IP for the second. The first query is the one the application makes to check the destination; the second is the one the HTTP client makes when it resolves again. So pass the checked IP to the HTTP client and have it connect to that IP.
Don't follow redirects
An external server can respond with "this has moved to another URL" and give a new URL. This is called a redirect. Some HTTP clients follow it by default and send the request to the new URL (Appendix). If an attacker redirects from their own server to an internal URL, the request reaches an internal IP that was never checked. So configure the HTTP client not to follow redirects.
Require IMDSv2
On EC2, require IMDSv2. With IMDSv2, IMDS does not return credentials unless the request includes a header with a token obtained by a PUT request.5
In all of these features (notification URLs, image URLs, XML, and AI chat), the only thing the attacker controls is the URL. A URL alone can't send a PUT or a header. So once IMDSv2 is required, credentials can't be read through these features.
The following command lists the instances that don't require IMDSv2.6
aws ec2 describe-instances \
--filters "Name=metadata-options.http-tokens,Values=optional" \
--query "Reservations[*].Instances[*].[InstanceId]" \
--output text
Before requiring it, check the CloudWatch metric MetadataNoToken. This metric counts the calls to IMDS made without a token. If it isn't 0, software that doesn't support IMDSv2 is running on the instance, so upgrade it to a version that does.
Once it reaches 0, require IMDSv2 on each instance. When you specify --http-tokens, also specify --http-endpoint enabled.7
aws ec2 modify-instance-metadata-options \
--instance-id i-1234567890abcdef0 \
--http-tokens required \
--http-endpoint enabled
Appendix: default redirect behavior of HTTP clients
The table below shows whether the major HTTP clients follow redirects by default and how to stop them, based on each client's documentation as of November 26, 2026.
| HTTP client | Version | Follows redirects by default | How to stop following |
|---|---|---|---|
| Python requests | 2.34.2 | Yes (except HEAD) | Pass allow_redirects=False
|
| Go net/http | 1.27.4 | Yes (up to 10 redirects) | Return http.ErrUseLastResponse from Client.CheckRedirect
|
| Node.js fetch | 26.13.1 | Yes | Pass redirect: "manual" or redirect: "error"
|
| Java HttpClient | 25 | No | Keep the default |
| Ruby Net::HTTP | 4.0 | No | Keep the default |
| curl | 8.23.0 | No | Don't pass -L
|
-
"SSRF attacks against other back-end systems" in PortSwigger's Server-side request forgery (SSRF). ↩
-
CVE-2023-32786, listed in CWE-918. A case where a prompt could make a framework for building LLM applications fetch data from an arbitrary URL. ↩
-
AWS, Retrieve security credentials from instance metadata. ↩
-
AWS, Use the Instance Metadata Service to access instance metadata. ↩
-
AWS, Transition to using Instance Metadata Service Version 2. ↩
-
AWS, Modify instance metadata options for existing instances. ↩
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.