How to fix: fetch() targeting localhost / loopback / link-local
fetch("http://127.0.0.1:8080/..."), fetch("http://[::1]:..."), or fetch("http://169.254.169.254/...") target loopback or cloud metadata endpoints. When the URL is even partially user-controlled, the request can be redirected to internal services.
Why it matters
Attacker reaches internal services such as admin panels, databases, or cloud metadata APIs.
The `code-ssrf-fetch-port` check verifies that the server does not expose the `ssrf-fetch-port` weakness in the code category. The detection runs against the response headers, body, or auxiliary probes (DNS, TLS, async fetch) as appropriate.
How to fix it
- 1Resolve the destination hostname and reject private/loopback/link-local ranges before fetching.
- 2Use an outbound HTTP proxy that enforces an allowlist of external hosts.
- 3Disable HTTP redirects or re-validate the host after each redirect.
Code examples
Block private IPs and non-standard ports
import { isIP } from "net";
async function safeRequest(url: string) {
const parsed = new URL(url);
const BLOCKED_PORTS = new Set(["22", "3306", "5432", "6379", "8080"]);
if (BLOCKED_PORTS.has(parsed.port)) throw new Error("Port not allowed");
if (["localhost", "127.0.0.1", "::1"].includes(parsed.hostname)) {
throw new Error("Loopback not allowed");
}
return fetch(url, { signal: AbortSignal.timeout(5_000) });
}References
Detected automatically by VulnRadar. The check id is code-ssrf-fetch-port, stable across every scan so you can filter or diff on it.
More code checks
- CriticalForm Submits Data Over Insecure HTTP
- MediumPotential Prototype Pollution Sinks Detected
- HighPotential Command Injection Vectors
- HighJWT Decoded Without Signature Verification
- HighPotential Path Traversal Vulnerability
- HighInsecure Authentication Mechanisms Detected
- HighPotential Insecure Deserialization
- Mediumeval() Usage in Inline Scripts
Questions
- How severe is fetch() targeting localhost / loopback / link-local?
- VulnRadar rates this high severity in the code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
- Does VulnRadar check for this automatically?
- Yes. It runs as part of every scan under the check id code-ssrf-fetch-port, which stays the same between runs so you can filter, diff, or gate CI on it.
- Which standards does fetch() targeting localhost / loopback / link-local map to?
- It maps to CWE-918. In the OWASP Top 10 it falls under A10:2021.
Missing a check?
The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.