Skip to content

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

FIX: anonymize more things when anonymizing user (#36242)

Samd727cb5c6f5e
diff --git a/app/jobs/regular/anonymize_user.rb b/app/jobs/regular/anonymize_user.rb
index 71069f2c79c..2d698153007 100644
--- a/app/jobs/regular/anonymize_user.rb
+++ b/app/jobs/regular/anonymize_user.rb
@@ -45,6 +45,7 @@ module Jobs
TopicViewItem.where(ip_where).update_all(ip_address: new_ip)
UserHistory.where(ip_where("acting_user_id")).update_all(ip_address: new_ip)
UserProfileView.where(ip_where).update_all(ip_address: new_ip)
+ UserIpAddressHistory.where(user_id: @user_id).delete_all
# UserHistory for delete_user logs the user's IP. Note this is quite ugly but we don't
# have a better way of querying on details right now.
diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rb
index 1026230f171..bfd30127b9b 100644
--- a/app/services/user_anonymizer.rb
+++ b/app/services/user_anonymizer.rb
@@ -70,6 +70,11 @@ class UserAnonymizer
@user.user_associated_accounts.destroy_all
@user.api_keys.destroy_all
@user.user_api_keys.destroy_all
+ @user.user_auth_tokens.destroy_all
+ @user.user_second_factors.destroy_all
+ UserSecurityKey.where(user_id: @user.id).delete_all
+ @user.push_subscriptions.destroy_all
+ PostReplyKey.where(user_id: @user.id).delete_all
@user.user_emails.secondary.destroy_all
@user_history = log_action
diff --git a/spec/services/user_anonymizer_spec.rb b/spec/services/user_anonymizer_spec.rb
index 1ee995a07b6..c9aba7aed7c 100644
--- a/spec/services/user_anonymizer_spec.rb
+++ b/spec/services/user_anonymizer_spec.rb
@@ -247,6 +247,42 @@ RSpec.describe UserAnonymizer do
expect(user.user_api_keys).to be_empty
end
+ it "removes user auth tokens" do
+ UserAuthToken.generate!(user_id: user.id)
+
+ expect { make_anonymous }.to change { UserAuthToken.count }.by(-1)
+ expect(user.reload.user_auth_tokens).to be_empty
+ end
+
+ it "removes second factor credentials" do
+ Fabricate(:user_second_factor_totp, user: user)
+
+ expect { make_anonymous }.to change { UserSecondFactor.count }.by(-1)
+ expect(user.reload.user_second_factors).to be_empty
+ end
+
+ it "removes security keys" do
+ Fabricate(:user_security_key_with_random_credential, user: user)
+
+ expect { make_anonymous }.to change { UserSecurityKey.count }.by(-1)
+ expect(UserSecurityKey.where(user_id: user.id).count).to eq(0)
+ end
+
+ it "removes push subscriptions" do
+ Fabricate(:push_subscription, user: user)
+
+ expect { make_anonymous }.to change { PushSubscription.count }.by(-1)
+ expect(user.reload.push_subscriptions).to be_empty
+ end
+
+ it "removes post reply keys" do
+ post = Fabricate(:post)
+ PostReplyKey.create!(user_id: user.id, post_id: post.id)
+
+ expect { make_anonymous }.to change { PostReplyKey.count }.by(-1)
+ expect(PostReplyKey.where(user_id: user.id).count).to eq(0)
+ end
+
context "when executing jobs" do
before { Jobs.run_immediately! }
@@ -385,6 +421,8 @@ RSpec.describe UserAnonymizer do
delete_history = StaffActionLogger.new(admin).log_user_deletion(user)
user_history = StaffActionLogger.new(user).log_backup_create
+ ip_address_history = UserIpAddressHistory.create!(user_id: user.id, ip_address: old_ip)
+
UserAnonymizer.make_anonymous(user, admin, anonymize_ip: anon_ip)
expect(user.registration_ip_address).to eq(anon_ip)
expect(link.reload.ip_address).to eq(anon_ip)
@@ -396,6 +434,7 @@ RSpec.describe UserAnonymizer do
expect(delete_history.reload.ip_address).to eq(anon_ip)
expect(user_history.reload.ip_address).to eq(anon_ip)
expect(user_profile_view.reload.ip_address).to eq(anon_ip)
+ expect(UserIpAddressHistory.exists?(id: ip_address_history.id)).to eq(false)
end
end