Skip to content
Merged
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
33 changes: 31 additions & 2 deletions lib/net/http/persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
# #verify_callback :: For server certificate verification
# #verify_depth :: Depth of certificate verification
# #verify_mode :: How connections should be verified
# #verify_hostname :: Use hostname verification for server certificate
# during the handshake
#
# == Proxies
#
Expand Down Expand Up @@ -447,6 +449,21 @@ def self.detect_idle_timeout uri, max = 10

attr_reader :verify_mode

##
# HTTPS verify_hostname.
#
# If a client sets this to true and enables SNI with SSLSocket#hostname=,
# the hostname verification on the server certificate is performed
# automatically during the handshake using
# OpenSSL::SSL.verify_certificate_identity().
#
# You can set +verify_hostname+ as true to use hostname verification
# during the handshake.
#
# NOTE: This works with Ruby > 3.0.

attr_reader :verify_hostname

##
# Creates a new Net::HTTP::Persistent.
#
Expand Down Expand Up @@ -506,6 +523,7 @@ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
@verify_callback = nil
@verify_depth = nil
@verify_mode = nil
@verify_hostname = nil
@cert_store = nil

@generation = 0 # incremented when proxy URI changes
Expand Down Expand Up @@ -963,8 +981,10 @@ def ssl connection
connection.min_version = @min_version if @min_version
connection.max_version = @max_version if @max_version

connection.verify_depth = @verify_depth
connection.verify_mode = @verify_mode
connection.verify_depth = @verify_depth
connection.verify_mode = @verify_mode
connection.verify_hostname = @verify_hostname if
@verify_hostname && connection.respond_to?(:verify_hostname=)

if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then
Expand Down Expand Up @@ -1073,6 +1093,15 @@ def verify_mode= verify_mode
reconnect_ssl
end

##
# Sets the HTTPS verify_hostname. Defaults to false.

def verify_hostname= verify_hostname
@verify_hostname = verify_hostname

reconnect_ssl
end

##
# SSL verification callback.

Expand Down
16 changes: 16 additions & 0 deletions test/test_net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,7 @@ def test_ssl
assert_equal OpenSSL::SSL::VERIFY_PEER, c.verify_mode
assert_kind_of OpenSSL::X509::Store, c.cert_store
assert_nil c.verify_callback
assert_nil c.verify_hostname if c.respond_to?(:verify_hostname)
end

def test_ssl_ca_file
Expand Down Expand Up @@ -1332,6 +1333,21 @@ def test_ssl_verify_mode
assert_equal OpenSSL::SSL::VERIFY_NONE, c.verify_mode
end

def test_ssl_verify_hostname
skip 'OpenSSL is missing' unless HAVE_OPENSSL

@http.verify_hostname = true
c = Net::HTTP.new 'localhost', 80

skip 'net/http doesn\'t provide verify_hostname= method' unless
c.respond_to?(:verify_hostname=)

@http.ssl c

assert c.use_ssl?
assert c.verify_hostname
end

def test_ssl_warning
skip 'OpenSSL is missing' unless HAVE_OPENSSL

Expand Down