A platform for community discussion. Free, open, simple.
FIX: handle email aliases when normalize emails is enabled (#42344)
David Battersby68b9d6857602
diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rbindex 00f08044b9c..0b651bcb4de 100644--- a/app/controllers/admin/email_templates_controller.rb+++ b/app/controllers/admin/email_templates_controller.rb@@ -31,6 +31,7 @@ class Admin::EmailTemplatesController < Admin::AdminControllersystem_messages.email_reject_attachmentsystem_messages.email_reject_auto_generatedsystem_messages.email_reject_bad_destination_address+ system_messages.email_reject_email_aliassystem_messages.email_reject_emptysystem_messages.email_reject_inactive_usersystem_messages.email_reject_insufficient_trust_leveldiff --git a/app/models/user_email.rb b/app/models/user_email.rbindex 19810374e6b..2616b518ec9 100644--- a/app/models/user_email.rb+++ b/app/models/user_email.rb@@ -40,13 +40,22 @@ class UserEmail < ActiveRecord::Baseendend+ # Strips dots from the local part and everything between "+" and "@", so that+ # aliases of the same address share a single normalized form.+ def self.normalize(email)+ local_part, domain = email.to_s.split("@", 2)+ return if local_part.blank? || domain.blank?++ "#{local_part.gsub(".", "").gsub(/\+.*/, "")}@#{domain}"+ end++ def self.find_by_normalized(email)+ normalized = normalize(Email.downcase(email))+ where("lower(normalized_email) = ?", normalized).first if normalized.present?+ end+def normalize_email- self.normalized_email =- if email.present?- username, domain = email.split("@", 2)- username = username.gsub(".", "").gsub(/\+.*/, "")- "#{username}@#{domain}"- end+ self.normalized_email = self.class.normalize(email)endprivatediff --git a/app/services/user/action/find_by_email.rb b/app/services/user/action/find_by_email.rbindex cddd995062f..f39dba7bf37 100644--- a/app/services/user/action/find_by_email.rb+++ b/app/services/user/action/find_by_email.rb@@ -16,10 +16,9 @@ class User::Action::FindByEmail < Service::ActionBasedef normalized_email_matchreturn if !SiteSetting.normalize_emails- local_part, domain = email.split("@", 2)- return if local_part.blank? || domain.blank?+ normalized_email = UserEmail.normalize(email)+ return if normalized_email.blank?- normalized_email = "#{local_part.gsub(".", "").gsub(/\+.*/, "")}@#{domain}"users.joins(:user_emails).where(user_emails: { normalized_email: normalized_email }).firstendenddiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 936781825d0..447590007bd 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -289,6 +289,7 @@ en:screened_email_error: "Happens when the sender's email address was already screened."unsubscribe_not_allowed: "Happens when unsubscribing via email is not allowed for this user."email_not_allowed: "Happens when the email address is not on the allowlist or is on the blocklist."+ email_alias_not_allowed: "Happens when the sender's address is an alias of an address already registered to an account, and normalized emails are required to be unique."unrecognized_error: "Unrecognized Error"secure_uploads_placeholder: "Redacted: This site has secure uploads enabled, visit the topic or click View Media to see the attached uploads."view_redacted_media: "View Media"@@ -1127,6 +1128,7 @@ en:error: "There was an error changing your email address. Perhaps the address is already in use?"doesnt_exist: "That email address is not associated with your account."error_staged: "There was an error changing your email address. The address is already in use by a staged user."+ error_alias: "That email address is an alias of an address already in use."already_done: "Sorry, this confirmation link is no longer valid. Perhaps your email was already changed?"confirm: "Confirm"max_secondary_emails_error: "You have reached the maximum allowed secondary emails limit."@@ -4084,6 +4086,15 @@ en:Your reply was sent from a blocked email address. Try sending from another email address, or [contact a staff member](%{base_url}/about).+ email_reject_email_alias:+ title: "Email Reject Email Alias"+ subject_template: "[%{email_prefix}] Email issue -- unrecognized email address"+ subject_template_improved: "%{email_prefix}: Email issue -- unrecognized email address"+ text_body_template: |+ We're sorry, but your email message to %{destination} (titled %{former_title}) didn't work.++ We couldn't match the address you sent from to an account on this site. Try sending from the email address you signed up with, or [contact a staff member](%{base_url}/about).+email_reject_inactive_user:title: "Email Reject Inactive User"subject_template: "[%{email_prefix}] Email issue -- Inactive User"diff --git a/lib/email/processor.rb b/lib/email/processor.rbindex e47ff2565cd..f445e3fe05b 100644--- a/lib/email/processor.rb+++ b/lib/email/processor.rb@@ -61,6 +61,8 @@ module Email:email_reject_screened_emailwhen Email::Receiver::EmailNotAllowed:email_reject_not_allowed_email+ when Email::Receiver::EmailAliasNotAllowed+ :email_reject_email_aliaswhen Email::Receiver::AutoGeneratedEmailError:email_reject_auto_generatedwhen Email::Receiver::InactiveUserErrordiff --git a/lib/email/receiver.rb b/lib/email/receiver.rbindex 6d81b969283..10d77ca6421 100644--- a/lib/email/receiver.rb+++ b/lib/email/receiver.rb@@ -76,6 +76,9 @@ module Emailclass EmailNotAllowed < ProcessingErrorend+ class EmailAliasNotAllowed < ProcessingError+ end+class OldDestinationError < ProcessingErrorend@@ -792,6 +795,10 @@ module Emailif user.nil? && SiteSetting.enable_staged_usersraise EmailNotAllowed unless EmailValidator.allowed?(email)+ # Staging would be rejected by the normalized uniqueness rule, which is a+ # deliberate block rather than an unexpected failure. Callers that tolerate+ # a failed create keep getting nil, so only the sender aborts the email.+ raise EmailAliasNotAllowed if raise_on_failed_create && normalized_email_taken?(email)username = UserNameSuggester.sanitize_username(display_name) if display_name.present?begin@@ -819,6 +826,10 @@ module Emailfind_or_create_user(email, display_name, raise_on_failed_create: true)end+ def normalized_email_taken?(email)+ SiteSetting.normalize_emails? && UserEmail.find_by_normalized(email).present?+ end+def all_destinations@all_destinations ||= [@mail.destinations,diff --git a/lib/email_updater.rb b/lib/email_updater.rbindex 2f9e609cf5e..b1c532898e8 100644--- a/lib/email_updater.rb+++ b/lib/email_updater.rb@@ -22,9 +22,22 @@ class EmailUpdaterEmailValidator.new(attributes: :email).validate_each(self, :email, email)return if errors.present?- if existing_user = User.find_by_email(email)- if SiteSetting.hide_email_address_taken+ existing_user = User.find_by_email(email)+ is_alias = false++ # Uniqueness is enforced on the normalized address, so an alias of an address+ # already in use has to be caught here rather than after the confirmation+ # round-trip, where it would only surface as a failed save.+ if existing_user.nil? && SiteSetting.normalize_emails?+ existing_user = UserEmail.find_by_normalized(email)&.user+ is_alias = existing_user.present?+ end++ if existing_user+ if SiteSetting.hide_email_address_taken && existing_user != @userJobs.enqueue(:critical_user_email, type: "account_exists", user_id: existing_user.id)+ elsif is_alias+ errors.add(:base, I18n.t("change_email.error_alias"))elseerror_message = +"change_email.error"error_message << "_staged" if existing_user.staged?diff --git a/spec/lib/email/receiver_spec.rb b/spec/lib/email/receiver_spec.rbindex e2a447bed74..f0fbc235c44 100644--- a/spec/lib/email/receiver_spec.rb+++ b/spec/lib/email/receiver_spec.rb@@ -1041,6 +1041,24 @@ RSpec.describe Email::Receiver doFabricate(:user, email: "[email protected]", trust_level: TrustLevel[3])expect { process(:tl3_user) }.to raise_error(Email::Receiver::InsufficientTrustLevelError)end++ context "when the sender's address is an alias of a registered address" do+ before { Fabricate(:user, email: "[email protected]") }++ it "raises an EmailAliasNotAllowed if normalize_emails is enabled" do+ SiteSetting.normalize_emails = true++ expect { process(:new_user) }.to raise_error(Email::Receiver::EmailAliasNotAllowed)+ expect(IncomingEmail.last.error).to eq("Email::Receiver::EmailAliasNotAllowed")+ end++ it "stages a new user if normalize_emails is disabled" do+ SiteSetting.normalize_emails = false++ expect { process(:new_user) }.to change(Topic, :count)+ expect(Topic.last.user.email).to eq("[email protected]")+ end+ endenddescribe "#reply_by_email_address_regex" dodiff --git a/spec/lib/email_updater_spec.rb b/spec/lib/email_updater_spec.rbindex d0f15f309fb..2f1cc0218cf 100644--- a/spec/lib/email_updater_spec.rb+++ b/spec/lib/email_updater_spec.rb@@ -439,6 +439,60 @@ RSpec.describe EmailUpdater doendend+ context "when the new email is an alias of an address already in use" do+ let(:user) { Fabricate(:user, email: old_email) }+ let(:updater) { EmailUpdater.new(guardian: user.guardian, user: user) }+ let(:alias_of_old_email) { "[email protected]" }++ before { SiteSetting.hide_email_address_taken = false }++ context "when normalize_emails is enabled" do+ before { SiteSetting.normalize_emails = true }++ it "errors instead of failing after the email is confirmed" do+ updater.change_to(alias_of_old_email, add: true)++ expect(updater.errors.messages[:base].first).to eq(I18n.t("change_email.error_alias"))+ expect(user.email_change_requests).to be_empty+ end++ it "keeps the generic error for an address that matches exactly" do+ Fabricate(:user, email: new_email)+ updater.change_to(new_email)++ expect(updater.errors.messages[:base].first).to eq(I18n.t("change_email.error"))+ end++ it "errors when the alias belongs to another user" do+ Fabricate(:user, email: new_email)+ updater.change_to("[email protected]")++ expect(updater.errors.messages[:base].first).to eq(I18n.t("change_email.error_alias"))+ expect(user.email_change_requests).to be_empty+ end++ it "errors for the user's own address even when hide_email_address_taken is enabled" do+ SiteSetting.hide_email_address_taken = true+ updater.change_to(alias_of_old_email, add: true)++ expect(updater.errors.messages[:base].first).to eq(I18n.t("change_email.error_alias"))+ expect(user.email_change_requests).to be_empty+ end+ end++ context "when normalize_emails is disabled" do+ before { SiteSetting.normalize_emails = false }++ it "allows adding the alias as a secondary email" do+ updater.change_to(alias_of_old_email, add: true)+ updater.confirm(updater.change_req.new_email_token.token)++ expect(updater.errors).to be_blank+ expect(user.reload.secondary_emails).to contain_exactly(alias_of_old_email)+ end+ end+ end+context "when hide_email_address_taken is enabled" dobefore { SiteSetting.hide_email_address_taken = true }diff --git a/spec/models/user_email_spec.rb b/spec/models/user_email_spec.rbindex 7224943d5c7..68ba800c808 100644--- a/spec/models/user_email_spec.rb+++ b/spec/models/user_email_spec.rb@@ -24,6 +24,41 @@ RSpec.describe UserEmail doendend+ describe ".normalize" do+ it "removes dots from the local part and everything between + and @" do+ expect(UserEmail.normalize("[email protected]")).to eq("[email protected]")+ expect(UserEmail.normalize("[email protected]")).to eq("[email protected]")+ expect(UserEmail.normalize("[email protected]")).to eq("[email protected]")+ end++ it "keeps dots in the domain" do+ expect(UserEmail.normalize("[email protected]")).to eq("[email protected]")+ end++ it "returns nil when there is no local part or domain" do+ expect(UserEmail.normalize("a.b")).to be_nil+ expect(UserEmail.normalize("@example.com")).to be_nil+ expect(UserEmail.normalize("")).to be_nil+ expect(UserEmail.normalize(nil)).to be_nil+ end+ end++ describe ".find_by_normalized" do+ it "finds an email whose normalized form matches" do+ user_email = user.user_emails.create!(email: "[email protected]", primary: false)++ expect(UserEmail.find_by_normalized("[email protected]")).to eq(user_email)+ expect(UserEmail.find_by_normalized("[email protected]")).to eq(user_email)+ expect(UserEmail.find_by_normalized("[email protected]")).to be_nil+ end++ it "does not match rows without a normalized email" do+ user.user_emails.update_all(normalized_email: nil)++ expect(UserEmail.find_by_normalized("a.b")).to be_nil+ end+ end+describe "normalized_email" doit "checks if normalized email is unique" doSiteSetting.normalize_emails = true