houseabsolute / Data-Validate-IP

IPv4 and IPv6 validation methods
https://metacpan.org/release/Data-Validate-IP/
Other
4 stars 1 forks source link

is_public_ipv6 throws an exception on 0000:00:0000:0000:0000:0000:0000:00000 #6

Closed oschwald closed 8 years ago

oschwald commented 8 years ago
is_public_ipv6('0000:00:0000:0000:0000:0000:0000:00000')

throws:

Bad arg length for NetAddr::IP::Util::sub128, length is 0, should be 128 at lib/site_perl/5.20.2/x86_64-linux/NetAddr/IP/Lite.pm line 1360.
oschwald commented 8 years ago

The cause of this is that inet_pton ignores leading zeros, which causes this module to think things like ::00000000000 are valid IPv6 addresses when using the fast check. This invalid address is then passed to NetAddr::IP->new, which returns undef, leading to the exception above.

I attempted to add a check to _fast_is_ipv6 to check if that string had more than four hexadecimal digits in a row, but this ended up making it approximately the same speed as _slow_is_ipv6 according to the benchmark.

Possible solutions:

  1. Remove _fast_is_ipv6
  2. Only enable _fast_is_ipv6 if inet_pton does not have this behavior
  3. Come up with some ingenious fast check that does not have these problems but is fast enough to warrant maintaining the second code path.

Although (2) makes sense, if there aren't any or many such platforms, the code would go untested and likely break without anyone noticing.