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
6 changes: 5 additions & 1 deletion lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
if defined?(OpenSSL::SSL)

require "io/nonblock"
require "ipaddr"
# Only OpenSSL::SSL.verify_certificate_identity uses IPAddr, and only for
# certificates carrying an iPAddress SAN. Autoloading keeps ipaddr (and the
# socket library it pulls in) off the require path for everyone else, while
# leaving the IPAddr constant resolvable as before.
autoload :IPAddr, "ipaddr"
require "socket"

module OpenSSL
Expand Down
24 changes: 24 additions & 0 deletions test/openssl/test_require.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
require_relative "utils"

class OpenSSL::TestRequire < OpenSSL::TestCase
IPADDR_LOADED = '$LOADED_FEATURES.any? { |f| File.basename(f) == "ipaddr.rb" }'

def subprocess(script)
lib = File.expand_path("../../lib", __dir__)
IO.popen([RbConfig.ruby, "-I", lib, "-e", script], &:read)
end

def test_requiring_openssl_does_not_load_ipaddr
assert_equal "false", subprocess("require 'openssl'; print #{IPADDR_LOADED}")
end

def test_ipaddr_is_still_reachable_after_requiring_openssl
assert_equal "127.0.0.1", subprocess("require 'openssl'; print IPAddr.new('127.0.0.1').to_s")
end

def test_referencing_ipaddr_loads_it
script = "require 'openssl'; IPAddr.new('127.0.0.1'); print #{IPADDR_LOADED}"
assert_equal "true", subprocess(script)
end
end