Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix starttls_auto and ssl_verify_mode settings

Markus Kahl5b3fef79094d
diff --git a/lib/open_project/configuration.rb b/lib/open_project/configuration.rb
index 6b603f040ac..21d638de923 100644
--- a/lib/open_project/configuration.rb
+++ b/lib/open_project/configuration.rb
@@ -173,6 +173,10 @@ module OpenProject
ActionMailer::Base.smtp_settings[:enable_starttls_auto] = Setting.smtp_enable_starttls_auto?
ActionMailer::Base.smtp_settings[:ssl] = Setting.smtp_ssl?
+
+ Setting.smtp_openssl_verify_mode.tap do |mode|
+ ActionMailer::Base.smtp_settings[:openssl_verify_mode] = mode unless mode.nil?
+ end
end
def cache_parameters
diff --git a/lib/open_project/patches/mail_smtp_start_tls_auto_fix.rb b/lib/open_project/patches/mail_smtp_start_tls_auto_fix.rb
new file mode 100644
index 00000000000..c5cffe5b3ff
--- /dev/null
+++ b/lib/open_project/patches/mail_smtp_start_tls_auto_fix.rb
@@ -0,0 +1,25 @@
+##
+# This is a fix for a new SMTP bug introduced with Ruby 3.
+# This can be removed once the official fix from the `mail` gem maintainers
+# has been released and the gem bumped by us.
+#
+# Details: https://community.openproject.org/projects/openproject/work_packages/42385/activity
+module OpenProject
+ module Patches
+ module MailSmtpStartTlsAutoHotfix
+ def build_smtp_session
+ super.tap do |smtp|
+ smtp.disable_starttls if disable_starttls?
+ end
+ end
+
+ def disable_starttls?
+ settings[:enable_starttls_auto] == false && !settings[:enable_starttls]
+ end
+ end
+ end
+end
+
+require 'mail'
+
+Mail::SMTP.prepend OpenProject::Patches::MailSmtpStartTlsAutoHotfix
diff --git a/spec/lib/open_project/configuration_spec.rb b/spec/lib/open_project/configuration_spec.rb
index 4e04211307a..653935c7663 100644
--- a/spec/lib/open_project/configuration_spec.rb
+++ b/spec/lib/open_project/configuration_spec.rb
@@ -139,6 +139,7 @@ describe OpenProject::Configuration, :settings_reset do
port: 25,
domain: 'example.com',
enable_starttls_auto: true,
+ openssl_verify_mode: 'peer',
ssl: false)
end
end
@@ -164,6 +165,7 @@ describe OpenProject::Configuration, :settings_reset do
port: 25,
domain: 'example.com',
enable_starttls_auto: false,
+ openssl_verify_mode: 'peer',
ssl: true)
end
end
@@ -191,6 +193,7 @@ describe OpenProject::Configuration, :settings_reset do
user_name: 'username',
password: 'p4ssw0rd',
enable_starttls_auto: true,
+ openssl_verify_mode: 'peer',
ssl: false)
end
end
@@ -218,6 +221,7 @@ describe OpenProject::Configuration, :settings_reset do
user_name: 'username',
password: 'p4ssw0rd',
enable_starttls_auto: false,
+ openssl_verify_mode: 'peer',
ssl: true)
end
end
diff --git a/spec/mailers/smtp_settings_spec.rb b/spec/mailers/smtp_settings_spec.rb
new file mode 100644
index 00000000000..652f08a30cc
--- /dev/null
+++ b/spec/mailers/smtp_settings_spec.rb
@@ -0,0 +1,169 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+require 'spec_helper'
+
+describe "SMTP settings" do
+ let(:smtp_settings) { {} }
+ let(:enable_starttls_auto) { nil }
+ let(:openssl_verify_mode) { nil }
+ let(:ssl) { nil }
+
+ before do
+ smtp_settings.clear
+ smtp_settings.merge! ActionMailer::Base.smtp_settings
+
+ allow(Setting).to receive(:email_delivery_method).and_return :smtp
+ allow(ActionMailer::Base).to receive(:delivery_method).and_return :smtp
+ allow(ActionMailer::Base).to receive(:smtp_settings).and_return smtp_settings
+
+ if !enable_starttls_auto.nil?
+ allow(Setting).to receive(:smtp_enable_starttls_auto?).and_return enable_starttls_auto
+ end
+
+ if !openssl_verify_mode.nil?
+ allow(Setting).to receive(:smtp_openssl_verify_mode).and_return openssl_verify_mode
+ end
+
+ if !ssl.nil?
+ allow(Setting).to receive(:smtp_ssl?).and_return ssl
+ end
+
+ OpenProject::Configuration.reload_mailer_configuration!
+ end
+
+ def send_mail
+ ActionMailer::Base
+ .mail(from: "[email protected]", to: "[email protected]", subject: "Test mail", body: "body")
+ .deliver_now
+ end
+
+ describe "enable_starttls_auto" do
+ context "by default" do
+ before do
+ expect(smtp_settings[:enable_starttls_auto]).to be false
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).not_to be_starttls_auto
+ end
+ end
+
+ it "tries sending the email without STARTTLS auto" do
+ send_mail
+ end
+ end
+
+ context "with enable_starttls_auto: true" do
+ let(:enable_starttls_auto) { true }
+
+ before do
+ expect(smtp_settings[:enable_starttls_auto]).to be true
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).to be_starttls_auto
+ end
+ end
+
+ it "tries sending the email with STARTTLS auto" do
+ send_mail
+ end
+ end
+ end
+
+ describe "openssl_verify_mode" do
+ context "by default" do
+ before do
+ expect(smtp_settings[:ssl]).to be false
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).not_to be_tls
+ end
+ end
+
+ it "tries sending the email without SSL to begin with" do
+ send_mail
+ end
+ end
+
+ context "with SSL enabled" do
+ let(:ssl) { true }
+
+ before do
+ expect(smtp_settings[:ssl]).to be true
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).to be_tls
+ expect(instance.instance_variable_get('@ssl_context_tls').verify_mode).to eq OpenSSL::SSL::VERIFY_PEER
+ end
+ end
+
+ it "tries sending the email with SSL, with verification" do
+ send_mail
+ end
+ end
+
+ context "with SSL enabled and verification disabled" do
+ let(:ssl) { true }
+ let(:openssl_verify_mode) { 'none' }
+
+ before do
+ expect(smtp_settings[:ssl]).to be true
+ expect(smtp_settings[:openssl_verify_mode]).to eq 'none'
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).to be_tls
+ expect(instance.instance_variable_get('@ssl_context_tls').verify_mode).to eq OpenSSL::SSL::VERIFY_NONE
+ end
+ end
+
+ it "tries sending the email with SSL but without verification" do
+ send_mail
+ end
+ end
+
+ # just a test case making sure our starttls patch doesn't disable tls in general by mistake
+ context "with SSL enabled and starttls disabled" do
+ let(:ssl) { true }
+ let(:enable_starttls_auto) { false }
+
+ before do
+ expect(smtp_settings[:ssl]).to be true
+ expect(smtp_settings[:enable_starttls_auto]).to be false
+
+ expect_any_instance_of(Net::SMTP).to receive(:start) do |instance|
+ expect(instance).to be_tls
+ expect(instance).not_to be_starttls_auto
+ expect(instance.instance_variable_get('@ssl_context_tls').verify_mode).to eq OpenSSL::SSL::VERIFY_PEER
+ end
+ end
+
+ it "tries sending the email with SSL, with verification" do
+ send_mail
+ end
+ end
+ end
+end
\ No newline at end of file