From fdcbc5419180866f24aa220a0478038cfa114436 Mon Sep 17 00:00:00 2001 From: kares Date: Tue, 27 Oct 2026 15:49:25 +0100 Subject: [PATCH 1/2] [compat] raise Errno::ECONNRESET on reset during handshake connect/accept wrapped every IOException as SSLError, while MRI raises the errno (e.g. Errno::ECONNRESET) for socket failures during SSL_connect/SSL_accept non-SSL IOExceptions now go through newIOErrorFromException, as sysread/syswrite already do --- .../java/org/jruby/ext/openssl/SSLSocket.java | 13 +++++- test/ssl/test_ssl.rb | 42 +++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/jruby/ext/openssl/SSLSocket.java b/src/main/java/org/jruby/ext/openssl/SSLSocket.java index 96a71329..3bab8310 100644 --- a/src/main/java/org/jruby/ext/openssl/SSLSocket.java +++ b/src/main/java/org/jruby/ext/openssl/SSLSocket.java @@ -358,12 +358,17 @@ private IRubyObject connectImpl(final ThreadContext context, final boolean block final RaiseException verifyError = tryExtractVerifyError(context.runtime, e); throw verifyError != null ? verifyError : newSSLErrorFromHandshake(context.runtime, e); } - catch (IOException e) { + catch (SSLException e) { LOG.debug(context.runtime, "connectImpl", e); forceClose(); final RaiseException verifyError = tryExtractVerifyError(context.runtime, e); throw verifyError != null ? verifyError : newSSLError(context.runtime, e); } + catch (IOException e) { // e.g. connection reset (Errno::ECONNRESET as in MRI) + LOG.debug(context.runtime, "connectImpl", e); + forceClose(); + throw newError(context.runtime::newIOErrorFromException, e); + } catch (NotYetConnectedException e) { throw newErrnoEPIPEError(context.runtime, "SSL_connect"); } @@ -461,11 +466,15 @@ public boolean matches(SNIServerName serverName) { final RaiseException verifyError = tryExtractVerifyError(context.runtime, e); throw verifyError != null ? verifyError : newSSLErrorFromHandshake(context.runtime, e); } - catch (IOException e) { + catch (SSLException e) { LOG.debugStack(context.runtime, null, e); final RaiseException verifyError = tryExtractVerifyError(context.runtime, e); throw verifyError != null ? verifyError : newSSLError(context.runtime, e); } + catch (IOException e) { // e.g. connection reset (Errno::ECONNRESET as in MRI) + LOG.debugStack(context.runtime, null, e); + throw newError(context.runtime::newIOErrorFromException, e); + } catch (RaiseException e) { throw e; } diff --git a/test/ssl/test_ssl.rb b/test/ssl/test_ssl.rb index 9887b5a0..881c45b4 100644 --- a/test/ssl/test_ssl.rb +++ b/test/ssl/test_ssl.rb @@ -546,6 +546,48 @@ def test_unexpected_eof_is_not_accepted_by_default tcp_server&.close rescue nil end + def test_connect_reset_by_peer_raises_econnreset + tcp_server = TCPServer.new("127.0.0.1", 0) + server_thread = Thread.new do + sock = tcp_server.accept + sock.readpartial(5) # wait for ClientHello + sock.setsockopt(Socket::Option.linger(true, 0)) # close with RST + sock.close + end + + sock = TCPSocket.new("127.0.0.1", tcp_server.addr[1]) + ssl = OpenSSL::SSL::SSLSocket.new(sock) + assert_raise(Errno::ECONNRESET) { ssl.connect } + ensure + sock&.close rescue nil + server_thread&.join rescue nil + tcp_server&.close rescue nil + end + + def test_accept_reset_by_peer_raises_econnreset + server_ctx = OpenSSL::SSL::SSLContext.new + server_ctx.cert = @svr_cert + server_ctx.key = @svr_key + tcp_server = TCPServer.new("127.0.0.1", 0) + ssl_server = OpenSSL::SSL::SSLServer.new(tcp_server, server_ctx) + ssl_server.start_immediately = false + + client_thread = Thread.new do + sock = TCPSocket.new("127.0.0.1", tcp_server.addr[1]) + sock.write("\x16\x03\x01\x00\xff") # handshake record header, body never sent + sleep 0.2 + sock.setsockopt(Socket::Option.linger(true, 0)) # close with RST + sock.close + end + + ssl = ssl_server.accept + assert_raise(Errno::ECONNRESET) { ssl.accept } + ensure + ssl&.io&.close rescue nil + client_thread&.join rescue nil + tcp_server&.close rescue nil + end + def test_unexpected_eof_can_be_ignored_with_option server_ctx = OpenSSL::SSL::SSLContext.new server_ctx.cert = @svr_cert From c11ea8d3f0329eb03948af7fd0f37603c7139865 Mon Sep 17 00:00:00 2001 From: kares Date: Tue, 27 Oct 2026 15:49:25 +0100 Subject: [PATCH 2/2] [test] omit badssl tests on handshake connection reset connection resets now raise Errno::ECONNRESET (as in MRI), only an EOF mid-handshake still needs the SSLError rescue --- integration/tls_verify_test.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/integration/tls_verify_test.rb b/integration/tls_verify_test.rb index a2b679b8..96cb2ae8 100644 --- a/integration/tls_verify_test.rb +++ b/integration/tls_verify_test.rb @@ -117,9 +117,10 @@ def test_max_version_is_honoured def test_min_version_above_server_maximum_fails with_service do configure = lambda { |ctx| ctx.min_version = OpenSSL::SSL::TLS1_3_VERSION } - assert_raise(OpenSSL::SSL::SSLError) do + err = assert_raise(OpenSSL::SSL::SSLError, Errno::ECONNRESET) do ssl_connect('tls-v1-2.badssl.com', 1012, configure: configure) { |ssl| ssl } end + omit "badssl.com dropped the connection (#{err.message})" if err.is_a?(Errno::ECONNRESET) end end @@ -168,8 +169,8 @@ def with_service Errno::ENETUNREACH, Errno::ETIMEDOUT, Timeout::Error => e omit "badssl.com not reachable (#{e.class}: #{e.message})" rescue OpenSSL::SSL::SSLError => e - # JRuby reports a peer dropping the connection mid-handshake as SSLError (MRI: Errno::ECONNRESET) - raise unless e.message.match?(/Connection reset|closing inbound before receiving peer's close_notify/i) + # peer closing mid-handshake is an SSLError (not Errno) in JRuby and MRI + raise unless e.message.match?(/closing inbound before receiving peer's close_notify|unexpected eof/i) omit "badssl.com dropped the connection (#{e.message})" end