Skip to content

Commit

Permalink
Add rate limiting to login requests
Browse files Browse the repository at this point in the history
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
jayjay-w committed Jan 11, 2024
1 parent e5b5d25 commit e60a914
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ gem 'graphiql-rails', git: 'https://github.com/meedan/graphiql-rails.git', ref:
gem 'graphql-formatter'
gem 'nokogiri', '1.14.3'
gem 'puma'
gem 'rack-attack'
gem 'rack-cors', '1.0.6', require: 'rack/cors'
gem 'sidekiq', '5.2.10'
gem 'sidekiq-cloudwatchmetrics'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,8 @@ GEM
raabro (1.4.0)
racc (1.7.1)
rack (2.2.6.4)
rack-attack (6.7.0)
rack (>= 1.0, < 4)
rack-cors (1.0.6)
rack (>= 1.6.0)
rack-protection (2.2.0)
Expand Down Expand Up @@ -983,6 +985,7 @@ DEPENDENCIES
puma
pusher
rack (= 2.2.6.4)
rack-attack
rack-cors (= 1.0.6)
rack-protection (= 2.2.0)
railroady
Expand Down
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,8 @@ class Application < Rails::Application
})

config.active_record.yaml_column_permitted_classes = [Time, Symbol]

# Rack Attack Configuration
config.middleware.use Rack::Attack
end
end
2 changes: 2 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,6 @@
else
puts '[WARNING] config.hosts not provided. Only requests from localhost are allowed. To change, update `whitelisted_hosts` in config.yml'
end

config.hosts << "api.check.orb.local"
end
2 changes: 2 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,6 @@
config.after_initialize do
PaperTrail.enabled = ENV['PAPERTRAIL_ENABLED'] || false
end

config.cache_store = :memory_store
end
16 changes: 16 additions & 0 deletions config/initializers/rack_attack.rb
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
12 changes: 12 additions & 0 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e60a914

Please sign in to comment.