From 3ec120f7d5a0d3cb68f36e8c8472db7e3a316610 Mon Sep 17 00:00:00 2001 From: Mradul Singh Date: Wed, 18 Nov 2026 10:41:54 +0530 Subject: [PATCH] Reject compressed IPv6 addresses with too many groups "::" stands for one or more groups of zeros, so a compressed address can have at most 7 other groups (5 before an embedded IPv4 address). IPv6::Regex did not count the groups: * "1:2:3:4:5:6:7:8::" matched and IPv6.create silently dropped the "::", returning 1:2:3:4:5:6:7:8. * "1:2:3:4:5:6:7:8:9::" and "1:2:3:4:5:6:7::1.2.3.4" matched, but IPv6.create failed with "negative argument" from String#*. Resolv.getaddress returned such strings as addresses without a lookup, and Resolv::DNS#getname raised the "negative argument" ArgumentError. Add a lookahead to Regex_CompressedHex and Regex_CompressedHex4Dec that rejects too many groups. --- lib/resolv.rb | 8 ++++++++ test/resolv/test_addr.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) 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