Skip to content

SSRF Prevention: AWS IMDSv2 & Cloud Metadata

Defend against Server-Side Request Forgery (SSRF) in cloud apps: protect AWS IMDSv2 and GCP metadata endpoints, block private IPs, and stop rebinding.

By BugSnaps Security Research · 2 min read · Updated

Add BugSnaps as a preferred source on Google(opens in a new tab)

Want more of our security guides? Select BugSnaps and confirm your choice on Google.

Server-Side Request Forgery (SSRF) occurs when a web application accepts a URL, hostname, or webhook address from a user and instructs the backend server to fetch data from that location without validating the destination. In cloud environments such as AWS, GCP, Azure, and Kubernetes, an unvalidated server-side HTTP client can reach internal services and cloud instance metadata endpoints that are inaccessible from the public internet.

How cloud metadata services amplify SSRF impact

Cloud virtual machines and containers frequently expose an instance metadata service on the link-local address `169.254.169.254`. Under older AWS IMDSv1 configurations, a simple HTTP GET request to `http://169.254.169.254/latest/meta-data/iam/security-credentials/` returns temporary IAM credentials attached to the compute role. Hardening your cloud runtime significantly reduces the blast radius of any URL-fetching feature:

  • Enforce AWS IMDSv2: require session-oriented `PUT` requests with an `X-aws-ec2-metadata-token-ttl-seconds` header and set the instance metadata hop limit to 1 so simple GET-based SSRF relays cannot read credentials.
  • Verify GCP and Azure metadata headers: GCP metadata endpoints require `Metadata-Flavor: Google` and Azure IMDS requires `Metadata: true`, which prevents basic headerless GET SSRF from reading instance tokens.
  • Isolate Kubernetes workloads: apply Kubernetes NetworkPolicies to block application pods from reaching the cloud metadata IP (`169.254.169.254/32`) and internal control-plane endpoints unless explicitly needed.

Why naive string blocklists fail against DNS rebinding and IP encodings

Checking whether a user-supplied URL starts with `http://127.0.0.1` or `http://169.254.169.254` at the string level is easy to bypass. HTTP clients often accept alternative IP notations—such as decimal integers (`2130706433`), hexadecimal (`0x7f000001`), octal octets, IPv6 loopback (`[::1]`), or HTTP 302 redirects from an external server back to an internal address.

Even if you resolve DNS before fetching, a custom hostname controlled by an attacker can return a public IP address during your validation lookup with a 0-second TTL and return `127.0.0.1` or `169.254.169.254` milliseconds later when the HTTP client opens its socket (DNS rebinding). To prevent this, disable automatic HTTP redirects on outbound fetchers, resolve the hostname once inside a custom socket dialer, verify that the resolved IP is not in private or link-local ranges (RFC 1918, RFC 3927, RFC 4193), and route untrusted outbound traffic through a dedicated egress proxy.

Validate the resolved IP address inside your HTTP client's connection dialer, disable untrusted redirects, and enforce IMDSv2 across all cloud instances.

Run a vulnerability scan on your website.

Verify your domain and use MyPentest to check for supported web and API weaknesses. Review findings and coverage; a clean scan does not rule out every CVE.