From e20c0da4530cc86f0c6fd3d36f5269b71f0eeb68 Mon Sep 17 00:00:00 2001 From: bardrop Date: Mon, 16 Nov 2026 17:32:44 +0530 Subject: [PATCH] fix: match IPv4-mapped IPv6 addresses against IPv4 CIDRs in ipMatch Node reports IPv4 clients of a dual-stack server as ::ffff:a.b.c.d, so ipMatch('::ffff:192.168.2.123', '192.168.2.0/24') returned false while ipMatch('::ffff:192.168.2.123', '192.168.2.123') returned true. Go Casbin (net.IPNet.Contains) returns true for both. An IPv4 subnet now reads an IPv4-mapped address (::ffff:0:0/96 only) as its IPv4 address before masking. --- src/util/ip.ts | 16 ++++++++++++++++ test/util.test.ts | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/util/ip.ts b/src/util/ip.ts index e093a70..fa34912 100644 --- a/src/util/ip.ts +++ b/src/util/ip.ts @@ -219,6 +219,11 @@ export const ip = { contains: function (other: string) { let otherBuffer: Buffer; try { + // An IPv4-mapped IPv6 address (::ffff:a.b.c.d) is the IPv4 address + // a.b.c.d, so compare it with an IPv4 subnet as IPv4. + if (networkBuffer.length === 4) { + other = ip.toIPv4IfMapped(other); + } otherBuffer = ip.toBuffer(ip.mask(other, mask)); } catch { return false; @@ -282,6 +287,17 @@ export const ip = { return true; }, + toIPv4IfMapped: function (addr: string): string { + if (!ip.isV6Format(addr)) { + return addr; + } + const buff = ip.toBuffer(addr); + for (let i = 0; i < 10; i++) { + if (buff[i] !== 0) return addr; + } + if (buff[10] !== 0xff || buff[11] !== 0xff) return addr; + return ip.toString(buff, 12, 4); + }, toLong: function (ip: string): number { let ipl = 0; ip.split('.').forEach(function (octet) { diff --git a/test/util.test.ts b/test/util.test.ts index 85bb91f..e70dbee 100644 --- a/test/util.test.ts +++ b/test/util.test.ts @@ -196,6 +196,24 @@ test('test ipMatchFunc - IPv6 CIDR containment is correct', () => { expect(util.ipMatchFunc('fe80::1', 'fd00::/8')).toEqual(false); }); +// Node reports IPv4 clients of a dual-stack server as IPv4-mapped IPv6 (::ffff:a.b.c.d). +test('test ipMatchFunc - IPv4-mapped IPv6 address in an IPv4 CIDR', () => { + expect(util.ipMatchFunc('::ffff:192.168.2.123', '192.168.2.0/24')).toEqual(true); + expect(util.ipMatchFunc('::ffff:c0a8:27b', '192.168.2.0/24')).toEqual(true); + expect(util.ipMatchFunc('::FFFF:192.168.2.123', '192.168.2.0/24')).toEqual(true); + expect(util.ipMatchFunc('::ffff:10.0.0.1', '0.0.0.0/0')).toEqual(true); + expect(util.ipMatchFunc('::ffff:203.0.113.42', '203.0.113.42/32')).toEqual(true); + + expect(util.ipMatchFunc('::ffff:192.168.3.1', '192.168.2.0/24')).toEqual(false); + expect(util.ipMatchFunc('::ffff:203.0.113.43', '203.0.113.42/32')).toEqual(false); + // Only ::ffff:0:0/96 is IPv4; other IPv6 addresses never match an IPv4 CIDR + expect(util.ipMatchFunc('::c0a8:27b', '192.168.2.0/24')).toEqual(false); + expect(util.ipMatchFunc('2001:db8::c0a8:27b', '192.168.2.0/24')).toEqual(false); + expect(util.ipMatchFunc('::1:ffff:c0a8:27b', '192.168.2.0/24')).toEqual(false); + // An overflowed octet must not sneak in through the mapped form + expect(util.ipMatchFunc('::ffff:448.168.2.10', '192.168.2.0/24')).toEqual(false); +}); + // CVE fix: garbage input like "abc" must be rejected, not silently match IPv6 policies. test('test ipMatchFunc - non-IP strings are rejected as ip1', () => { expect(() => util.ipMatchFunc('abc', '::1/128')).toThrow(Error);