Skip to content

Commit

Permalink
🔒 Add Net::IMAP#tls_verified?
Browse files Browse the repository at this point in the history
Returns true after the TLS negotiation has completed and the remote
hostname has been verified.

This can be useful, e.g. a project may require automated safeguards
against selecting particular SASL mechanisms—or against authenticating
at all—when TLS hasn't been established and the peer verified.
  • Loading branch information
nevans committed Jun 12, 2023
1 parent ed4786b commit db71adb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/net/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,11 @@ def disconnected?
return @sock.closed?
end

# Returns true after the TLS negotiation has completed and the remote
# hostname has been verified. This will still be false if TLS was
# established but peer verification was disabled.
def tls_verified?; @tls_verified end

# Sends a {CAPABILITY command [IMAP4rev1 §6.1.1]}[https://www.rfc-editor.org/rfc/rfc3501#section-6.1.1]
# and returns an array of capabilities that the server supports. Each
# capability is a string.
Expand Down Expand Up @@ -2222,6 +2227,7 @@ def initialize(host, port_or_options = {},
@utf8_strings = false
@open_timeout = options[:open_timeout] || 30
@idle_response_timeout = options[:idle_response_timeout] || 5
@tls_verified = false
@parser = ResponseParser.new
@sock = tcp_socket(@host, @port)
begin
Expand Down Expand Up @@ -2581,6 +2587,7 @@ def start_tls_session(params = {})
ssl_socket_connect(@sock, @open_timeout)
if context.verify_mode != VERIFY_NONE
@sock.post_connection_check(@host)
@tls_verified = true
end
end

Expand Down

0 comments on commit db71adb

Please sign in to comment.