From 153a0e9e73ee0fcd68d0faa1e32df9c690c75da2 Mon Sep 17 00:00:00 2001 From: abu-tahir-0 Date: Tue, 24 Nov 2026 08:09:27 +0530 Subject: [PATCH] fix: verify HTTPS proxy certificates before CONNECT Use a dedicated proxy SSL context with peer and hostname verification so proxy credentials and application bytes are never sent over an unauthenticated TLS connection. Add an optional proxy certificate store and cover trusted, untrusted, and wrong-host proxies. --- lib/net/http.rb | 24 ++++++++++- sig/net-http.rbs | 5 +++ test/net/http/test_https_proxy.rb | 67 +++++++++++++++++++++++++++---- 3 files changed, 87 insertions(+), 9 deletions(-) diff --git a/lib/net/http.rb b/lib/net/http.rb index 150e2277..2ac24354 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -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 @@ -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, @@ -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 diff --git a/sig/net-http.rbs b/sig/net-http.rbs index cc19054d..06882c43 100644 --- a/sig/net-http.rbs +++ b/sig/net-http.rbs @@ -1145,6 +1145,11 @@ module Net # attr_accessor proxy_pass: String? + # + # Sets or returns the X509::Store used to verify the proxy certificate. + # + attr_accessor proxy_cert_store: untyped + #