Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using insecure deprecated Net::IMAP.new args #1587

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/mail/network/retriever_methods/imap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,13 @@ def start(config=Mail::Configuration.instance, &block)
raise ArgumentError, ":enable_starttls and :enable_ssl are mutually exclusive. Set :enable_ssl if you're on an IMAPS connection. Set :enable_starttls if you're on an IMAP connection and using STARTTLS for secure TLS upgrade."
end

imap = Net::IMAP.new(settings[:address], settings[:port], settings[:enable_ssl], nil, false)
ssl = settings[:enable_ssl]
starttls = settings[:enable_starttls]
ssl &&= Hash.try_convert(ssl) || {}
starttls &&= Hash.try_convert(starttls) || {}

imap.starttls if settings[:enable_starttls]
imap = Net::IMAP.new(settings[:address], port: settings[:port], ssl: ssl)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a good opportunity to nilify the default port setting as net/imap has good defaults since forever.

imap.starttls(starttls) if starttls

if settings[:authentication].nil?
imap.login(settings[:user_name], settings[:password])
Expand Down
32 changes: 31 additions & 1 deletion spec/mail/network/retriever_methods/imap_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,27 @@
end
end

describe "Implicit SSL" do
before do
allow(Net::IMAP).to receive(:new).and_call_original
end

it "calls Net::IMAP.new new with ssl keyword arg" do
Mail.find
expect(Net::IMAP).to have_received(:new)
.with("localhost", port: 993, ssl: {})
end

it "passes enable_ssl params to ssl keyword" do
Mail.defaults do
retriever_method :imap, port: 993, enable_ssl: {ca_file: "etc.ca"}
end
Mail.find
expect(Net::IMAP).to have_received(:new)
.with("localhost", port: 993, ssl: {ca_file: "etc.ca"})
end
end

describe "STARTTLS" do
before do
@imap = MockIMAP.new
Expand All @@ -280,7 +301,16 @@
retriever_method :imap, :enable_starttls => true
end

expect(@imap).to receive(:starttls)
expect(@imap).to receive(:starttls).with({})
Mail.find
end

it "passes params to starttls" do
Mail.defaults do
retriever_method :imap, enable_starttls: {attr1: :val1, attr2: "v2"}
end

expect(@imap).to receive(:starttls).with({attr1: :val1, attr2: "v2"})
Mail.find
end

Expand Down