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 Dec 22, 2022
1 parent 217097b commit b4e3004
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 @@ -419,6 +419,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, and returns an array of
# capabilities that the server supports. Each capability
# is a string. See [IMAP] for a list of possible
Expand Down Expand Up @@ -1303,6 +1308,7 @@ def initialize(host, port_or_options = {},
@tagno = 0
@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 @@ -1655,6 +1661,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 b4e3004

Please sign in to comment.