Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(security): block IPv6 outside the 2000::/3 global-unicast range
Yudhi Armyndharis3e98c10f2e75
diff --git a/src/common/security/ssrf-guard.spec.ts b/src/common/security/ssrf-guard.spec.tsindex cbea33f4b..94b4f0254 100644--- a/src/common/security/ssrf-guard.spec.ts+++ b/src/common/security/ssrf-guard.spec.ts@@ -45,6 +45,9 @@ describe('isBlockedAddress', () => {['feff::1', 'IPv6 site-local upper bound feff'],['fc0::1', 'IETF-reserved 0000::/3 — not ULA despite the fc prefix'],['fe8::1', 'IETF-reserved 0000::/3 — not link-local despite the fe prefix'],+ ['ff02::1', 'IPv6 multicast ff00::/8 (all-nodes) — same posture as IPv4 224.0.0.0/4'],+ ['4000::1', 'reserved above the 2000::/3 global-unicast range (4000::/3)'],+ ['e000::1', 'reserved above the 2000::/3 global-unicast range (e000::/3)'],['::ffff:127.0.0.1', 'IPv4-mapped loopback (dotted)'],['::ffff:7f00:1', 'IPv4-mapped loopback (hex)'],['::ffff:0a00:0001', 'IPv4-mapped RFC1918 (hex, zero-padded)'],@@ -75,6 +78,8 @@ describe('isBlockedAddress', () => {['1.1.1.1', 'public IPv4'],['172.32.0.1', 'just outside 172.16/12'],['2001:4860:4860::8888', 'public IPv6'],+ ['2606:4700::1111', 'public IPv6 in 2000::/3 (RFC 4291 global unicast) stays allowed'],+ ['64:ff9b::8.8.8.8', 'NAT64 of public 8.8.8.8 (dotted tail) stays allowed via the ladder'],['::ffff:0808:0808', 'IPv4-mapped public 8.8.8.8 (hex)'],['::ffff:8.8.8.8', 'IPv4-mapped public 8.8.8.8 (dotted) — the reserved-block catch must not swallow it'],['2002:0808:0808::', '6to4 of public 8.8.8.8 stays allowed'],diff --git a/src/common/security/ssrf-guard.ts b/src/common/security/ssrf-guard.tsindex 0c1819fb2..409100fbc 100644--- a/src/common/security/ssrf-guard.ts+++ b/src/common/security/ssrf-guard.ts@@ -103,6 +103,13 @@ for (const [subnet, prefix] of BLOCKED_V4_RANGES) BLOCKED_V4.addSubnet(subnet, pconst BLOCKED_V6 = new BlockList();for (const [subnet, prefix] of BLOCKED_V6_RANGES) BLOCKED_V6.addSubnet(subnet, prefix, 'ipv6');+// The single IPv6 ALLOW: RFC 4291's global-unicast 2000::/3. The fallthrough in isBlockedAddress+// allows only this range — everything outside it that no earlier branch classified (IPv6 multicast+// ff00::/8, the reserved blocks above 3fff:: — 4000::/3, 5f00::/16, 8000::/3, c000::/3, e000::/3)+// blocks, mirroring the IPv4 posture where multicast 224.0.0.0/4 and reserved 240.0.0.0/4 block.+const GLOBAL_UNICAST_V6 = new BlockList();+GLOBAL_UNICAST_V6.addSubnet('2000::', 3, '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}`;@@ -213,9 +220,11 @@ export function isBlockedAddress(ip: string): boolean {// Reserved-range membership (unspecified, loopback, ULA, link-local, deprecated site-local,// IETF-reserved 0000::/3) via the stdlib BlockList — numeric, so compressed and fully-expanded- // spellings of the same address answer alike. Embedded forms already returned above (public- // embeddings, NAT64 included, stay allowed — only the ladder decides those).- return BLOCKED_V6.check(lower, 'ipv6');+ // spellings of the same address answer alike — and beyond that only RFC 4291 global unicast+ // (2000::/3) is allowed: IPv6 multicast (ff00::/8) and the reserved blocks above 3fff:: block+ // too, the same fail-closed posture as IPv4's multicast/reserved ranges. Embedded forms already+ // returned above (public embeddings, NAT64 included, stay allowed — only the ladder decides those).+ return BLOCKED_V6.check(lower, 'ipv6') || !GLOBAL_UNICAST_V6.check(lower, 'ipv6');}// Not a valid IP literal — cannot verify, so block.