Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPv6 CIDR contains() returns true when it should return false #89

Open
natesilva opened this issue Feb 13, 2018 · 3 comments
Open

IPv6 CIDR contains() returns true when it should return false #89

natesilva opened this issue Feb 13, 2018 · 3 comments

Comments

@natesilva
Copy link

Here’s an example:

ip.cidrSubnet('2001:db8:f53b::/48').contains('2001:db8:f53a::1');
// returns: true, should return false

ip.cidrSubnet('2001:db8:f53b::/128').contains('2001:db8:f53a::1');
// returns: true, should return false

This seems obvious so, probably I’m using the library incorrectly, but I can’t see how to fix it.

@willdougadams
Copy link

+1

Looks to me like the mask always assumes v4, any mask value over 32 will produce a subnet with numHosts < 1

> for (var i=0; i<128; i++) {
...  console.log('cidr ::1/' + i + '  numHosts: ' + ip.cidrSubnet(`::1/${i}`).numHosts)
...}
cidr ::1/0  numHosts: 4294967294
cidr ::1/1  numHosts: 2147483646

... etc ...

cidr ::1/29  numHosts: 6
cidr ::1/30  numHosts: 2
cidr ::1/31  numHosts: 2
cidr ::1/32  numHosts: 1
cidr ::1/33  numHosts: 0.5
cidr ::1/34  numHosts: 0.25
cidr ::1/35  numHosts: 0.125

... etc ...

cidr ::1/127  numHosts: 2.524354896707238e-29

The tests for the cidrSubnet method here don't test for v6, so it may be that this was intended.

What we can do with v6 is ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/128') and do some string comparison with the result.

> ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/128')
'1111:2222:3333:4444:5555:6666:7777:8888'
> ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/64')
'1111:2222:3333:4444::'
> ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/32')
'7777:8888::'
> ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/16')
'7777::'
> ip.cidr('1111:2222:3333:4444:5555:6666:7777:8888/1')
'::'

Would be nice if cidrSubnet() threw an error/warning about this behavior when it detects a v6 address :)

@clarsen
Copy link

clarsen commented Aug 31, 2019

FWIW, I've found that https://www.npmjs.com/package/subnet-check seems to work for both ipv4 and ipv6 for my purposes.

@natesilva
Copy link
Author

That looks like a fork of my package https://www.npmjs.com/package/is-in-subnet — yes, I wrote that specifically for this use case. I’m glad it works for you!

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

No branches or pull requests

3 participants