Skip to content

Commit 84022e6

Browse files
committed
Added Rack-Attack
1 parent d51b8bf commit 84022e6

File tree

4 files changed

+84
-3
lines changed

4 files changed

+84
-3
lines changed

Gemfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,10 @@ end
173173

174174
group :production do
175175
gem 'honeybadger'
176-
gem 'rails_12factor'
176+
gem 'newrelic_resque_agent'
177+
gem 'newrelic_rpm'
177178
gem 'puma'
178179
gem 'puma_worker_killer'
179-
gem 'newrelic_rpm'
180-
gem 'newrelic_resque_agent'
180+
gem 'rack-attack'
181+
gem 'rails_12factor'
181182
end

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ GEM
416416
quiet_assets (1.0.3)
417417
railties (>= 3.1, < 5.0)
418418
rack (1.4.5)
419+
rack-attack (4.1.0)
420+
rack
419421
rack-cache (1.2)
420422
rack (>= 0.4)
421423
rack-protection (1.5.3)
@@ -682,6 +684,7 @@ DEPENDENCIES
682684
puma_worker_killer
683685
querystring
684686
quiet_assets
687+
rack-attack
685688
rails (~> 3.2)
686689
rails-erd
687690
rails_12factor

config/environments/production.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
config.assets.digest = true
2929
config.static_cache_control = 'public, max-age=31536000'
3030
config.host = ENV['HOST_DOMAIN']
31+
config.middleware.use('Rack::Attack')
3132
end

config/initializers/rack-attack.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
if Rails.env.production?
2+
class Rack::Attack
3+
4+
### Configure Cache ###
5+
6+
# If you don't want to use Rails.cache (Rack::Attack's default), then
7+
# configure it here.
8+
#
9+
# Note: The store is only used for throttling (not blacklisting and
10+
# whitelisting). It must implement .increment and .write like
11+
# ActiveSupport::Cache::Store
12+
13+
# Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
14+
15+
### Throttle Spammy Clients ###
16+
17+
# If any single client IP is making tons of requests, then they're probably
18+
# malicious or a poorly-configured scraper. Either way, they don't deserve
19+
# to hog all of the app server's CPU. Cut them off!
20+
21+
# Throttle all requests by IP (60rpm)
22+
#
23+
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
24+
throttle('req/ip', limit: 300, period: 5.minutes) do |req|
25+
req.ip
26+
end
27+
28+
### Prevent Brute-Force Login Attacks ###
29+
30+
# The most common brute-force login attack is a brute-force password attack
31+
# where an attacker simply tries a large number of emails and passwords to
32+
# see if any credentials match.
33+
#
34+
# Another common method of attack is to use a swarm of computers with
35+
# different IPs to try brute-forcing a password for a specific account.
36+
37+
# Throttle POST requests to /login by IP address
38+
#
39+
# Key: "rack::attack:#{Time.now.to_i/:period}:logins/ip:#{req.ip}"
40+
throttle('logins/ip', limit: 5, period: 20.seconds) do |req|
41+
if req.path == '/login' && req.post?
42+
req.ip
43+
end
44+
end
45+
46+
# Throttle POST requests to /login by email param
47+
#
48+
# Key: "rack::attack:#{Time.now.to_i/:period}:logins/email:#{req.email}"
49+
#
50+
# Note: This creates a problem where a malicious user could intentionally
51+
# throttle logins for another user and force their login requests to be
52+
# denied, but that's not very common and shouldn't happen to you. (Knock on
53+
# wood!)
54+
throttle("logins/email", limit: 5, period: 20.seconds) do |req|
55+
if req.path == '/login' && req.post?
56+
# return the email if present, nil otherwise
57+
req.params['email'].presence
58+
end
59+
end
60+
61+
### Custom Throttle Response ###
62+
63+
# By default, Rack::Attack returns an HTTP 429 for throttled responses,
64+
# which is just fine.
65+
#
66+
# If you want to return 503 so that the attacker might be fooled into
67+
# believing that they've successfully broken your app (or you just want to
68+
# customize the response), then uncomment these lines.
69+
# throttled_response = lambda do |env|
70+
# [ 503, # status
71+
# {}, # headers
72+
# ['']] # body
73+
# end
74+
end
75+
end
76+

0 commit comments

Comments
 (0)