Skip to content

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

SECURITY: Prevent abuse of the update_activation_email route (#17694)

Roman Rizzi7c73e896cea1
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 4911472e5d9..a15a3223c83 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1108,10 +1108,12 @@ class UsersController < ApplicationController
RateLimiter.new(nil, "activate-edit-email-hr-#{request.remote_ip}", 5, 1.hour).performed!
if params[:username].present?
+ RateLimiter.new(nil, "activate-edit-email-hr-username-#{params[:username]}", 5, 1.hour).performed!
@user = User.find_by_username_or_email(params[:username])
raise Discourse::InvalidAccess.new unless @user.present?
raise Discourse::InvalidAccess.new unless @user.confirm_password?(params[:password])
elsif user_key = session[SessionController::ACTIVATE_USER_KEY]
+ RateLimiter.new(nil, "activate-edit-email-hr-user-key-#{user_key}", 5, 1.hour).performed!
@user = User.where(id: user_key.to_i).first
end
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index e986135e51c..4b2491435f6 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -3717,6 +3717,23 @@ describe UsersController do
token.reload
expect(token.expired?).to eq(true)
end
+
+ it 'tells the user to slow down after many requests' do
+ RateLimiter.enable
+ RateLimiter.clear_all!
+ freeze_time
+
+ user = post_user
+ token = user.email_tokens.first
+
+ 6.times do |n|
+ put "/u/update-activation-email.json", params: {
+ email: "updatedemail#{n}@example.com"
+ }, env: { "REMOTE_ADDR": "1.2.3.#{n}" }
+ end
+
+ expect(response.status).to eq(429)
+ end
end
context "with a username and password" do
@@ -3791,6 +3808,25 @@ describe UsersController do
token.reload
expect(token.expired?).to eq(true)
end
+
+ it 'tells the user to slow down after many requests' do
+ RateLimiter.enable
+ RateLimiter.clear_all!
+ freeze_time
+
+ user = inactive_user
+ token = user.email_tokens.first
+
+ 6.times do |n|
+ put "/u/update-activation-email.json", params: {
+ username: user.username,
+ password: 'qwerqwer123',
+ email: "updatedemail#{n}@example.com"
+ }, env: { "REMOTE_ADDR": "1.2.3.#{n}" }
+ end
+
+ expect(response.status).to eq(429)
+ end
end
end