-
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.
FInalize on throttling and rate limiting
- Moved limits to the configuration file - Added tests for all limits
- Loading branch information
Showing
6 changed files
with
49 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
class Rack::Attack | ||
# Throttle login attempts by IP address | ||
throttle('logins/ip', limit: 10, period: 60.seconds) do |req| | ||
throttle('logins/ip', limit: ChheckConfig.get('login_rate_limit', 10).to_i, 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: 10, period: 60.seconds) do |req| | ||
throttle('logins/email', limit: ChheckConfig.get('login_rate_limit', 10).to_i, 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: ChheckConfig.get('api_rate_limit', 100).to_i, 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 |
---|---|---|
|
@@ -74,9 +74,9 @@ def setup | |
test "should lock user after excessive login requests" do | ||
u = create_user login: 'test', password: '12345678', password_confirmation: '12345678', email: '[email protected]' | ||
u.confirm | ||
maximum_attempts = Devise.maximum_attempts | ||
Devise.maximum_attempts = 2 | ||
|
||
maximum_attempts.times do | ||
2.times do | ||
post :create, params: { api_user: { email: '[email protected]', password: '12345679' } } | ||
end | ||
|
||
|
@@ -94,7 +94,7 @@ def setup | |
post :create, params: { api_user: { email: '[email protected]', password: '12345679' } } | ||
end | ||
|
||
travel_to 2.hours.from_now do | ||
travel_to CheckConfig.get('devise_unlock_accounts_after', 5).to_i + 1.hours.from_now do | ||
u.reload | ||
assert !u.access_locked? | ||
assert_nil u.locked_at | ||
|
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,31 @@ | ||
require 'test_helper' | ||
|
||
class ThrottlingTest < ActionDispatch::IntegrationTest | ||
setup do | ||
Rails.cache.clear | ||
end | ||
|
||
test "should throttle excessive requests to /api/graphql" do | ||
limit = 100 | ||
|
||
limit.times do | ||
post api_graphql_path | ||
assert_response :unauthorized | ||
end | ||
|
||
get api_graphql_path | ||
assert_response :too_many_requests | ||
end | ||
|
||
test "should throttle excessive requests to /api/users/sign_in" do | ||
limit = 10 | ||
user_params = { api_user: { email: '[email protected]', password: 'password' } } | ||
|
||
limit.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 |