Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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+)
Expand Down
34 changes: 34 additions & 0 deletions test/resolv/test_addr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down