|
| 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