Skip to content

Commit 257ede0

Browse files
nevansrhenium
andcommitted
🥅 Re-raise #starttls error from receiver thread
Fixes #394. When `start_tls_session` raises an exception, that's caught in the receiver thread, but not re-raised. Fortunately, `@sock` will now be a permanently broken SSLSocket, so I don't think this can lead to accidentally using an insecure connection. Even so, `#starttls` should disconnect the socket and re-raise the error immediately. Failing test case was provided by @rhenium in #394. Co-authored-by: Kazuki Yamaguchi <[email protected]>
1 parent 436bd1c commit 257ede0

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/net/imap.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,13 +1239,21 @@ def logout!
12391239
#
12401240
def starttls(**options)
12411241
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(options)
1242-
send_command("STARTTLS") do |resp|
1242+
error = nil
1243+
ok = send_command("STARTTLS") do |resp|
12431244
if resp.kind_of?(TaggedResponse) && resp.name == "OK"
12441245
clear_cached_capabilities
12451246
clear_responses
12461247
start_tls_session
12471248
end
1249+
rescue Exception => error
1250+
raise # note that the error backtrace is in the receiver_thread
12481251
end
1252+
if error
1253+
disconnect
1254+
raise error
1255+
end
1256+
ok
12491257
end
12501258

12511259
# :call-seq:

test/net/imap/test_imap.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,16 @@ def test_starttls_unknown_ca
113113
omit "This test is not working with Windows" if RUBY_PLATFORM =~ /mswin|mingw/
114114

115115
imap = nil
116-
assert_raise(OpenSSL::SSL::SSLError) do
117-
ex = nil
118-
starttls_test do |port|
119-
imap = Net::IMAP.new("localhost", port: port)
116+
ex = nil
117+
starttls_test do |port|
118+
imap = Net::IMAP.new("localhost", port: port)
119+
begin
120120
imap.starttls
121-
imap
122121
rescue => ex
123-
imap
124122
end
125-
raise ex if ex
123+
imap
126124
end
125+
assert_kind_of(OpenSSL::SSL::SSLError, ex)
127126
assert_equal false, imap.tls_verified?
128127
assert_equal({}, imap.ssl_ctx_params)
129128
assert_equal(nil, imap.ssl_ctx.ca_file)

0 commit comments

Comments
 (0)