Skip to content

Commit 705aa59

Browse files
committed
🍒 pick 62eea6f: 🔒🥅 Ensure STARTTLS tagged response was handled [backport #664]
Taking a "belt-and-suspenders" approach to a STARTTLS stripping attack: This handles `STARTTLS` as a special-case: if the `STARTTLS` handler did not run, for _whatever_ reason, an exception _must_ be raised and the connection dropped. _No_ command should ever receive a tagged `OK` prior to completely sending the command. But `STARTTLS` is security-sensitive enough to warrant this special-case handler.
1 parent c9a6f28 commit 705aa59

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

lib/net/imap.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1312,9 +1312,11 @@ def logout!
13121312
#
13131313
def starttls(**options)
13141314
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(options)
1315+
handled = false
13151316
error = nil
13161317
ok = send_command("STARTTLS") do |resp|
13171318
if resp.kind_of?(TaggedResponse) && resp.name == "OK"
1319+
handled = true
13181320
clear_cached_capabilities
13191321
clear_responses
13201322
start_tls_session
@@ -1326,6 +1328,13 @@ def starttls(**options)
13261328
disconnect
13271329
raise error
13281330
end
1331+
unless handled
1332+
disconnect
1333+
raise InvalidResponseError,
1334+
"STARTTLS handler was bypassed, although server responded %p" % [
1335+
ok.raw_data.chomp
1336+
]
1337+
end
13291338
ok
13301339
end
13311340

0 commit comments

Comments
 (0)