Difficulty: Advanced
What is Server-Side Request Forgery (SSRF), why is it dangerous in cloud environments, and how do you prevent it?
SSRF is a vulnerability where an attacker makes your server send HTTP (or other protocol) requests to a destination the attacker chooses, usually by supplying a URL that the server fetches. Because the request comes from inside your network, it can reach internal services, admin panels and cloud metadata endpoints that are never exposed to the internet. In cloud setups, SSRF is often the first step to stealing credentials and taking over an account.
Any feature where the server fetches a user-supplied URL:
<img src> and <iframe> targets).In blind SSRF the response is not shown, but the attacker can still hit internal endpoints that trigger actions, or infer open ports from timing.
Cloud VMs expose an instance metadata service at the link-local address 169.254.169.254. On AWS, a request to /latest/meta-data/iam/security-credentials/<role> returns temporary access keys for the instance's IAM role. The 2019 Capital One breach followed this pattern: an SSRF through a misconfigured WAF let the attacker read role credentials and then download S3 data.
AWS IMDSv2 mitigates this by requiring a session token obtained with a PUT request and a custom header, which most SSRF primitives (simple GET with a URL) cannot send. Enforce IMDSv2 (HttpTokens=required) and keep the hop limit at 1 for containers that do not need metadata. GCP and Azure require a Metadata-Flavor: Google or Metadata: true header for similar reasons.
Checking that the URL string does not contain localhost or 169.254 is easy to bypass:
http://2130706433/ (decimal for 127.0.0.1), http://0x7f.1/, http://[::1]/, http://[::ffff:169.254.169.254]/.302 to the metadata endpoint and the HTTP client follows it.file://, gopher://, dict:// in libraries like curl.http and https, disable redirects or re-validate every hop, set short timeouts and a response size limit.SSRF was its own category (A10) in the OWASP Top 10 2021; the 2025 edition folds it into Broken Access Control, but the impact has not changed.
import dns from 'node:dns/promises';
import ipaddr from 'ipaddr.js';
async function assertPublicUrl(raw) {
const url = new URL(raw);
if (!['http:', 'https:'].includes(url.protocol)) {
throw new Error('Only http(s) allowed');
}
const records = await dns.lookup(url.hostname, { all: true });
for (const { address } of records) {
let ip = ipaddr.parse(address);
if (ip.kind() === 'ipv6' && ip.isIPv4MappedAddress()) ip = ip.toIPv4Address();
if (ip.range() !== 'unicast') { // private, loopback, linkLocal, etc.
throw new Error(`Blocked internal address ${address}`);
}
}
return { url, ip: records[0].address };
}
// Then connect to the validated IP (e.g. a custom undici dispatcher
// with a fixed lookup) and set redirect: 'manual' to stop 302 tricks.
The check runs on the resolved IP, not the URL string, which defeats decimal and hex encodings. Pinning the connection to the checked IP blocks DNS rebinding.
# Require session tokens and keep metadata unreachable from extra network hops
aws ec2 modify-instance-metadata-options \
--instance-id i-0abc123def4567890 \
--http-tokens required \
--http-put-response-hop-limit 1 \
--http-endpoint enabled
# A legitimate IMDSv2 call needs a PUT first
TOKEN=$(curl -s -X PUT http://169.254.169.254/latest/api/token \
-H 'X-aws-ec2-metadata-token-ttl-seconds: 300')
curl -s -H "X-aws-ec2-metadata-token: $TOKEN" \
http://169.254.169.254/latest/meta-data/instance-id
Most SSRF bugs only let the attacker control a GET URL, so they cannot perform the PUT with a custom header that IMDSv2 requires.
SSRF, Cloud Metadata API, IMDSv2, Allowlisting, Internal Network