Skip to content

Commit 9f2bd5f

Browse files
hsbtclaude
andcommitted
Stop vendoring resolv for two regexps
Vendored net/http loaded the whole 3500-line DNS resolver only to match Resolv::IPv4::Regex and Resolv::IPv6::Regex when deciding whether an address may be sent as a Server Name Indication. Inline equivalent regexps instead, so that a resolver RubyGems never resolves with is no longer shipped and no longer shows up as an outdated copy in security audits of the Ruby tarball. https://bugs.ruby-lang.org/issues/22317 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
1 parent 7e21899 commit 9f2bd5f

10 files changed

Lines changed: 105 additions & 3620 deletions

File tree

‎Manifest.txt‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,6 @@ lib/rubygems/vendor/pub_grub/lib/pub_grub/version_constraint.rb
533533
lib/rubygems/vendor/pub_grub/lib/pub_grub/version_range.rb
534534
lib/rubygems/vendor/pub_grub/lib/pub_grub/version_solver.rb
535535
lib/rubygems/vendor/pub_grub/lib/pub_grub/version_union.rb
536-
lib/rubygems/vendor/resolv/COPYING
537-
lib/rubygems/vendor/resolv/lib/resolv.rb
538536
lib/rubygems/vendor/securerandom/COPYING
539537
lib/rubygems/vendor/securerandom/lib/securerandom.rb
540538
lib/rubygems/vendor/timeout/COPYING

‎lib/rubygems/vendor/net-http/lib/net/http.rb‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
require_relative '../../../net-protocol/lib/net/protocol'
2424
require_relative '../../../uri/lib/uri'
25-
require_relative '../../../resolv/lib/resolv'
2625
autoload :OpenSSL, 'openssl'
2726

2827
module Gem::Net #:nodoc:
@@ -1535,6 +1534,29 @@ def use_ssl=(flag)
15351534

15361535
SSL_IVNAMES = SSL_ATTRIBUTES.map { |a| "@#{a}".to_sym }.freeze # :nodoc:
15371536

1537+
# Resolv::IPv4::Regex and Resolv::IPv6::Regex, copied from resolv 0.7.1 rather
1538+
# than required, so that net/http does not load a DNS resolver just to keep an
1539+
# IP address literal out of the Server Name Indication.
1540+
# https://github.com/ruby/resolv/blob/v0.7.1/lib/resolv.rb
1541+
ipv4_octet = /0|1(?:[0-9][0-9]?)?|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?|[3-9][0-9]?/
1542+
hex16 = /[0-9A-Fa-f]{1,4}/
1543+
hex16_group = /(?:#{hex16}(?::#{hex16})*)?/
1544+
dotted_quad = /\d+\.\d+\.\d+\.\d+/
1545+
zone_id = /%[-0-9A-Za-z._~]+/
1546+
1547+
IPV4_ADDRESS = /\A(?:#{ipv4_octet})\.(?:#{ipv4_octet})\.(?:#{ipv4_octet})\.(?:#{ipv4_octet})\z/ # :nodoc:
1548+
1549+
IPV6_ADDRESS = /\A(?:
1550+
(?:#{hex16}:){7}#{hex16} # a:b:c:d:e:f:g:h
1551+
| #{hex16_group}::#{hex16_group} # a::b
1552+
| (?:#{hex16}:){6}#{dotted_quad} # a:b:c:d:e:f:w.x.y.z
1553+
| #{hex16_group}::(?:#{hex16}:)*#{dotted_quad} # a::b:w.x.y.z
1554+
| [Ff][Ee]80(?::#{hex16}){7}#{zone_id} # fe80:b:c:d:e:f:g:h%em1
1555+
| [Ff][Ee]80:(?:#{hex16_group}::#{hex16_group}|:#{hex16_group})?:#{hex16}#{zone_id} # fe80::b%em1
1556+
)\z/x # :nodoc:
1557+
1558+
private_constant :IPV4_ADDRESS, :IPV6_ADDRESS
1559+
15381560
# Sets or returns the path to a CA certification file in PEM format.
15391561
attr_accessor :ca_file
15401562

@@ -1734,7 +1756,7 @@ def connect
17341756

17351757
# Server Name Indication (SNI) RFC 3546/6066
17361758
case @address
1737-
when Gem::Resolv::IPv4::Regex, Gem::Resolv::IPv6::Regex
1759+
when IPV4_ADDRESS, IPV6_ADDRESS
17381760
# don't set SNI, as IP addresses in SNI is not valid
17391761
# per RFC 6066, section 3.
17401762

‎lib/rubygems/vendor/resolv/COPYING‎

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)