Bug #5468

IPSocket#valid_v6? in ipaddr.rb contains incorrect regexps

Added by James Harton over 2 years ago. Updated about 1 year ago.

Assignee:Akinori MUSHA
Target version:next minor
ruby -v:ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0] Backport:


Code to reproduce:

require 'ipaddr'
IPAddr.new('FF32:00FF:A12:34FF:FE56:7890::/96', Socket::AF_INET) # See RFC4489

Fails with:

ArgumentError: invalid address
    from /Users/jnh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/ipaddr.rb:477:in `rescue in initialize'
    from /Users/jnh/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/ipaddr.rb:474:in `initialize'
    from (irb):2:in `new'
    from (irb):2
    from /Users/jnh/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>'

I have tested on both 1.9.2 and 1.8.7:

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin11.1.0]
ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0]

Looking over #validv6 it appears that it contains a number of invalid regular expressions (IPv4 compatible IPv6 addressing was deprecated by RFC4291 in 2006 for example). I would suggest that all the regular expressions are removed and the operating systems inetpton() call is used to validate it instead.


#1 Updated by George Koehler over 2 years ago

'FF32:00FF:A12:34FF:FE56:7890::/96' is an IPv6 prefix, but Socket::AFINET means IPv4 address. It works if I say Socket::AFINET6.

$ ruby -v
ruby 1.9.4dev (2011-10-18 trunk 33473) [x8664-openbsd4.9]
$ irb
irb(main):001:0> require 'ipaddr'
=> true
irb(main):002:0> IPAddr.new('FF32:00FF:A12:34FF:FE56:7890::/96', Socket::AF
ArgumentError: address family mismatch
from /home/kernigh/prefix/lib/ruby/1.9.1/ipaddr.rb:510:in initialize'
from (irb):2:in
from (irb):2
from /home/kernigh/prefix/bin/irb:12:in `'
irb(main):003:0> IPAddr.new('FF32:00FF:A12:34FF:FE56:7890::/96', Socket::AF_INET6)
=> #

#2 Updated by James Harton over 2 years ago

Apologies, you're right. That was a stupid mistake to make :)
However, I'm still getting an invalid address:

ruby-1.9.3-p0 :004 > IPAddr.new('FF32:00FF:A12:34FF:FE56:7890::/96', Socket::AF_INET6)
ArgumentError: invalid address
    from /Users/jnh/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/ipaddr.rb:496:in `rescue in initialize'
    from /Users/jnh/.rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/ipaddr.rb:493:in `initialize'
    from (irb):4:in `new'
    from (irb):4
    from /Users/jnh/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]

I'm presuming this is fixed in 1.9.4 then?

#3 Updated by Koichi Sasada about 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Akinori MUSHA

#4 Updated by Yui NARUSE about 1 year ago

  • Target version changed from 1.9.2 to next minor

Also available in: Atom PDF