Skip to content

Commit ceb8c14

Browse files
committed
🚧 update default_ssl_and_port ... WIP
1 parent 5567a93 commit ceb8c14

File tree

2 files changed

+84
-17
lines changed

2 files changed

+84
-17
lines changed

lib/net/imap.rb

+64-15
Original file line numberDiff line numberDiff line change
@@ -3333,26 +3333,75 @@ def remove_response_handler(handler)
33333333
SSL_PORT = 993 # :nodoc:
33343334

33353335
def default_ssl_and_port(tls, port)
3336-
if tls.nil? && port
3337-
tls = true if port == SSL_PORT || /\Aimaps\z/i === port
3338-
tls = false if port == PORT
3339-
elsif port.nil? && !tls.nil?
3340-
port = tls ? SSL_PORT : PORT
3341-
end
3342-
if tls.nil? && port.nil?
3343-
tls = config.default_tls.dup.freeze
3344-
port = tls ? SSL_PORT : PORT
3345-
if tls.nil?
3346-
warn "A future version of Net::IMAP::Config#default_tls " \
3347-
"will default to 'true', for secure connections by default. " \
3348-
"Use 'Net::IMAP.new(host, ssl: false)' or " \
3349-
"Net::IMAP.config.default_tls = false' to silence this warning."
3350-
end
3336+
case [tls && true, classify_port(port)]
3337+
in true, nil then return tls, SSL_PORT
3338+
in false, nil then return tls, PORT
3339+
in nil, :tls then return true, port
3340+
in nil, :plain then return false, port
3341+
in nil, nil then return use_default_ssl
3342+
in true, :tls | :other then return tls, port
3343+
in false, :plain | :other then return tls, port
3344+
in true, :plain then return warn_mismatched_port tls, port
3345+
in false, :tls then return warn_mismatched_port tls, port
3346+
in nil, :other then return warn_nonstandard_port port
33513347
end
3348+
# TODO: move this wherever is appropriate
33523349
tls &&= tls.respond_to?(:to_hash) ? tls.to_hash : {}
3350+
end
3351+
3352+
# classify_port(port) -> :tls | :plain | :other | nil
3353+
def classify_port(port)
3354+
case port
3355+
in (SSL_PORT | /\Aimaps\z/i) then :tls
3356+
in (PORT | /\Aimap\z/i) then :plain
3357+
in (Integer | String) then :other
3358+
in nil then nil
3359+
end
3360+
end
3361+
3362+
def warn_mismatched_port(tls, port)
3363+
if tls
3364+
warn "Using TLS on plaintext IMAP port"
3365+
else
3366+
warn "Using plaintext on TLS IMAP port"
3367+
end
3368+
[tls, port]
3369+
end
3370+
3371+
def warn_nonstandard_port(port)
3372+
tls = !!config.default_ssl
3373+
if config.warn_nonstandard_port_without_ssl
3374+
warn "Using #{tls ? "TLS" : "plaintext"} on port #{port}. " \
3375+
"Set ssl explicitly for non-standard IMAP ports."
3376+
end
3377+
# TODO: print default_ssl warning
33533378
[tls, port]
33543379
end
33553380

3381+
TLS_DEFAULT_WARNING =
3382+
"Net::IMAP.config.default_ssl will default to true in the future. " \
3383+
"To silence this warning, " \
3384+
"set Net::IMAP.config.default_ssl = (true | false)' or " \
3385+
"use 'Net::IMAP.new(host, ssl: (true | false))'."
3386+
private_constant :TLS_DEFAULT_WARNING
3387+
3388+
def use_default_ssl
3389+
case config.default_ssl
3390+
when true then [true, SSL_PORT]
3391+
when false then [false, PORT]
3392+
when :warn
3393+
warn TLS_DEFAULT_WARNING unless port
3394+
port ||= SSL_PORT
3395+
warn "Using TLS on port #{port}."
3396+
[true, port]
3397+
when nil
3398+
warn TLS_DEFAULT_WARNING unless port
3399+
port ||= PORT
3400+
warn "Using plain-text on port #{port}."
3401+
[false, port]
3402+
end
3403+
end
3404+
33563405
def start_imap_connection
33573406
@greeting = get_server_greeting
33583407
@capabilities = capabilities_from_resp_code @greeting

lib/net/imap/config.rb

+20-2
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,22 @@ def self.[](config)
257257
# with no params.
258258
attr_accessor :default_ssl, type: Enum[false, nil, :warn, true]
259259

260+
# Whether to warn for using default_ssl when the port is non-standard.
261+
#
262+
# Although default_ssl is used for non-standard ports, this warning is
263+
# different replaces the warning when default_ssl is +nil+ or +:warn+.
264+
# When this option is false but default_ssl is +nil+ or +:warn+, that
265+
# warning will be printed instead.
266+
#
267+
# ==== Valid options
268+
#
269+
# [+false+ <em>(original behavior)</em>]
270+
# Don't print a special warning for nonstandard ports without explicit
271+
# +ssl+.
272+
# [+true+ <em>(eventual future default)</em>]
273+
# Print a special warning for nonstandard ports without explicit +ssl+.
274+
attr_accessor :warn_nonstandard_port_without_ssl, type: :boolean
275+
260276
# Whether to use the +SASL-IR+ extension when the server and \SASL
261277
# mechanism both support it. Can be overridden by the +sasl_ir+ keyword
262278
# parameter to Net::IMAP#authenticate.
@@ -508,6 +524,7 @@ def defaults_hash
508524
open_timeout: 30,
509525
idle_response_timeout: 5,
510526
default_ssl: false,
527+
warn_nonstandard_port_without_ssl: false,
511528
sasl_ir: true,
512529
enforce_logindisabled: true,
513530
max_response_size: 512 << 20, # 512 MiB
@@ -547,14 +564,15 @@ def defaults_hash
547564
).freeze
548565

549566
version_defaults[0.6r] = Config[0.5r].dup.update(
567+
default_ssl: :warn,
550568
responses_without_block: :frozen_dup,
551-
default_ssl: nil,
552569
parser_use_deprecated_uidplus_data: false,
553570
parser_max_deprecated_uidplus_data_size: 0,
554571
).freeze
555572

556573
version_defaults[0.7r] = Config[0.6r].dup.update(
557-
default_ssl: :warn,
574+
default_ssl: true,
575+
warn_nonstandard_port_without_ssl: true,
558576
).freeze
559577

560578
# Safe conversions one way only:

0 commit comments

Comments
 (0)