Skip to content

Commit e905309

Browse files
authored
Merge pull request #120 from cosmo0920/handle-verify_hostname-in-ssl-context
Handle Net::HTTP#verify_hostname in SSL Context if available
2 parents 12a9230 + 123f97d commit e905309

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

lib/net/http/persistent.rb

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@
7373
# #verify_callback :: For server certificate verification
7474
# #verify_depth :: Depth of certificate verification
7575
# #verify_mode :: How connections should be verified
76+
# #verify_hostname :: Use hostname verification for server certificate
77+
# during the handshake
7678
#
7779
# == Proxies
7880
#
@@ -454,6 +456,21 @@ def self.detect_idle_timeout uri, max = 10
454456

455457
attr_reader :verify_mode
456458

459+
##
460+
# HTTPS verify_hostname.
461+
#
462+
# If a client sets this to true and enables SNI with SSLSocket#hostname=,
463+
# the hostname verification on the server certificate is performed
464+
# automatically during the handshake using
465+
# OpenSSL::SSL.verify_certificate_identity().
466+
#
467+
# You can set +verify_hostname+ as true to use hostname verification
468+
# during the handshake.
469+
#
470+
# NOTE: This works with Ruby > 3.0.
471+
472+
attr_reader :verify_hostname
473+
457474
##
458475
# Creates a new Net::HTTP::Persistent.
459476
#
@@ -513,6 +530,7 @@ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
513530
@verify_callback = nil
514531
@verify_depth = nil
515532
@verify_mode = nil
533+
@verify_hostname = nil
516534
@cert_store = nil
517535

518536
@generation = 0 # incremented when proxy URI changes
@@ -980,8 +998,10 @@ def ssl connection
980998
connection.min_version = @min_version if @min_version
981999
connection.max_version = @max_version if @max_version
9821000

983-
connection.verify_depth = @verify_depth
984-
connection.verify_mode = @verify_mode
1001+
connection.verify_depth = @verify_depth
1002+
connection.verify_mode = @verify_mode
1003+
connection.verify_hostname = @verify_hostname if
1004+
@verify_hostname && connection.respond_to?(:verify_hostname=)
9851005

9861006
if OpenSSL::SSL::VERIFY_PEER == OpenSSL::SSL::VERIFY_NONE and
9871007
not Object.const_defined?(:I_KNOW_THAT_OPENSSL_VERIFY_PEER_EQUALS_VERIFY_NONE_IS_WRONG) then
@@ -1090,6 +1110,15 @@ def verify_mode= verify_mode
10901110
reconnect_ssl
10911111
end
10921112

1113+
##
1114+
# Sets the HTTPS verify_hostname. Defaults to false.
1115+
1116+
def verify_hostname= verify_hostname
1117+
@verify_hostname = verify_hostname
1118+
1119+
reconnect_ssl
1120+
end
1121+
10931122
##
10941123
# SSL verification callback.
10951124

test/test_net_http_persistent.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,7 @@ def test_ssl
12591259
assert_equal OpenSSL::SSL::VERIFY_PEER, c.verify_mode
12601260
assert_kind_of OpenSSL::X509::Store, c.cert_store
12611261
assert_nil c.verify_callback
1262+
assert_nil c.verify_hostname if c.respond_to?(:verify_hostname)
12621263
end
12631264

12641265
def test_ssl_ca_file
@@ -1342,6 +1343,21 @@ def test_ssl_verify_mode
13421343
assert_equal OpenSSL::SSL::VERIFY_NONE, c.verify_mode
13431344
end
13441345

1346+
def test_ssl_verify_hostname
1347+
skip 'OpenSSL is missing' unless HAVE_OPENSSL
1348+
1349+
@http.verify_hostname = true
1350+
c = Net::HTTP.new 'localhost', 80
1351+
1352+
skip 'net/http doesn\'t provide verify_hostname= method' unless
1353+
c.respond_to?(:verify_hostname=)
1354+
1355+
@http.ssl c
1356+
1357+
assert c.use_ssl?
1358+
assert c.verify_hostname
1359+
end
1360+
13451361
def test_ssl_warning
13461362
skip 'OpenSSL is missing' unless HAVE_OPENSSL
13471363

0 commit comments

Comments
 (0)