Skip to content

fix: match IPv4-mapped IPv6 addresses against IPv4 CIDRs in ipMatch - #586

Open
bardrop wants to merge 1 commit into
apache:masterfrom
bardrop-oss:fix-ipmatch-ipv4-mapped
Open

bardrop wants to merge 1 commit into
apache:masterfrom
bardrop-oss:fix-ipmatch-ipv4-mapped

Conversation

@bardrop

@bardrop bardrop commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

ipMatch never matched an IPv4-mapped IPv6 address against an IPv4 CIDR:

ipMatch('::ffff:192.168.2.123', '192.168.2.0/24') // false
ipMatch('::ffff:192.168.2.123', '192.168.2.123')  // true

This is the form Node uses for IPv4 clients on a dual-stack server (server.listen() binds :: by default, so req.socket.remoteAddress is ::ffff:192.168.2.123). A policy like ipMatch(r.ip, '192.168.2.0/24') therefore denied every IPv4 client, while an exact-IP policy worked.

subnet().contains() masked the low 4 bytes of the IPv6 address and then compared a 16-byte result with the 4-byte network, so it was always false. Now, when the subnet is IPv4, an address in ::ffff:0:0/96 is read as its IPv4 address first. Other IPv6 addresses (::c0a8:27b, 2001:db8::c0a8:27b) still never match an IPv4 CIDR. This is the same as Go Casbin, where net.IPNet.Contains does the To4() step.

Testing

  • New test in test/util.test.ts fails before (Expected: true, Received: false) and passes after.
  • Checked the same inputs with Go's net.ParseCIDR(...).Contains(net.ParseIP(...)) (what Go util.IPMatch uses): same result for all of them.
  • jest (17 suites, 240 passed), eslint . --ext .js,.ts, tsc clean.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant