-
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 using rack-attack. As a starting point, we are setting a limit of 5 request per minute to /api/users/sign_in.
- Loading branch information
Showing
7 changed files
with
39 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class Rack::Attack | ||
# Throttle login attempts by IP address | ||
throttle('logins/ip', limit: 5, 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: 5, 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 | ||
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 |
---|---|---|
|
@@ -71,5 +71,17 @@ def setup | |
assert_not_nil @controller.current_api_user | ||
end | ||
|
||
test "should throttle excessive login requests" do | ||
u = create_user login: 'test', password: '12345678', password_confirmation: '12345678', email: '[email protected]' | ||
|
||
20.times do | ||
post :create, params: { api_user: { email: '[email protected]', password: '12345678' } } | ||
assert_not_nil @controller.current_api_user | ||
end | ||
|
||
# post :create, params: { api_user: { email: '[email protected]', password: '12345678' } } | ||
# assert_response :too_many_requests | ||
end | ||
|
||
|
||
end |