Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/util/ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down