Skip to content
Draft
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
120 changes: 76 additions & 44 deletions lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ class HTTPHeaderSyntaxError < StandardError; end
# Returns the proxy user name.
# - {:proxy_user=}[rdoc-ref:Net::HTTP#proxy_user=]:
# Sets the proxy user.
# - {#proxy_use_ssl?}[rdoc-ref:Net::HTTP#proxy_use_ssl?]:
# Returns whether TLS is used for connection to the proxy server.
# - {:proxy_use_ssl=}[rdoc-ref:Net::HTTP#proxy_use_ssl=]:
# Sets whether TLS is used for connection to the proxy server.
#
# === Security
#
Expand Down Expand Up @@ -1100,7 +1104,7 @@ class << HTTP
# For proxy-defining arguments +p_addr+ through +p_no_proxy+,
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
#
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = nil)
def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_no_proxy = nil, p_use_ssl = false)
http = super address, port

if proxy_class? then # from Net::HTTP::Proxy()
Expand All @@ -1109,7 +1113,6 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
http.proxy_port = @proxy_port
http.proxy_user = @proxy_user
http.proxy_pass = @proxy_pass
http.proxy_use_ssl = @proxy_use_ssl
elsif p_addr == :ENV then
http.proxy_from_env = true
else
Expand Down Expand Up @@ -1190,7 +1193,7 @@ def initialize(address, port = nil) # :nodoc:
@proxy_port = nil
@proxy_user = nil
@proxy_pass = nil
@proxy_use_ssl = nil
@proxy_use_ssl = false

@use_ssl = false
@ssl_context = nil
Expand Down Expand Up @@ -1326,7 +1329,7 @@ def response_body_encoding=(value)
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
attr_writer :proxy_pass

# Sets whether the proxy uses SSL;
# Sets whether TLS is used for the connection to the proxy server;
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
attr_writer :proxy_use_ssl

Expand Down Expand Up @@ -1566,6 +1569,15 @@ def use_ssl=(flag)

private_constant :IPV4_ADDRESS, :IPV6_ADDRESS

def ssl_use_sni_for?(address)
case address
when IPV4_ADDRESS, IPV6_ADDRESS
return false
end
true
end
private :ssl_use_sni_for?

# Sets or returns the path to a CA certification file in PEM format.
attr_accessor :ca_file

Expand Down Expand Up @@ -1689,41 +1701,36 @@ def do_start
private :do_start

def connect
if use_ssl?
if use_ssl? || proxy_use_ssl?
# reference early to load OpenSSL before connecting,
# as OpenSSL may take time to load.
@ssl_context = OpenSSL::SSL::SSLContext.new
OpenSSL
end

if proxy? then
conn_addr = proxy_address
conn_port = proxy_port
if proxy?
debug "opening connection to proxy server #{proxy_address}:#{proxy_port}..."
s = timeouted_connect(proxy_address, proxy_port)
if proxy_use_ssl?
debug "starting SSL for #{proxy_address}:#{proxy_port}..."
s = OpenSSL::SSL::SSLSocket.new(s, proxy_ssl_context)
s.sync_close = true
s.hostname = proxy_address if ssl_use_sni_for?(proxy_address)
ssl_socket_connect(s, @open_timeout)
if proxy_ssl_context.verify_mode != OpenSSL::SSL::VERIFY_NONE &&
!ssl_use_sni_for?(proxy_address)
s.post_connection_check(proxy_address)
end
debug "SSL established to proxy server, " \
"protocol: #{s.ssl_version}, cipher: #{s.cipher[0]}"
end
else
conn_addr = conn_address
conn_port = port
debug "opening connection to #{conn_address}:#{port}..."
s = timeouted_connect(conn_address, port)
end

debug "opening connection to #{conn_addr}:#{conn_port}..."
begin
s = timeouted_connect(conn_addr, conn_port)
rescue => e
if (defined?(IO::TimeoutError) && e.is_a?(IO::TimeoutError)) || e.is_a?(Errno::ETIMEDOUT) # for compatibility with previous versions
e = Net::OpenTimeout.new(e)
end
raise e, "Failed to open TCP connection to " +
"#{conn_addr}:#{conn_port} (#{e.message})"
end
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
debug "opened"
if use_ssl?
if proxy?
if @proxy_use_ssl
proxy_sock = OpenSSL::SSL::SSLSocket.new(s)
ssl_socket_connect(proxy_sock, @open_timeout)
else
proxy_sock = s
end
proxy_sock = BufferedIO.new(proxy_sock, read_timeout: @read_timeout,
plain_sock = BufferedIO.new(s, read_timeout: @read_timeout,
write_timeout: @write_timeout,
continue_timeout: @continue_timeout,
debug_output: @debug_output)
Expand All @@ -1740,8 +1747,8 @@ def connect
buf << "Proxy-Authorization: Basic #{credential}\r\n"
end
buf << "\r\n"
proxy_sock.write(buf)
HTTPResponse.read_new(proxy_sock).value
plain_sock.write(buf)
HTTPResponse.read_new(plain_sock).value
# assuming nothing left in buffers after successful CONNECT response
end

Expand All @@ -1755,6 +1762,7 @@ def connect
end
end
end
@ssl_context = OpenSSL::SSL::SSLContext.new
@ssl_context.set_params(ssl_parameters)
unless @ssl_context.session_cache_mode.nil? # a dummy method on JRuby
@ssl_context.session_cache_mode =
Expand All @@ -1770,18 +1778,17 @@ def connect
verify_hostname = @ssl_context.verify_hostname

# Server Name Indication (SNI) RFC 3546/6066
case @address
when IPV4_ADDRESS, IPV6_ADDRESS
if ssl_use_sni_for?(@address)
ssl_host_address = @address
else
# don't set SNI, as IP addresses in SNI is not valid
# per RFC 6066, section 3.

# Avoid openssl warning
@ssl_context.verify_hostname = false
else
ssl_host_address = @address
end

debug "starting SSL for #{conn_addr}:#{conn_port}..."
debug "starting SSL for #{conn_address}:#{port}..."
s = OpenSSL::SSL::SSLSocket.new(s, @ssl_context)
s.sync_close = true
s.hostname = ssl_host_address if s.respond_to?(:hostname=) && ssl_host_address
Expand All @@ -1796,6 +1803,7 @@ def connect
end
debug "SSL established, protocol: #{s.ssl_version}, cipher: #{s.cipher[0]}"
end

@socket = BufferedIO.new(s, read_timeout: @read_timeout,
write_timeout: @write_timeout,
continue_timeout: @continue_timeout,
Expand All @@ -1822,13 +1830,22 @@ def connect
private_constant :TCP_SOCKET_NEW_HAS_OPEN_TIMEOUT

def timeouted_connect(conn_addr, conn_port)
if TCP_SOCKET_NEW_HAS_OPEN_TIMEOUT
s = if TCP_SOCKET_NEW_HAS_OPEN_TIMEOUT
TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout)
else
Timeout.timeout(@open_timeout, Net::OpenTimeout) {
TCPSocket.open(conn_addr, conn_port, @local_host, @local_port)
}
end
s.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
s
rescue => e
s&.close
if (defined?(IO::TimeoutError) && e.is_a?(IO::TimeoutError)) || e.is_a?(Errno::ETIMEDOUT) # for compatibility with previous versions
e = Net::OpenTimeout.new(e)
end
raise e, "Failed to open TCP connection to " +
"#{conn_addr}:#{conn_port} (#{e.message})"
end
private :timeouted_connect

Expand Down Expand Up @@ -1856,14 +1873,13 @@ def do_finish
@proxy_port = nil
@proxy_user = nil
@proxy_pass = nil
@proxy_use_ssl = nil

# Creates an \HTTP proxy class which behaves like \Net::HTTP, but
# performs all access via the specified proxy.
#
# This class is obsolete. You may pass these same parameters directly to
# \Net::HTTP.new. See Net::HTTP.new for details of the arguments.
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ssl = nil) #:nodoc:
def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil) #:nodoc:
return self unless p_addr

Class.new(self) {
Expand All @@ -1881,7 +1897,6 @@ def HTTP.Proxy(p_addr = :ENV, p_port = nil, p_user = nil, p_pass = nil, p_use_ss

@proxy_user = p_user
@proxy_pass = p_pass
@proxy_use_ssl = p_use_ssl
}
end

Expand All @@ -1908,9 +1923,6 @@ def proxy_class?
# Returns the password for accessing the proxy, or +nil+ if none;
# see Net::HTTP@Proxy+Server.
attr_reader :proxy_pass

# Use SSL when talking to the proxy. If Net::HTTP does not use a proxy, nil.
attr_reader :proxy_use_ssl
end

# Returns +true+ if a proxy server is defined, +false+ otherwise;
Expand Down Expand Up @@ -1977,6 +1989,26 @@ def proxy_pass
end
end

# Returns whether TLS is used for the connection to the proxy server;
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
def proxy_use_ssl?
if @proxy_from_env
proxy_uri&.scheme == "https"
else
@proxy_use_ssl
end
end

# Returns the OpenSSL::SSL::SSLContext object used for SSL/TLS sessions
# with the proxy server.
def proxy_ssl_context # :nodoc:
@proxy_ssl_context ||= (
ctx = OpenSSL::SSL::SSLContext.new
ctx.set_params
ctx
)
end

alias proxyaddr proxy_address #:nodoc: obsolete
alias proxyport proxy_port #:nodoc: obsolete

Expand Down
11 changes: 7 additions & 4 deletions test/net/fixtures/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ regen_certs:
cacert.pem: server.key
openssl req -new -x509 -days 3650 -key server.key -out cacert.pem -subj "/C=JP/ST=Shimane/L=Matz-e city/O=Ruby Core Team/CN=Ruby Test CA/emailAddress=security@ruby-lang.org"

server.csr:
openssl req -new -key server.key -out server.csr -subj "/C=JP/ST=Shimane/O=Ruby Core Team/OU=Ruby Test/CN=localhost"
server.crt: cacert.pem
openssl req -new -key server.key -out server.csr -subj "/C=JP/ST=Shimane/O=Ruby Core Team/OU=Ruby Test/CN=Server 1" -addext "subjectAltName = DNS:localhost"
openssl x509 -days 3650 -CA cacert.pem -CAkey server.key -set_serial 00 -in server.csr -req -copy_extensions copy -out server.crt
rm server.csr

server.crt: server.csr cacert.pem
openssl x509 -days 3650 -CA cacert.pem -CAkey server.key -set_serial 00 -in server.csr -req -out server.crt
server_ip_san.crt: cacert.pem
openssl req -new -key server.key -out server.csr -subj "/C=JP/ST=Shimane/O=Ruby Core Team/OU=Ruby Test/CN=Server 2" -addext "subjectAltName = IP:127.0.0.1,IP:::1"
openssl x509 -days 3650 -CA cacert.pem -CAkey server.key -set_serial 01 -in server.csr -req -copy_extensions copy -out server_ip_san.crt
rm server.csr
39 changes: 20 additions & 19 deletions test/net/fixtures/server.crt
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDYTCCAkkCAQAwDQYJKoZIhvcNAQELBQAwgYwxCzAJBgNVBAYTAkpQMRAwDgYD
VQQIDAdTaGltYW5lMRQwEgYDVQQHDAtNYXR6LWUgY2l0eTEXMBUGA1UECgwOUnVi
eSBDb3JlIFRlYW0xFTATBgNVBAMMDFJ1YnkgVGVzdCBDQTElMCMGCSqGSIb3DQEJ
ARYWc2VjdXJpdHlAcnVieS1sYW5nLm9yZzAeFw0yNDAxMDExMTQ3MjNaFw0zMzEy
MjkxMTQ3MjNaMGAxCzAJBgNVBAYTAkpQMRAwDgYDVQQIDAdTaGltYW5lMRcwFQYD
VQQKDA5SdWJ5IENvcmUgVGVhbTESMBAGA1UECwwJUnVieSBUZXN0MRIwEAYDVQQD
DAlsb2NhbGhvc3QwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCw+egZ
Q6eumJKq3hfKfED4dE/tL4FI5sjqont9ABVI+1GSqyi1bFBgsRjM0THllIdMbKmJ
tWwnKW8J+5OgNN8y6Xxv8JmM/Y5vQt2lis0fqXmG8UTz0VTWdlAXXmhUs6lSADvA
aIe4RVrCsZ97L3ZQTryY7JRVcbB4khUN3Gp0yg+801SXzoFTTa+UGIRLE66jH51a
a5VXu99hnv1OiH8tQrjdi8mH6uG/icq4XuIeNWMF32wHqIOOPvQcWV3M5D2vxJEj
702Ku6k9OQXkAo17qRSEonWW4HtLbtmS8He1JNPc/n3dVUm+fM6NoDXPoLP7j55G
9zKyqGtGAWXAj1MTAgMBAAEwDQYJKoZIhvcNAQELBQADggEBACtGNdj5TEtnJBYp
M+LhBeU3oNteldfycEm993gJp6ghWZFg23oX8fVmyEeJr/3Ca9bAgDqg0t9a0npN
oWKEY6wVKqcHgu3gSvThF5c9KhGbeDDmlTSVVNQmXWX0K2d4lS2cwZHH8mCm2mrY
PDqlEkSc7k4qSiqig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A0GCSqGSIb3DQEBCwUAA4IB
AQCUav+Av881U4D5HXYaU4JH3AYLP3pmaCAnaASbkCV5WjjIo8A4jLC5FrgibaQc
4vw4JdHaO8JK05qb61rtzQLPyk8fIrV8LAcisBMBRnawgo98EB6OVXjpgKSRm4YU
7SlYpnO+D9jMc1ECl8K1ILQyuT4+zgEFREZLNGjKuaJXDExD8JPhT7DumJ5XMGoI
2r2YWCfnsef1RbGtze30nAJqcf6XrHyn95VU70AV45yXuE/FOZDb8dX90n1K3Frm
ebqT0Q8smwLB+R9+BhWjhlW3oj8hLtsLBPz5U7Fp4rg2nMoOSvrrqgkRyhoxzJie
R+WNFodMHu0PxkmSGD37G76S
-----END CERTIFICATE-----
22 changes: 22 additions & 0 deletions test/net/fixtures/server_ip_san.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-----BEGIN CERTIFICATE-----
MIIDqzCCApOgAwIBAgIBATANBgkqhkiG9w0BAQsFADCBjDELMAkGA1UEBhMCSlAx
EDAOBgNVBAgMB1NoaW1hbmUxFDASBgNVBAcMC01hdHotZSBjaXR5MRcwFQYDVQQK
DA5SdWJ5IENvcmUgVGVhbTEVMBMGA1UEAwwMUnVieSBUZXN0IENBMSUwIwYJKoZI
hvcNAQkBFhZzZWN1cml0eUBydWJ5LWxhbmcub3JnMB4XDTI2MDYyMTEzMTQ0M1oX
DTM2MDYxODEzMTQ0M1owXzELMAkGA1UEBhMCSlAxEDAOBgNVBAgMB1NoaW1hbmUx
FzAVBgNVBAoMDlJ1YnkgQ29yZSBUZWFtMRIwEAYDVQQLDAlSdWJ5IFRlc3QxETAP
BgNVBAMMCFNlcnZlciAyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
sPnoGUOnrpiSqt4XynxA+HRP7S+BSObI6qJ7fQAVSPtRkqsotWxQYLEYzNEx5ZSH
TGypibVsJylvCfuToDTfMul8b/CZjP2Ob0LdpYrNH6l5hvFE89FU1nZQF15oVLOp
UgA7wGiHuEVawrGfey92UE68mOyUVXGweJIVDdxqdMoPvNNUl86BU02vlBiESxOu
ox+dWmuVV7vfYZ79Toh/LUK43YvJh+rhv4nKuF7iHjVjBd9sB6iDjj70HFldzOQ9
r8SRI+9NirupPTkF5AKNe6kUhKJ1luB7S27ZkvB3tSTT3P593VVJvnzOjaA1z6Cz
+4+eRvcysqhrRgFlwI9TEwIDAQABo0QwQjAhBgNVHREEGjAYhwR/AAABhxAAAAAA
AAAAAAAAAAAAAAABMB0GA1UdDgQWBBSJGVleDvFp9cu9R+E0/OKYzGkwkTANBgkq
hkiG9w0BAQsFAAOCAQEAi8zbc22L5+8UGjXftk2Dfg814rsholLK4h8eXHptRh1k
cqRJ0DrXn1SbiavBAkRUBeh07CtSOaxq/LP9ZiJHAcZ99+ZDVG0BRmg4esh9LEiV
FuM7piCfdsDRBfzk25Ko8lQEq1l3aIUSzMpaQV8OvZC51JM8EBGxj3r6n5iZf0b9
3yKVZme1l2mnMSvCeJ9VY1FCnIEwF1WTzBgOQLQOu4zRlR1O0xtn4+KIL79F7XSE
HB0Sni8krQA7tDprMQsKLoLmvPpFrkTHyR3D7/BTvsE3N0k6cPV9WJTg4wOyT4JC
7Gm1sdFNKo2MhdE/YXKqb5+rv2ILfezwtdcVlB23wQ==
-----END CERTIFICATE-----
20 changes: 20 additions & 0 deletions test/net/http/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,18 @@ def test_proxy_address
TestNetHTTPUtils.clean_http_proxy_env do
http = Net::HTTP.new 'hostname.example', nil, 'proxy.example'
assert_equal 'proxy.example', http.proxy_address
assert_equal 80, http.proxy_port

http = Net::HTTP.new 'hostname.example', nil
assert_equal nil, http.proxy_address

http = Net::HTTP.new 'hostname.example', nil,
'proxy.example', 8000, 'user', 'pass', 'invalid', true
assert_equal 'proxy.example', http.proxy_address
assert_equal 8000, http.proxy_port
assert_equal 'user', http.proxy_user
assert_equal 'pass', http.proxy_pass
assert_equal true, http.proxy_use_ssl?
end
end

Expand Down Expand Up @@ -195,6 +204,17 @@ def test_proxy_eh_ENV_with_urlencoded_user
end
end

def test_proxy_eh_ENV_https_proxy
TestNetHTTPUtils.clean_http_proxy_env do
ENV['http_proxy'] = 'https://proxy.example:8000'

http = Net::HTTP.new 'hostname.example'

assert_equal true, http.proxy?
assert_equal true, http.proxy_use_ssl?
end
end

def test_proxy_eh_ENV_none_set
TestNetHTTPUtils.clean_http_proxy_env do
assert_equal false, Net::HTTP.new('hostname.example').proxy?
Expand Down
Loading
Loading