From ca9a047675845e4b4ba4531b07ffa913c6be8697 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 17 Nov 2026 12:49:18 +0000 Subject: [PATCH] THRIFT-6117: Reject duplicate opens in Ruby SSL and UNIX sockets Client: rb Thrift::SSLSocket and Thrift::UNIXSocket override Socket#open without its already-open check, so a second open connected again and replaced the handle. The first connection stayed open but unreachable, and a later close closed only the replacement. Both now raise TransportException::ALREADY_OPEN before connecting, as Socket#open does, and keep the existing connection. In UNIXSocket the check sits outside the rescue that turns connect errors into NOT_OPEN. A shared example opens a real TCP, SSL and UNIX connection, opens it again, and checks that the second open is rejected, that the server sees no new connection, and that close ends the original one. Generated-By: amp/0.0.1794916837-g564d50 (model: anthropic/claude-opus-5-5; operator: kpumuk@apache.org) --- lib/rb/lib/thrift/transport/ssl_socket.rb | 2 + lib/rb/lib/thrift/transport/unix_socket.rb | 10 +++-- lib/rb/spec/socket_spec.rb | 35 ++++++----------- lib/rb/spec/socket_spec_shared.rb | 26 +++++++++++++ lib/rb/spec/ssl_socket_spec.rb | 44 +++++++++++++++++++--- lib/rb/spec/unix_socket_spec.rb | 19 ++++++++++ 6 files changed, 103 insertions(+), 33 deletions(-) diff --git a/lib/rb/lib/thrift/transport/ssl_socket.rb b/lib/rb/lib/thrift/transport/ssl_socket.rb index ffcf232c6b1..4d94dce09cf 100644 --- a/lib/rb/lib/thrift/transport/ssl_socket.rb +++ b/lib/rb/lib/thrift/transport/ssl_socket.rb @@ -33,6 +33,8 @@ def initialize(host = "localhost", port = 9090, timeout = nil, ssl_context = nil attr_accessor :server_hostname def open + raise TransportException.new(TransportException::ALREADY_OPEN, "Socket already open") if open? + deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout unless @timeout.nil? || @timeout == 0 socket = connect_socket(deadline) diff --git a/lib/rb/lib/thrift/transport/unix_socket.rb b/lib/rb/lib/thrift/transport/unix_socket.rb index 2cacd63de6b..cd4f7c5d69e 100644 --- a/lib/rb/lib/thrift/transport/unix_socket.rb +++ b/lib/rb/lib/thrift/transport/unix_socket.rb @@ -31,9 +31,13 @@ def initialize(path, timeout = nil) end def open - @handle = ::UNIXSocket.new(@path) - rescue StandardError - raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}") + raise TransportException.new(TransportException::ALREADY_OPEN, "Socket already open") if open? + + begin + @handle = ::UNIXSocket.new(@path) + rescue StandardError + raise TransportException.new(TransportException::NOT_OPEN, "Could not open UNIX socket at #{@path}") + end end def to_s diff --git a/lib/rb/spec/socket_spec.rb b/lib/rb/spec/socket_spec.rb index 0b428bbce88..0edf8348845 100644 --- a/lib/rb/spec/socket_spec.rb +++ b/lib/rb/spec/socket_spec.rb @@ -51,30 +51,6 @@ @socket.open end - it "should reject a second open without orphaning the live connection" do - allow(Addrinfo).to receive(:foreach).and_call_original - server = TCPServer.new("127.0.0.1", 0) - socket = Thrift::Socket.new("127.0.0.1", server.addr[1]) - peer = nil - - socket.open - peer = server.accept - first_handle = socket.handle - - expect { socket.open }.to raise_error(Thrift::TransportException, /already open/) do |error| - expect(error.type).to eq(Thrift::TransportException::ALREADY_OPEN) - end - expect(socket.handle).to equal(first_handle) - - socket.close - expect(peer.read).to eq("") - ensure - socket&.close - first_handle&.close unless first_handle&.closed? - peer&.close - server&.close - end - it "should accept host/port options" do handle = double("Handle", closed?: false) allow(handle).to receive(:close) @@ -215,4 +191,15 @@ peer&.close end end + + describe "with a live connection" do + let(:listener) { TCPServer.new("127.0.0.1", 0) } + let(:transport) { Thrift::Socket.new("127.0.0.1", listener.local_address.ip_port, 1) } + + def accept_peer + listener.accept + end + + it_should_behave_like "a socket rejecting duplicate opens" + end end diff --git a/lib/rb/spec/socket_spec_shared.rb b/lib/rb/spec/socket_spec_shared.rb index c28a863155e..de8fe056837 100644 --- a/lib/rb/spec/socket_spec_shared.rb +++ b/lib/rb/spec/socket_spec_shared.rb @@ -20,6 +20,32 @@ require "spec_helper" +# Expects the including group to define +listener+ (a listening ::TCPServer or +# ::UNIXServer), +transport+ (an unopened client transport connecting to it), +# and +accept_peer+ (the server side of the transport's first connection). +shared_examples_for "a socket rejecting duplicate opens" do + it "should reject a second open without connecting again or orphaning the live connection" do + transport.open + peer = accept_peer + first_handle = transport.handle + + expect { transport.open }.to raise_error(Thrift::TransportException, "Socket already open") do |error| + expect(error.type).to eq(Thrift::TransportException::ALREADY_OPEN) + end + expect(transport.handle).to equal(first_handle) + expect(IO.select([listener], nil, nil, 0)).to be_nil + + transport.close + expect(first_handle).to be_closed + expect(peer.read).to eq("") + ensure + transport.close + first_handle.close unless first_handle.nil? || first_handle.closed? + peer&.close + listener.close + end +end + shared_examples_for "a socket" do it "should open a socket" do expect(@socket.open).to eq(@handle) diff --git a/lib/rb/spec/ssl_socket_spec.rb b/lib/rb/spec/ssl_socket_spec.rb index 831d330df87..da503ac8a71 100644 --- a/lib/rb/spec/ssl_socket_spec.rb +++ b/lib/rb/spec/ssl_socket_spec.rb @@ -372,16 +372,48 @@ def build_client(context = nil) server_hostname: "localhost", ) end + end - def server_context - OpenSSL::SSL::SSLContext.new.tap do |context| - context.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, "server.crt"))) - context.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, "server.key"))) + describe "with a live connection" do + before(:each) do + server = listener + context = server_context + @server_thread = Thread.new do + peer = OpenSSL::SSL::SSLSocket.new(server.accept, context) + peer.sync_close = true + peer.accept end + @server_thread.report_on_exception = false + end + + after(:each) do + @server_thread.join(1) + @server_thread.kill if @server_thread.alive? + end + + let(:listener) { TCPServer.new("127.0.0.1", 0) } + let(:transport) do + context = OpenSSL::SSL::SSLContext.new + context.verify_mode = OpenSSL::SSL::VERIFY_PEER + context.ca_file = File.join(ssl_keys_dir, "server.crt") + Thrift::SSLSocket.new("127.0.0.1", listener.local_address.ip_port, 1, context, server_hostname: "localhost") + end + + def accept_peer + @server_thread.value end - def ssl_keys_dir - File.expand_path("../../../test/keys", __dir__) + it_should_behave_like "a socket rejecting duplicate opens" + end + + def server_context + OpenSSL::SSL::SSLContext.new.tap do |context| + context.cert = OpenSSL::X509::Certificate.new(File.read(File.join(ssl_keys_dir, "server.crt"))) + context.key = OpenSSL::PKey::RSA.new(File.read(File.join(ssl_keys_dir, "server.key"))) end end + + def ssl_keys_dir + File.expand_path("../../../test/keys", __dir__) + end end diff --git a/lib/rb/spec/unix_socket_spec.rb b/lib/rb/spec/unix_socket_spec.rb index a28abe113d8..44833621be8 100644 --- a/lib/rb/spec/unix_socket_spec.rb +++ b/lib/rb/spec/unix_socket_spec.rb @@ -52,6 +52,25 @@ end end + describe "with a live connection" do + before(:each) do + @dir = Dir.mktmpdir("thrift_unix_spec") + end + + after(:each) do + FileUtils.remove_entry(@dir) + end + + let(:listener) { ::UNIXServer.new(File.join(@dir, "server.sock")) } + let(:transport) { Thrift::UNIXSocket.new(listener.path, 1) } + + def accept_peer + listener.accept + end + + it_should_behave_like "a socket rejecting duplicate opens" + end + describe Thrift::UNIXServerSocket do before(:each) do @path = "/tmp/thrift_spec_socket"