Skip to content

Reject compressed IPv6 addresses with too many groups - #167

Open
bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-ipv6-compressed-group-count
Open

bardrop wants to merge 1 commit into
ruby:masterfrom
bardrop-oss:fix-ipv6-compressed-group-count

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

:: stands for one or more groups of zeros (RFC 4291 2.2), so a compressed address can have at most 7 other groups, or 5 before an embedded IPv4 address. IPv6::Regex did not count the groups:

Resolv::IPv6.create("1:2:3:4:5:6:7:8::").to_s      #=> "1:2:3:4:5:6:7:8"  (the "::" is silently dropped)
Resolv::IPv6.create("1:2:3:4:5:6:7:8:9::")         #=> ArgumentError: negative argument
Resolv::IPv6.create("1:2:3:4:5:6:7::1.2.3.4")      #=> ArgumentError: negative argument
Resolv::IPv6::Regex.match?("1:2:3:4:5:6:7:8:9::")  #=> true

The "negative argument" comes from "\0" * omitlen when the groups already fill more than 16 bytes. Because Resolv::AddressRegex also matched, Resolv.getaddress("1:2:3:4:5:6:7:8:9::") returned the string as an address without a lookup, and Resolv::DNS.new.getname raised the ArgumentError.

This adds a lookahead to Regex_CompressedHex and Regex_CompressedHex4Dec that rejects too many groups, so these strings no longer match and IPv6.create raises the usual "not numeric IPv6 address" error.

The new test fails without the change. rake test passes. I also compared IPv6::Regex + IPv6.create against Python's ipaddress on about 19k random addresses: after this change the only remaining difference is an embedded IPv4 part with an octet over 255 (like ::1.2.3.256), which the regex still matches but create already rejects with the normal error. I left that alone.

"::" 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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant