Skip to content

Commit 7a233c5

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 a530fa7 commit 7a233c5

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
@@ -1412,9 +1412,11 @@ def logout!
14121412
#
14131413
def starttls(**options)
14141414
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(options)
1415+
handled = false
14151416
error = nil
14161417
ok = send_command("STARTTLS") do |resp|
14171418
if resp.kind_of?(TaggedResponse) && resp.name == "OK"
1419+
handled = true
14181420
clear_cached_capabilities
14191421
clear_responses
14201422
start_tls_session
@@ -1426,6 +1428,13 @@ def starttls(**options)
14261428
disconnect
14271429
raise error
14281430
end
1431+
unless handled
1432+
disconnect
1433+
raise InvalidResponseError,
1434+
"STARTTLS handler was bypassed, although server responded %p" % [
1435+
ok.raw_data.chomp
1436+
]
1437+
end
14291438
ok
14301439
end
14311440

0 commit comments

Comments
 (0)