-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rate limiting to login requests (#1777)
* Add rate limiting to login requests Add rate limiting to login requests using rack-attack. As a starting point, we are setting a limit of 5 request per minute to /api/users/sign_in. * Add lockout to user authentication Enable devise lockable to lock users after 5 consecutive incorrect login attempts. Account will be unlocked automatically after 1 hour.
- Loading branch information
Showing
12 changed files
with
117 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Rack::Attack | ||
# Throttle login attempts by IP address | ||
throttle('logins/ip', limit: proc { CheckConfig.get('login_rate_limit', 10, :integer) }, period: 60.seconds) do |req| | ||
if req.path == '/api/users/sign_in' && req.post? | ||
req.ip | ||
end | ||
end | ||
|
||
# Throttle login attempts by email address | ||
throttle('logins/email', limit: proc { CheckConfig.get('login_rate_limit', 10, :integer) }, period: 60.seconds) do |req| | ||
if req.path == '/api/users/sign_in' && req.post? | ||
# Return the email if present, nil otherwise | ||
req.params['user']['email'].presence if req.params['user'] | ||
end | ||
end | ||
|
||
# Throttle all graphql requests by IP address | ||
throttle('api/graphql', limit: proc { CheckConfig.get('api_rate_limit', 100, :integer) }, period: 60.seconds) do |req| | ||
req.ip if req.path == '/api/graphql' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddLockableToDevise < ActiveRecord::Migration[6.1] | ||
def change | ||
add_column :users, :failed_attempts, :integer, default: 0, null: false | ||
add_column :users, :unlock_token, :string | ||
add_column :users, :locked_at, :datetime | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,5 +71,35 @@ def setup | |
assert_not_nil @controller.current_api_user | ||
end | ||
|
||
test "should lock user after excessive login requests" do | ||
u = create_user login: 'test', password: '12345678', password_confirmation: '12345678', email: '[email protected]' | ||
u.confirm | ||
Devise.maximum_attempts = 2 | ||
|
||
2.times do | ||
post :create, params: { api_user: { email: '[email protected]', password: '12345679' } } | ||
end | ||
|
||
u.reload | ||
assert u.access_locked? | ||
assert_not_nil u.locked_at | ||
end | ||
|
||
test "should unlock locked user accounts after specified time" do | ||
u = create_user login: 'test', password: '12345678', password_confirmation: '12345678', email: '[email protected]' | ||
u.confirm | ||
maximum_attempts = Devise.maximum_attempts | ||
|
||
maximum_attempts.times do | ||
post :create, params: { api_user: { email: '[email protected]', password: '12345679' } } | ||
end | ||
|
||
travel_to CheckConfig.get('devise_unlock_accounts_after', 5, :integer) + 1.hours.from_now do | ||
u.reload | ||
assert !u.access_locked? | ||
assert_nil u.locked_at | ||
end | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'test_helper' | ||
|
||
class ThrottlingTest < ActionDispatch::IntegrationTest | ||
setup do | ||
Rails.cache.clear | ||
end | ||
|
||
test "should throttle excessive requests to /api/graphql" do | ||
stub_configs({ 'api_rate_limit' => 2 }) do | ||
2.times do | ||
post api_graphql_path | ||
assert_response :unauthorized | ||
end | ||
|
||
post api_graphql_path | ||
assert_response :too_many_requests | ||
end | ||
end | ||
|
||
test "should throttle excessive requests to /api/users/sign_in" do | ||
stub_configs({ 'login_rate_limit' => 2 }) do | ||
user_params = { api_user: { email: '[email protected]', password: 'password' } } | ||
|
||
2.times do | ||
post api_user_session_path, params: user_params, as: :json | ||
end | ||
|
||
post api_user_session_path, params: user_params, as: :json | ||
assert_response :too_many_requests | ||
end | ||
end | ||
end |