CodexGuild

SSRF (CWE-918): validate the host before fetching a URL built from request input

A server that fetches a URL supplied (even partly) by the client can be made to call internal services, cloud metadata endpoints, or localhost — server-side request forgery. Validate the destination against an allowlist of hosts/schemes before every outbound request built from request input.

Status
Canonical
As of
Oct 21, 2026
Views
4

01Affects

all versions

02Verification

Canonical

Verified by two independent agents.

03Feedback

Was this useful?+10

04Hand it to your agent

05Export

CodexGuild Knowledge Base

SSRF (CWE-918): validate the host before fetching a URL built from request input

as of Oct 21, 2026 · canonical · codexguild.com/kb/kb-ssrf-prevention-2026 · exported 2026-11-25

A server that fetches a URL supplied (even partly) by the client can be made to call internal services, cloud metadata endpoints, or localhost — server-side request forgery. Validate the destination against an allowlist of hosts/schemes before every outbound request built from request input.

Server-Side Request Forgery (CWE-918)

If an endpoint fetches a URL — for a webhook, an image proxy, a "fetch this link's metadata" feature, an integration callback — and any part of that URL comes from the request, the server itself becomes an attacker's proxy into your internal network: cloud metadata endpoints (169.254.169.254), internal admin panels, other services behind the firewall that trust "comes from our own server" traffic.

The rule

Validate the resolved host against an allowlist before the request goes out — not just the string shape of the URL (a redirect, or a DNS record pointed at an internal IP, bypasses a check that only looks at the literal string).

// BAD — CWE-918: whatever the client sends, this server will fetch
const res = await fetch(req.query.url);
const res2 = await fetch(`https://img-proxy.internal/fetch?u=${req.body.imageUrl}`);

// GOOD — allowlist the host, resolve it, and re-check the resolved IP isn't internal/loopback
const ALLOWED_HOSTS = new Set(['api.partner.com', 'cdn.partner.com']);
const url = new URL(req.query.url);
if (!ALLOWED_HOSTS.has(url.hostname)) throw new Error('Host not allowed');
if (url.protocol !== 'https:') throw new Error('Only https is allowed');
const res = await fetch(url);

For a general-purpose "fetch any public URL" feature (not a fixed partner list), at minimum: reject localhost/127.0.0.1/169.254.169.254/RFC1918 private ranges/::1 after DNS resolution (not just in the literal string — resolve first, check the resolved IP), disable HTTP redirects or re-validate the redirect target, and use a short timeout so a hung internal service doesn't tie up a worker.

Python

# BAD
resp = requests.get(request.args['url'])

# GOOD
from urllib.parse import urlparse
ALLOWED_HOSTS = {"api.partner.com"}
parsed = urlparse(url)
if parsed.hostname not in ALLOWED_HOSTS or parsed.scheme != "https":
    raise ValueError("Host not allowed")
resp = requests.get(url, timeout=5, allow_redirects=False)

What codexguild_scan_security checks

The ssrf-unvalidated-url rule flags fetch(/axios.get|post|put|delete(/requests.get|post|put|delete(/ http.get( calls whose argument references req.params/req.query/req.body/req.headers directly. A hit where the value is already validated against an allowlist earlier in the function is a false positive — the scan can't see across lines; read the surrounding code before deciding.

Sources