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
24 changes: 23 additions & 1 deletion lib/net/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ def initialize(address, port = nil) # :nodoc:
@proxy_user = nil
@proxy_pass = nil
@proxy_use_ssl = nil
@proxy_cert_store = nil

@use_ssl = false
@ssl_context = nil
Expand Down Expand Up @@ -1330,6 +1331,9 @@ def response_body_encoding=(value)
# see {Proxy Server}[rdoc-ref:Net::HTTP@Proxy+Server].
attr_writer :proxy_use_ssl

# Sets or returns the X509::Store used to verify the proxy certificate.
attr_accessor :proxy_cert_store

# Returns the IP address for the connection.
#
# If the session has not been started,
Expand Down Expand Up @@ -1713,8 +1717,26 @@ def connect
if use_ssl?
if proxy?
if @proxy_use_ssl
proxy_sock = OpenSSL::SSL::SSLSocket.new(s)
# Authenticate the proxy before sending CONNECT credentials.
proxy_ssl_context = OpenSSL::SSL::SSLContext.new
proxy_ssl_context.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER,
verify_hostname: true)
proxy_ssl_context.cert_store = @proxy_cert_store if @proxy_cert_store
proxy_ssl_host_address = nil
case proxy_address
when IPV4_ADDRESS, IPV6_ADDRESS
# IP addresses are not valid in SNI.
proxy_ssl_context.verify_hostname = false
else
proxy_ssl_host_address = proxy_address
end
proxy_sock = OpenSSL::SSL::SSLSocket.new(s, proxy_ssl_context)
proxy_sock.sync_close = true
if proxy_ssl_host_address && proxy_sock.respond_to?(:hostname=)
proxy_sock.hostname = proxy_ssl_host_address
end
ssl_socket_connect(proxy_sock, @open_timeout)
proxy_sock.post_connection_check(proxy_address)
else
proxy_sock = s
end
Expand Down
5 changes: 5 additions & 0 deletions sig/net-http.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,11 @@ module Net
#
attr_accessor proxy_pass: String?

# <!-- rdoc-file=lib/net/http.rb -->
# Sets or returns the X509::Store used to verify the proxy certificate.
#
attr_accessor proxy_cert_store: untyped

# <!--
# rdoc-file=lib/net/http.rb
# - ipaddr()
Expand Down
67 changes: 59 additions & 8 deletions test/net/http/test_https_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,13 @@ def read_fixture(key)
end

def test_https_proxy_ssl_connection
TCPServer.open("127.0.0.1", 0) {|tcpserver|
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = OpenSSL::PKey.read(read_fixture("server.key"))
ctx.cert = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
serv = OpenSSL::SSL::SSLServer.new(tcpserver, ctx)

_, port, _, _ = serv.addr
with_ssl_proxy do |port, serv|
client_thread = Thread.new {
proxy = Net::HTTP.Proxy("127.0.0.1", port, 'user', 'password', true)
proxy = Net::HTTP.Proxy("localhost", port, 'user', 'password', true)
http = proxy.new("foo.example.org", 8000)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.proxy_cert_store = proxy_cert_store
begin
http.start
rescue EOFError
Expand All @@ -97,6 +92,62 @@ def test_https_proxy_ssl_connection
end
}
assert_join_threads([client_thread, server_thread])
end
end

def test_https_proxy_rejects_untrusted_certificate
assert_proxy_certificate_rejected("localhost", trusted: false)
end

def test_https_proxy_rejects_wrong_host_certificate
assert_proxy_certificate_rejected("127.0.0.1", trusted: true)
end

def proxy_cert_store
OpenSSL::X509::Store.new.tap {|store|
store.add_cert(OpenSSL::X509::Certificate.new(read_fixture("cacert.pem")))
}
end

def with_ssl_proxy
TCPServer.open("127.0.0.1", 0) {|tcpserver|
ctx = OpenSSL::SSL::SSLContext.new
ctx.key = OpenSSL::PKey.read(read_fixture("server.key"))
ctx.cert = OpenSSL::X509::Certificate.new(read_fixture("server.crt"))
serv = OpenSSL::SSL::SSLServer.new(tcpserver, ctx)
yield serv.addr[1], serv
}
end

def assert_proxy_certificate_rejected(proxy_address, trusted:)
with_ssl_proxy do |port, serv|
proxy_request = nil
client_error = nil
client_thread = Thread.new {
proxy = Net::HTTP.Proxy(proxy_address, port, 'user', 'password', true)
http = proxy.new("foo.example.org", 8000)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.proxy_cert_store = proxy_cert_store if trusted
begin
http.start
rescue => error
client_error = error
end
}
server_thread = Thread.new {
begin
sock = serv.accept
proxy_request = sock.gets("\r\n\r\n")
rescue EOFError, Errno::ECONNRESET, Errno::ECONNABORTED, OpenSSL::SSL::SSLError
proxy_request = nil
ensure
sock&.close
end
}
assert_join_threads([client_thread, server_thread])
assert_nil(proxy_request, "proxy received bytes before certificate rejection: #{proxy_request.inspect}")
assert_kind_of(OpenSSL::SSL::SSLError, client_error)
end
end
end