Don't crash when email settings are invalid

Fixes: https://git.pleroma.social/pleroma/pleroma/-/issues/2606
Fixes: https://gitlab.com/soapbox-pub/soapbox/-/issues/4
This commit is contained in:
Alex Gleason 2021-05-03 14:27:03 -05:00
parent b221d77a6d
commit c80b1aaf51
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 47 additions and 27 deletions

View file

@ -34,15 +34,16 @@ defmodule Pleroma.ApplicationRequirements do
defp check_welcome_message_config!(:ok) do defp check_welcome_message_config!(:ok) do
if Pleroma.Config.get([:welcome, :email, :enabled], false) and if Pleroma.Config.get([:welcome, :email, :enabled], false) and
not Pleroma.Emails.Mailer.enabled?() do not Pleroma.Emails.Mailer.enabled?() do
Logger.error(""" Logger.warn("""
To send welcome email do you need to enable mail. To send welcome emails, you need to enable the mailer.
\nconfig :pleroma, Pleroma.Emails.Mailer, enabled: true Welcome emails will NOT be sent with the current config.
""")
{:error, "The mail disabled."} Enable the mailer:
else config :pleroma, Pleroma.Emails.Mailer, enabled: true
:ok """)
end end
:ok
end end
defp check_welcome_message_config!(result), do: result defp check_welcome_message_config!(result), do: result
@ -51,18 +52,21 @@ defmodule Pleroma.ApplicationRequirements do
# #
def check_confirmation_accounts!(:ok) do def check_confirmation_accounts!(:ok) do
if Pleroma.Config.get([:instance, :account_activation_required]) && if Pleroma.Config.get([:instance, :account_activation_required]) &&
not Pleroma.Config.get([Pleroma.Emails.Mailer, :enabled]) do not Pleroma.Emails.Mailer.enabled?() do
Logger.error( Logger.warn("""
"Account activation enabled, but no Mailer settings enabled.\n" <> Account activation is required, but the mailer is disabled.
"Please set config :pleroma, :instance, account_activation_required: false\n" <> Users will NOT be able to confirm their accounts with this config.
"Otherwise setup and enable Mailer." Either disable account activation or enable the mailer.
)
{:error, Disable account activation:
"Account activation enabled, but Mailer is disabled. Cannot send confirmation emails."} config :pleroma, :instance, account_activation_required: false
else
:ok Enable the mailer:
config :pleroma, Pleroma.Emails.Mailer, enabled: true
""")
end end
:ok
end end
def check_confirmation_accounts!(result), do: result def check_confirmation_accounts!(result), do: result

View file

@ -35,13 +35,13 @@ defmodule Pleroma.ApplicationRequirementsTest do
setup do: clear_config([:welcome]) setup do: clear_config([:welcome])
setup do: clear_config([Pleroma.Emails.Mailer]) setup do: clear_config([Pleroma.Emails.Mailer])
test "raises if welcome email enabled but mail disabled" do test "warns if welcome email enabled but mail disabled" do
clear_config([:welcome, :email, :enabled], true) clear_config([:welcome, :email, :enabled], true)
clear_config([Pleroma.Emails.Mailer, :enabled], false) clear_config([Pleroma.Emails.Mailer, :enabled], false)
assert_raise Pleroma.ApplicationRequirements.VerifyError, "The mail disabled.", fn -> assert capture_log(fn ->
capture_log(&Pleroma.ApplicationRequirements.verify!/0) assert Pleroma.ApplicationRequirements.verify!() == :ok
end end) =~ "Welcome emails will NOT be sent"
end end
end end
@ -57,15 +57,13 @@ defmodule Pleroma.ApplicationRequirementsTest do
setup do: clear_config([:instance, :account_activation_required]) setup do: clear_config([:instance, :account_activation_required])
test "raises if account confirmation is required but mailer isn't enable" do test "warns if account confirmation is required but mailer isn't enabled" do
clear_config([:instance, :account_activation_required], true) clear_config([:instance, :account_activation_required], true)
clear_config([Pleroma.Emails.Mailer, :enabled], false) clear_config([Pleroma.Emails.Mailer, :enabled], false)
assert_raise Pleroma.ApplicationRequirements.VerifyError, assert capture_log(fn ->
"Account activation enabled, but Mailer is disabled. Cannot send confirmation emails.", assert Pleroma.ApplicationRequirements.verify!() == :ok
fn -> end) =~ "Users will NOT be able to confirm their accounts"
capture_log(&Pleroma.ApplicationRequirements.verify!/0)
end
end end
test "doesn't do anything if account confirmation is disabled" do test "doesn't do anything if account confirmation is disabled" do

View file

@ -572,6 +572,24 @@ defmodule Pleroma.UserTest do
) )
end end
test "it fails gracefully with invalid email config" do
cng = User.register_changeset(%User{}, @full_user_data)
# Disable the mailer but enable all the things that want to send emails
clear_config([Pleroma.Emails.Mailer, :enabled], false)
clear_config([:instance, :account_activation_required], true)
clear_config([:instance, :account_approval_required], true)
clear_config([:welcome, :email, :enabled], true)
clear_config([:welcome, :email, :sender], "lain@lain.com")
# The user is still created
assert {:ok, %User{nickname: "nick"}} = User.register(cng)
# No emails are sent
ObanHelpers.perform_all()
refute_email_sent()
end
test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do test "it requires an email, name, nickname and password, bio is optional when account_activation_required is enabled" do
clear_config([:instance, :account_activation_required], true) clear_config([:instance, :account_activation_required], true)