Skip to content

Commit be14229

Browse files
committed
🗑️ Add deprecation warnings to .new and #starttls [🚧 WIP]
* `ssl` was renamed to `tls` in most places, with backwards compatible aliases. Using `ssl` does not print any deprecation warnings. Using both `tls` and `ssl` keywords raises an ArgumentError. * Preparing for a (backwards-incompatible) secure-by-default configuration, `Net::IMAP.default_tls` will determine the value for `tls` when no explicit port or tls setting is provided. Using port 143 will be insecure by default. Using port 993 will be secure by default. Providing no explicit port will use `Net::IMAP.default_tls` with the appropriate port. And providing any other unknown port will use `default_tls` with a warning. 🚧 TODO: should we use a different config var for default tls params when port is 993 and `tls` is unspecified? 🚧 TODO: should we use a different config var for choosing `tls` when `port` is non-standard vs choosing `port` and `tls` when neither are specified? 🚧 TODO: should we use a different var for `default_tls` be used to config params when port is 993 but tls is implicit? Another var?
1 parent b3f1dfe commit be14229

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

lib/net/imap.rb

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,20 @@ def max_response_size=(val) config.max_response_size = val end
968968
# Creates a new Net::IMAP object and connects it to the specified
969969
# +host+.
970970
#
971+
# ==== Default port and SSL
972+
#
973+
# When both both +port+ and +ssl+ are unspecified or +nil+,
974+
# +ssl+ is determined by {config.default_ssl}[rdoc-ref:Config#default_ssl]
975+
# and +port+ is based on that implicit value for +ssl+.
976+
#
977+
# When only one of the two is specified:
978+
# * When +ssl+ is truthy, +port+ defaults to +993+.
979+
# * When +ssl+ is +false+, +port+ defaults to +143+.
980+
# * When +port+ is +993+, +ssl+ defaults to +true+.
981+
# * When +port+ is +143+, +ssl+ defaults to +false+.
982+
# * When +port+ is nonstandard, the default for +ssl+ is determined
983+
# by {config.default_ssl}[rdoc-ref:Config#default_ssl].
984+
#
971985
# ==== Options
972986
#
973987
# Accepts the following options:
@@ -1081,7 +1095,7 @@ def initialize(host, port: nil, ssl: nil, response_handlers: nil,
10811095
# Config options
10821096
@host = host
10831097
@config = Config.new(config, **config_options)
1084-
@port = port || (ssl ? SSL_PORT : PORT)
1098+
ssl, @port = default_ssl_and_port(ssl, port)
10851099
@ssl_ctx_params, @ssl_ctx = build_ssl_ctx(ssl)
10861100

10871101
# Basic Client State
@@ -3343,6 +3357,27 @@ def remove_response_handler(handler)
33433357
PORT = 143 # :nodoc:
33443358
SSL_PORT = 993 # :nodoc:
33453359

3360+
def default_ssl_and_port(tls, port)
3361+
if tls.nil? && port
3362+
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
3363+
tls = false if port == PORT
3364+
elsif port.nil? && !tls.nil?
3365+
port = tls ? SSL_PORT : PORT
3366+
end
3367+
if tls.nil? && port.nil?
3368+
tls = config.default_tls.dup.freeze
3369+
port = tls ? SSL_PORT : PORT
3370+
if tls.nil?
3371+
warn "A future version of Net::IMAP::Config#default_tls " \
3372+
"will default to 'true', for secure connections by default. " \
3373+
"Use 'Net::IMAP.new(host, ssl: false)' or " \
3374+
"Net::IMAP.config.default_tls = false' to silence this warning."
3375+
end
3376+
end
3377+
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
3378+
[tls, port]
3379+
end
3380+
33463381
def start_imap_connection
33473382
@greeting = get_server_greeting
33483383
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,38 @@ def self.[](config)
222222
# The default value is +5+ seconds.
223223
attr_accessor :idle_response_timeout, type: Integer, default: 5
224224

225+
# The default value for the +ssl+ option of Net::IMAP.new, when +port+ is
226+
# unspecified or non-standard and +ssl+ is unspecified. default_ssl is
227+
# ignored when Net::IMAP.new is called with any explicit value for +ssl+.
228+
#
229+
# *Note*: A future release of Net::IMAP will set the default to +true+, as
230+
# per RFC7525[https://tools.ietf.org/html/rfc7525],
231+
# RFC7817[https://tools.ietf.org/html/rfc7817], and
232+
# RFC8314[https://tools.ietf.org/html/rfc8314].
233+
#
234+
# <em>(The default_ssl config attribute was added in +v0.5.?+.)</em>
235+
#
236+
# ==== Valid options
237+
#
238+
# [+false+ <em>(original behavior)</em>]
239+
# Plaintext by default, with no warnings.
240+
# [+nil+ <em>(planned default for +v0.6+)</em>]
241+
# Plaintext by default, but prints a warning.
242+
# [+:warn+ <em>(planned default for +v0.7+)</em>]
243+
# Use TLS by default, but print a warning.
244+
# [+true+ <em>(planned future default)</em>]
245+
# Use TLS by default, with the default SSL context params set by calling
246+
# {OpenSSL::SSL::SSLContext#set_params}[https://docs.ruby-lang.org/en/master/OpenSSL/SSL/SSLContext.html#method-i-set_params]
247+
# with no params.
248+
attr_accessor :default_ssl, type: Enum[
249+
false, nil, :warn, true
250+
], defaults: {
251+
0.0r => false,
252+
0.6r => nil,
253+
0.7r => :warn,
254+
1.0r => true,
255+
}
256+
225257
# Whether to use the +SASL-IR+ extension when the server and \SASL
226258
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
227259
# parameter to Net::IMAP#authenticate.

0 commit comments

Comments
 (0)