Skip to main intelligence content
SOURCES: CISA KEV + NIST NVD 2.0 + FIRST EPSS
LOOT & EXPLOITS
DEFENSIVE VULN & TOOL TRIAGE

PAID STACK WATCH · $99/year or $12/month

Back to The Lab
Verified: 2026-11-12
Web application securityDifficulty: Intermediate10 min technical walkthrough

Server-Side Request Forgery (SSRF) & IMDSv2 Enforcement (CWE-918)

How URL fetchers, webhook testers, and headless browsers get tricked into pivoting across internal subnets and cloud metadata endpoints.

1. WHAT THE CONCEPT MEANS

Server-Side Request Forgery (SSRF, CWE-918) occurs when a web application fetches a remote resource (such as a webhook callback, avatar URL, OpenGraph preview, PDF stylesheet, or AI RAG document URL) without validating the resolved destination IP address at connection time. Because the HTTP request originates from the server's own network namespace, an attacker can coerce the server into probing loopback (`127.0.0.1`), RFC 1918 internal subnets (`10.0.0.0/8`, `172.16.0.0/12`, `192.168.0.0/16`), or the cloud Instance Metadata Service (`169.254.169.254`).

2. WHY IT MATTERS IN REAL OPERATIONS

Modern web applications and AI platforms routinely fetch user-supplied URLs. On cloud hosts using legacy IMDSv1, a single unauthenticated `GET http://169.254.169.254/latest/meta-data/iam/security-credentials/<role>` returns temporary IAM session credentials in the HTTP response body. On self-hosted Linux servers, SSRF allows attackers to reach internal unauthenticated services bound to `127.0.0.1` (such as Redis, Elasticsearch, local LLM inference servers on `:11434`, or internal admin APIs).

3. HOW IT WORKS // TECHNICAL MECHANISM & ARCHITECTURE DIAGRAM

Most naive SSRF defenses parse the URL string and check `if (url.hostname === 'localhost' || url.hostname.startsWith('169.254.'))`. Attackers bypass string-level checks in three ways: (1) Alternate IP encodings (`http://2130706433/`, `http://0177.0.0.1/`, IPv6 `http://[::ffff:127.0.0.1]/`), (2) HTTP `302` redirects from an external attacker-controlled domain to `http://169.254.169.254/`, and (3) Time-of-Check to Time-of-Use (TOCTOU) DNS rebinding, where `attacker.example` resolves to a public IP during the validation lookup (`TTL=0`) and resolves to `127.0.0.1` milliseconds later when `fetch()` opens the TCP socket.

WHY URL STRING VALIDATION FAILS VS. CUSTOM SOCKET LOOKUP GUARD
1. Naive Check: String / Pre-DNS Validation

App resolves `hook.attacker.tld` -> `93.184.216.34` (passes check), then calls `fetch('https://hook.attacker.tld')`.

2. DNS Rebinding / HTTP 302 Pivot

Second DNS lookup (TTL=0) or 302 redirect resolves to `127.0.0.1:11434` or `169.254.169.254`. Server connects to internal socket.

3. Hardened Path: Custom Agent/Lookup Hook + Redirects Disabled

Custom `lookup` callback inspects the exact IP returned by `dns.lookup` immediately before `connect()` and rejects loopback, link-local, and RFC 1918 ranges.

4. REALISTIC CODE COMPARISON (TYPESCRIPT)

Validating an outbound webhook URL in Node.js so it can never connect to loopback, link-local (`169.254.0.0/16`), or RFC 1918 private addresses, even under DNS rebinding.

VULNERABLE IMPLEMENTATION
// VULNERABLE: String check misses DNS rebinding, 302 redirects, and IPv6/integer IPs
export async function fetchWebhookUnsafe(targetUrl: string): Promise<number> {
  const parsed = new URL(targetUrl);
  if (parsed.hostname === "localhost" || parsed.hostname === "169.254.169.254") {
    throw new Error("Blocked host");
  }
  const res = await fetch(parsed.toString());
  return res.status;
}
HARDENED IMPLEMENTATION
// SECURE: Validate the resolved IP address and block redirects to internal CIDRs
import dns from "node:dns/promises";
import net from "node:net";

function isPrivateOrReservedIp(ip: string): boolean {
  const v = net.isIP(ip);
  if (v === 4) {
    return (
      ip.startsWith("127.") ||
      ip.startsWith("10.") ||
      ip.startsWith("169.254.") ||
      ip.startsWith("192.168.") ||
      /^172\.(1[6-9]|2\d|3[0-1])\./.test(ip) ||
      ip.startsWith("0.")
    );
  }
  if (v === 6) {
    const low = ip.toLowerCase();
    return low === "::1" || low.startsWith("fe80:") || low.startsWith("fc") || low.startsWith("fd") || low.includes("::ffff:");
  }
  return true;
}

export async function verifyPublicEndpointIps(rawUrl: string): Promise<URL> {
  const parsed = new URL(rawUrl);
  if (parsed.protocol !== "https:") {
    throw new Error("Only HTTPS endpoints are permitted");
  }
  const records = await dns.lookup(parsed.hostname, { all: true, verbatim: true });
  if (records.length === 0 || records.some((r) => isPrivateOrReservedIp(r.address))) {
    throw new Error("Destination resolves to a private, loopback, or link-local IP");
  }
  return parsed;
}

Instead of trusting the hostname string, the hardened check resolves all A/AAAA records, rejects any IPv4/IPv6 loopback, link-local (`169.254.0.0/16`, `fe80::/10`), unique-local (`fc00::/7`), or IPv4-mapped IPv6 (`::ffff:`) address, and pairs with `redirect: 'error'` on the HTTP client.

5. HOW DEFENDERS RECOGNIZE THE PROBLEM

  • [1]Outbound HTTP requests from web worker processes toward `169.254.169.254` or internal management subnets in egress firewall / Zeek `http.log` telemetry.
  • [2]Cloud audit logs (AWS CloudTrail) showing IAM role credentials used from an external IP address outside the VPC.
  • [3]Unusual DNS queries from application servers for domains returning `127.0.0.1` or `169.254.169.254` in A records.

6. DEFENSIVE ENGINEERING MITIGATIONS

  • [1]Enforce IMDSv2 (`HttpTokens=required`, `HttpPutResponseHopLimit=1`) on all cloud compute instances so simple `GET` SSRF cannot read metadata credentials.
  • [2]Validate the resolved socket IP address inside a custom HTTP agent/dispatcher with `redirect: 'error'` rather than relying on URL hostname regexes.
  • [3]Run URL-fetching workers inside an isolated network namespace or egress proxy that drops traffic to RFC 1918, loopback, and link-local CIDRs at the Linux `nftables` layer.

ISOLATED LOCAL VERIFICATION EXERCISE

LOCAL SANDBOX ONLY

Authorization Boundary: Local verification showing how integer and shorthand IP notations resolve to `127.0.0.1` in standard POSIX resolvers.

node -e 'const dns = require("node:dns/promises"); Promise.all([dns.lookup("2130706433"), dns.lookup("127.1")]).then(console.log)'

Expected Observation: Both `2130706433` (32-bit integer for `127.0.0.1`) and shorthand `127.1` resolve directly to `{ address: '127.0.0.1', family: 4 }`, proving why string comparisons on `url.hostname` fail.

7. RECOMMENDED RESOURCES & PRIMARY CITATIONS

Source Citations:
[MITRE-CWE-918]CWE-918: Server-Side Request Forgery (SSRF)— MITRE CWELink
[OWASP-SSRF]Server-Side Request Forgery Prevention Cheat Sheet— OWASP FoundationLink