Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

refactor(security): classify SSRF ranges with net.BlockList

Yudhi Armyndharis15fd41ed26c5
diff --git a/src/common/security/ssrf-guard.ts b/src/common/security/ssrf-guard.ts
index 5ebc34dc7..d65b79c57 100644
--- a/src/common/security/ssrf-guard.ts
+++ b/src/common/security/ssrf-guard.ts
@@ -1,4 +1,4 @@
-import { isIPv4, isIPv6, type LookupFunction } from 'net';
+import { BlockList, isIPv4, isIPv6, type LookupFunction } from 'net';
import { lookup } from 'dns/promises';
import { type LookupAddress, type LookupOptions } from 'dns';
import { Agent, fetch as undiciFetch, Headers, type RequestInit, type Response } from 'undici';
@@ -66,18 +66,9 @@ function getAllowedHosts(): Set<string> {
);
}
-function ipv4ToInt(ip: string): number {
- return ip.split('.').reduce((acc, octet) => acc * 256 + Number(octet), 0);
-}
-
-function inCidr4(ipInt: number, base: string, bits: number): boolean {
- const baseInt = ipv4ToInt(base);
- const mask = bits === 0 ? 0 : (0xffffffff << (32 - bits)) >>> 0;
- return (ipInt & mask) >>> 0 === (baseInt & mask) >>> 0;
-}
-
-// IPv4 ranges that must never be reachable by an outbound webhook (SSRF targets).
-const BLOCKED_V4: ReadonlyArray<readonly [string, number]> = [
+// IPv4 ranges that must never be reachable by an outbound webhook (SSRF targets). Membership is
+// delegated to net.BlockList (the vetted stdlib implementation) rather than hand-rolled IP math.
+const BLOCKED_V4_RANGES: ReadonlyArray<readonly [string, number]> = [
['0.0.0.0', 8], // "this" network / unspecified
['10.0.0.0', 8], // RFC1918 private
['100.64.0.0', 10], // CGNAT
@@ -91,12 +82,24 @@ const BLOCKED_V4: ReadonlyArray<readonly [string, number]> = [
['240.0.0.0', 4], // reserved
];
-/**
- * Whether an IP literal points at an internal/reserved range that an outbound
- * webhook must not be allowed to reach (loopback, RFC1918, link-local/metadata,
- * CGNAT, multicast, IPv6 loopback/ULA/link-local, IPv4-mapped variants).
- * Anything that isn't a recognizable public IP is treated as blocked (fail-closed).
- */
+// IPv6 ranges with the same rule. The IPv4-embedding ranges (6to4 2002::/16, NAT64 64:ff9b::/96,
+// IPv4-compatible ::/96, mapped/RFC6052 ::ffff:0:0/96) are deliberately NOT here: an embedding of a
+// genuinely public IPv4 must stay reachable, so those are classified per-address by the
+// decapsulation ladder in isBlockedAddress, not blanket-blocked by range.
+const BLOCKED_V6_RANGES: ReadonlyArray<readonly [string, number]> = [
+ ['::', 128], // unspecified
+ ['::1', 128], // loopback
+ ['fc00::', 7], // ULA (RFC 4193)
+ ['fe80::', 10], // link-local
+ ['fec0::', 10], // deprecated site-local (RFC 3879)
+];
+
+const BLOCKED_V4 = new BlockList();
+for (const [subnet, prefix] of BLOCKED_V4_RANGES) BLOCKED_V4.addSubnet(subnet, prefix, 'ipv4');
+
+const BLOCKED_V6 = new BlockList();
+for (const [subnet, prefix] of BLOCKED_V6_RANGES) BLOCKED_V6.addSubnet(subnet, prefix, 'ipv6');
+
/** Two 16-bit hextets → dotted IPv4 string (for IPv4-in-IPv6 embeddings like ::ffff:, 6to4, NAT64). */
function hextetsToV4(hi: number, lo: number): string {
return `${(hi >> 8) & 0xff}.${hi & 0xff}.${(lo >> 8) & 0xff}.${lo & 0xff}`;
@@ -129,15 +132,21 @@ function expandIPv6(lower: string): number[] | null {
return nums.some(n => Number.isNaN(n)) ? null : nums;
}
+/**
+ * Whether an IP literal points at an internal/reserved range that an outbound
+ * webhook must not be allowed to reach (loopback, RFC1918, link-local/metadata,
+ * CGNAT, multicast, IPv6 loopback/ULA/link-local, IPv4-mapped variants).
+ * Anything that isn't a recognizable public IP is treated as blocked (fail-closed).
+ */
export function isBlockedAddress(ip: string): boolean {
+ // The address family is passed explicitly to check(): without it, an IPv6 literal silently
+ // answers false (BlockList defaults to interpreting the input as IPv4), which would fail open.
if (isIPv4(ip)) {
- const n = ipv4ToInt(ip);
- return BLOCKED_V4.some(([base, bits]) => inCidr4(n, base, bits));
+ return BLOCKED_V4.check(ip, 'ipv4');
}
if (isIPv6(ip)) {
const lower = ip.toLowerCase();
- if (lower === '::1' || lower === '::') return true;
// IPv4-mapped (::ffff:a.b.c.d or ::ffff:hhhh:hhhh) — classify by the embedded IPv4, handling
// BOTH the dotted-decimal and the hex-hextet form (the hex form bypassed a dotted-only regex).
@@ -150,15 +159,10 @@ export function isBlockedAddress(ip: string): boolean {
if (hextets.length === 2 && hextets.every(h => /^[0-9a-f]{1,4}$/.test(h))) {
const hi = parseInt(hextets[0], 16);
const lo = parseInt(hextets[1], 16);
- return isBlockedAddress(`${(hi >> 8) & 0xff}.${hi & 0xff}.${(lo >> 8) & 0xff}.${lo & 0xff}`);
+ return isBlockedAddress(hextetsToV4(hi, lo));
}
}
- const firstHextet = lower.split(':')[0];
- if (firstHextet.startsWith('fc') || firstHextet.startsWith('fd')) return true; // ULA fc00::/7
- if (/^fe[89ab]/.test(firstHextet)) return true; // link-local fe80::/10
- if (/^fe[c-f]/.test(firstHextet)) return true; // deprecated site-local fec0::/10 (RFC 3879)
-
// IPv6 forms that embed an IPv4 — 6to4 (2002::/16), NAT64 (64:ff9b::/96), and the deprecated
// IPv4-compatible ::/96 — are classified by the embedded address so they reach the IPv4 blocklist,
// mirroring the ::ffff: handling above. The literal is fully expanded first so a compressed all-zero
@@ -203,7 +207,11 @@ export function isBlockedAddress(ip: string): boolean {
return isBlockedAddress(hextetsToV4(hextets[6], hextets[7]));
}
}
- return false;
+
+ // Reserved-range membership (unspecified, loopback, ULA, link-local, deprecated site-local) via
+ // the stdlib BlockList — numeric, so compressed and fully-expanded spellings of the same
+ // address answer alike. Embedded forms already returned above (public embeddings stay allowed).
+ return BLOCKED_V6.check(lower, 'ipv6');
}
// Not a valid IP literal — cannot verify, so block.