diff --git a/lib/resolv.rb b/lib/resolv.rb index bac338c..1e71a4b 100644 --- a/lib/resolv.rb +++ b/lib/resolv.rb @@ -3225,8 +3225,12 @@ class IPv6 ## # Compressed IPv6 address format a::b + # + # "::" stands for one or more groups of zeros, so at most 7 other + # groups may appear around it. Regex_CompressedHex = /\A + (?!:*(?:[0-9A-Fa-f]{1,4}:+){7}[0-9A-Fa-f]) ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) \z/x @@ -3241,8 +3245,12 @@ class IPv6 ## # Compressed IPv4 mapped IPv6 address format a::b:w.x.y.z + # + # The IPv4 address takes two groups and "::" at least one, so at most 5 + # other groups may appear. Regex_CompressedHex4Dec = /\A + (?!:*(?:[0-9A-Fa-f]{1,4}:+){6}[0-9A-Fa-f]) ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}:)*) (\d+)\.(\d+)\.(\d+)\.(\d+) diff --git a/test/resolv/test_addr.rb b/test/resolv/test_addr.rb index f701c31..26545f2 100644 --- a/test/resolv/test_addr.rb +++ b/test/resolv/test_addr.rb @@ -34,6 +34,40 @@ def test_valid_ipv6_link_local_address assert_match(Resolv::IPv6::Regex, "fe80::1%ruby_3.0.0-1", bug17524) end + def test_ipv6_compressed_group_count + # "::" stands for one or more zero groups, so at most 7 other groups + # (or 5 before an embedded IPv4 address) may appear around it. + %w[ + 1:2:3:4:5:6:7:8:: + ::1:2:3:4:5:6:7:8 + 1:2:3:4::5:6:7:8 + 1:2:3:4:5:6:7:8:9:: + 1:2:3:4:5:6::1.2.3.4 + ::1:2:3:4:5:6:1.2.3.4 + 1:2:3:4:5:6:7::1.2.3.4 + 1:2:3:4:5:6::7:1.2.3.4 + ].each do |addr| + assert_not_match(Resolv::IPv6::Regex, addr) + assert_not_match(Resolv::AddressRegex, addr) + assert_raise_with_message(ArgumentError, /not numeric IPv6 address/, addr) do + Resolv::IPv6.create(addr) + end + end + + { + "1:2:3:4:5:6:7::" => "1:2:3:4:5:6:7:0", + "::2:3:4:5:6:7:8" => "0:2:3:4:5:6:7:8", + "1:2:3::5:6:7:8" => "1:2:3:0:5:6:7:8", + "::" => "::", + "1:2:3:4:5::1.2.3.4" => "1:2:3:4:5:0:102:304", + "::2:3:4:5:6:1.2.3.4" => "0:2:3:4:5:6:102:304", + "::1.2.3.4" => "::102:304", + }.each do |addr, expected| + assert_match(Resolv::IPv6::Regex, addr) + assert_equal(expected, Resolv::IPv6.create(addr).to_s, addr) + end + end + def test_valid_socket_ip_address_list Socket.ip_address_list.each do |addr| ip = addr.ip_address