Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596import dns from 'dns/promises';import { isIP } from 'net';
const PRIVATE_IPV4_RANGES = [ /^127\./, // 127.0.0.0/8 loopback /^10\./, // 10.0.0.0/8 /^172\.(1[6-9]|2\d|3[01])\./, // 172.16.0.0/12 /^192\.168\./, // 192.168.0.0/16 /^0\./, // 0.0.0.0/8 /^169\.254\./, // link-local /^100\.(6[4-9]|[7-9]\d|1[01]\d|12[0-7])\./, // CGN 100.64.0.0/10];
const BLOCKED_HOSTNAMES = [ 'localhost', 'host.docker.internal', 'gateway.docker.internal', 'metadata.google.internal', 'metadata.google', 'kubernetes.default', 'kubernetes.default.svc',];
function normalizeHostname(hostname: string): string { return hostname.trim().toLowerCase().replace(/\.+$/, '');}
export function isPrivateAddress(address: string): boolean { const normalized = address.trim().toLowerCase(); const ipv4 = normalized.replace(/^::ffff:/, '');
if (isIP(ipv4) === 4) { return PRIVATE_IPV4_RANGES.some((range) => range.test(ipv4)); }
if (isIP(normalized) === 6) { if (normalized === '::1' || normalized === '::') return true; if (/^f[cd][0-9a-f:]+$/i.test(normalized)) return true; // fc00::/7 unique-local if (/^fe[89ab][0-9a-f:]+$/i.test(normalized)) return true; // fe80::/10 link-local }
return false;}
function isBlockedHostname(hostname: string): boolean { if (BLOCKED_HOSTNAMES.includes(hostname)) return true;
// Block internal-only suffixes commonly used for service discovery. if ( hostname.endsWith('.internal') || hostname.endsWith('.localhost') || hostname.endsWith('.local') ) { return true; }
return false;}
// Syntactic check only — rejects URLs whose hostname literally is a private IP or an// internal DNS name. Use isPrivateUrlResolved for a DNS-backed check that also catches// attacker-controlled domains that A-record to RFC1918.export function isPrivateUrl(parsed: URL): boolean { const hostname = normalizeHostname(parsed.hostname); if (!hostname) return true;
// Block known internal hostnames if (isBlockedHostname(hostname)) return true;
if (isPrivateAddress(hostname)) return true;
return false;}
// DNS-resolving SSRF check. Use this for any user-controlled URL before fetching —// blocks public-name → private-IP shenanigans (e.g. 127.0.0.1.nip.io). Fails open// only when DNS itself fails, since the outbound fetch will then also fail naturally.export async function isPrivateUrlResolved(parsed: URL): Promise<boolean> { if (isPrivateUrl(parsed)) return true;
const hostname = normalizeHostname(parsed.hostname); if (!hostname || isIP(hostname)) { return isPrivateAddress(hostname); }
try { const results = await dns.lookup(hostname, { all: true, verbatim: true }); if (results.length === 0) return false; return results.some((result) => isPrivateAddress(result.address)); } catch { // If DNS resolution fails here, the outbound request will fail normally. // We only block when we can prove the host resolves to a private address. return false; }}