Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure normalized_email gets anonymized (#29719)

Roman Rizzid97d48ead1f1
diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rb
index 8dd04bd5bd0..21c02cd1448 100644
--- a/app/services/user_anonymizer.rb
+++ b/app/services/user_anonymizer.rb
@@ -40,7 +40,9 @@ class UserAnonymizer
end
@user.save!
+
@user.primary_email.update_attribute(:email, "#{@user.username}#{EMAIL_SUFFIX}")
+ @user.primary_email.update_attribute(:normalized_email, "#{@user.username}#{EMAIL_SUFFIX}")
options = @user.user_option
options.mailing_list_mode = false
diff --git a/db/migrate/20241112145744_anonymize_normalized_email_column.rb b/db/migrate/20241112145744_anonymize_normalized_email_column.rb
new file mode 100644
index 00000000000..9a5e36dd94c
--- /dev/null
+++ b/db/migrate/20241112145744_anonymize_normalized_email_column.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+class AnonymizeNormalizedEmailColumn < ActiveRecord::Migration[7.1]
+ def up
+ execute <<~SQL
+ UPDATE user_emails
+ SET normalized_email = email
+ WHERE SPLIT_PART(email, '@', 2) = 'anonymized.invalid'
+ SQL
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rb
index 5a753b4f37d..ad5ede53563 100644
--- a/spec/services/user_anonymizer_spec.rb
+++ b/spec/services/user_anonymizer_spec.rb
@@ -39,6 +39,14 @@ RSpec.describe UserAnonymizer do
expect(user.reload.email).to eq("#{user.username}@anonymized.invalid")
end
+ it "changes the primary email normalized email address" do
+ make_anonymous
+
+ primary_email = user.reload.primary_email
+
+ expect(primary_email.normalized_email).to eq("#{user.username}@anonymized.invalid")
+ end
+
it "changes the primary email address when there is an email domain allowlist" do
SiteSetting.allowed_email_domains = "example.net|wayne.com|discourse.org"