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

feat: regex for ipv6 #4

Merged
merged 2 commits into from
Nov 28, 2020

Conversation

vasco-santos
Copy link
Contributor

This PR adds regex for ipv6 addresses per #3 . I needed to do a special validation for addresses started by 0 as they were not being considered ipv4. Open to improve this solution

This was based on https://en.m.wikipedia.org/wiki/Reserved_IP_addresses#IPv6

cc @sickcodes @frenchbread

@sickcodes
Copy link
Contributor

I'll give it a test soon! Looks good.

@sickcodes
Copy link
Contributor

What was wrong with netmask?

@vasco-santos
Copy link
Contributor Author

vasco-santos commented Nov 26, 2020

netmask does not support ipv6 addresses

EDIT: rs/node-netmask#8 I would like support on that, but I am not familiar with coffeescript at all

Copy link
Contributor

@sickcodes sickcodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@sickcodes
Copy link
Contributor

npm run test
168 passed

Also tested the inverse of the test and 168 failed which is good.

Here's upstream of is-ip anyway: https://github.com/sindresorhus/ip-regex/blob/master/index.js

@frenchbread

@frenchbread
Copy link
Owner

Thanks for the PR! Good work. Works fine for me as well.

Could you please update the package version also, @vasco-santos & I'll merge it.

@vasco-santos
Copy link
Contributor Author

Sure, added a new commit for the release.

@frenchbread
Copy link
Owner

@vasco-santos Cool, thanks!

@frenchbread frenchbread merged commit ef75bd5 into frenchbread:master Nov 28, 2020
@@ -29,9 +31,27 @@ function netmaskCheck (params) {
for (let r of privateRanges) {
if (r.contains(params)) return true
}
return false
Copy link
Owner

@frenchbread frenchbread Nov 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, @vasco-santos, why did you remove this return statement? Now when I test it by passing public IP it returns undefiled rather than true.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh this is a bug! I was initially doing everything in the same function, as ipv6 being use d as a fallback for the ipv4 validation. But I moved later. I am going to PR this

}

export default (ip) => {
if (isIp.v4(ip) || ip.startsWith('0')) {
Copy link
Owner

@frenchbread frenchbread Nov 28, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, pardon me If I'm misunderstanding, but why add ip.startsWith('0') if netmaskCheck() already handles it? IMHO we could just have:

export default ip => netmaskCheck(ip) || ipv6Check(ip)

or

export default ip => {
  if (isIp.v4(ip)) return netmaskCheck(ip)
  else if (isIp.v6(ip)) return ipv6Check(ip)
  else return false
}

Update: isIp.v6(ip) check does not pass when running tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the first was my initial approach. But as I mentioned in the PR description the ipv4 with 0.0.0.0.0 is not a valid ipv4 per the isIp.v4(), which is correct... So this should not really be handled in ipv4.

The second approach suffer from the same issue I believe. 0.0.0.0.0 will not be nor ipv4, nor ipv6 and it will return false, when it should be true. We can figure out a better solution for such addresses

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've modified second code snippet:

export default ip => {
  if (isIp.v4(ip) || ip.startsWith('0')) return netmaskCheck(ip)
  else if (isIp.v6(ip)) return ipv6Check(ip)
  else return false
}

and getting this:

t.truthy(isPrivate(ip), true)
           |         |         
           false     "::ffff:0.0.255.255.255"
-----
t.truthy(isPrivate(ip), true)
           |         |         
           false     "::ffff:0.255.255.255.255"
-----
t.truthy(isPrivate(ip), true)
           |         |         
           false     "100::ffff::"

seems like isIp is not picking up on those...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, perhaps creating an issue to rethink this logic is a better way

@frenchbread
Copy link
Owner

Sorry, @vasco-santos, for rising these points after code has been merged. I am just wondering for future improvements.

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.

3 participants