VibeSecCheck
Home/Security Guides/ssrf-prevention-node-nextjs
Critical RiskHeaders, Auth & APIsSSRFNode.jsNext.jsCloudflare Workers

Preventing SSRF in URL Fetchers & Scanners

Any backend route that takes a user-supplied URL and calls `fetch(url)` is vulnerable to SSRF — allowing attackers to probe internal admin panels, localhost Redis/Postgres ports, or cloud metadata endpoints (`169.254.169.254`).

Why AI Builders Introduce This

When you ask an AI to build a link previewer, website scraper, or custom webhook dispatcher, it writes `await fetch(body.url)` with zero protocol, port, IP range, or redirect checks.

Real-World Exploit Impact
  • Exfiltration of AWS/GCP/Azure IAM credentials via http://169.254.169.254/latest/meta-data/
  • Scanning and interacting with internal VPC microservices or localhost services
  • Bypassing naive checks via 302 redirects to http://127.0.0.1 or decimal IPs like http://2130706433

Vulnerable Pattern vs. Production Fix

Vulnerable Link Preview Route (Direct SSRF)typescript
// app/api/preview/route.ts — CRITICAL SSRF!
export async function POST(req: Request) {
  const { url } = await req.json();
  // Attacker passes http://169.254.169.254/latest/meta-data/ or http://127.0.0.1:6379
  const res = await fetch(url);
  return Response.json({ html: await res.text() });
}
Hardened URL Validator + Redirect Re-Check + Timeout
// Always validate protocol, hostname, IP ranges, ports, AND final redirect URL!
export function assertSafePublicUrl(raw: string): URL {
  const url = new URL(raw);
  if (url.protocol !== "https:" && url.protocol !== "http:") {
    throw new Error("Blocked protocol");
  }
  const host = url.hostname.toLowerCase();
  if (
    host === "localhost" ||
    host.endsWith(".local") ||
    host.endsWith(".internal") ||
    /^(127\.|10\.|0\.|169\.254\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/.test(host) ||
    /^(0x|\d+$)/.test(host) ||
    host.includes(":")
  ) {
    throw new Error("Blocked private/internal host");
  }
  if (url.port && url.port !== "80" && url.port !== "443") {
    throw new Error("Non-standard port blocked");
  }
  return url;
}

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Audit every server-side fetch(), axios, or http.request() call that accepts user-provided URLs (webhooks, link previews, image proxies, RSS readers, scanners):
1. Enforce strict http/https protocol allowlist and standard ports (80/443).
2. Block localhost, *.local, *.internal, 127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16, numeric/hex IP encodings, and private IPv6 ranges (::1, fc00::/7, fe80::/10, ::ffff:*).
3. Re-validate redirect destinations (or disable redirects), enforce a 10-second AbortController timeout, and cap maximum response bytes read.

How to Verify Your Fix Worked

  1. 1Test your endpoint with `http://127.0.0.1`, `http://169.254.169.254`, `http://[::1]`, `http://2130706433`, and `file:///etc/passwd` — every single one must return HTTP 400.
  2. 2Verify a 302 redirect pointing to `http://localhost` is rejected.
  3. 3Verify slowloris targets abort cleanly after your configured timeout.

Frequently Asked Questions

Why do I need to block decimal IPs like http://2130706433?

2130706433 is the 32-bit integer representation of 127.0.0.1! Many HTTP clients resolve it straight to localhost, bypassing naive regex checks.

Why cap response body size when fetching external URLs?

Without a byte cap, an attacker can point your fetcher at a 10GB stream and crash your Node/Worker container with an out-of-memory error.

How does VibeSecCheck protect its own scanner against SSRF?

VibeSecCheck enforces protocol checks, blocks all RFC1918/loopback/metadata/IPv6-local/numeric IPs, restricts ports to 80/443, re-validates final redirect targets, enforces a 10s timeout, and caps response bytes.

Related Security Playbooks