|
6 | 6 | Rack::Attack.enabled = true |
7 | 7 |
|
8 | 8 | # Cache store required to work. |
9 | | -Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new # defaults to Rails.cache |
| 9 | +# Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new # defaults to Rails.cache |
| 10 | + |
| 11 | +Rack::Attack.cache.store = ActiveSupport::Cache::MemCacheStore.new |
| 12 | +# Rack::Attack.cache.store = ActiveSupport::Cache::FileStore.new("/tmp/cache") |
10 | 13 |
|
11 | 14 | # Throttle should send a 429 Error responsec code and display public/429.html |
12 | 15 | Rack::Attack.throttled_responder = lambda do |_env| |
13 | 16 | html = ActionView::Base.empty.render(file: 'public/429.html') |
14 | 17 | [429, { 'Content-Type' => 'text/html' }, [html]] |
15 | 18 | end |
16 | 19 |
|
17 | | -# Throttle attempts to a particular path. 2 POSTs to /users/password every 30 seconds |
18 | | -Rack::Attack.throttle "password_resets/ip", limit: 2, period: 30.seconds do |req| |
19 | | - req.post? && req.path == "/users/password" && req.ip |
20 | | -end |
| 20 | +# # Throttle attempts to a particular path. 2 POSTs to /users/password every 30 seconds |
| 21 | +# Rack::Attack.throttle "password_resets/ip", limit: 2, period: 30.seconds do |req| |
| 22 | +# req.ip if req.post? && req.path == "/users/password" |
| 23 | +# end |
21 | 24 |
|
22 | 25 | # Throttle attempts to a particular path. 4 POSTs to /users/sign_in every 30 seconds |
23 | 26 | Rack::Attack.throttle "logins/ip", limit: 4, period: 30.seconds do |req| |
24 | 27 | # Don't apply sign-in rate-limiting to test environment |
25 | | - req.post? && req.path == "/users/sign_in" && req.ip unless Rails.env.test? |
| 28 | + (req.ip if req.post? && req.path == "/users/sign_in") unless Rails.env.test? |
| 29 | +end |
| 30 | + |
| 31 | +# Throttle attempts to a particular path. 3 POST or PUTS to /users every 20 seconds |
| 32 | +# This includes password updates. This is to prevent brute force attacks on user.accept_terms. |
| 33 | +# This replaces the previous throttle on /users/password and is extended to other profile update. |
| 34 | +# Exclude test environment. |
| 35 | +Rack::Attack.throttle "profile_updates", limit: 3, period: 20.seconds do |req| |
| 36 | + if !Rails.env.test? && req.path != "/users/sign_in" && (req.put? || req.post?) && req.path.start_with?("/users") |
| 37 | + puts "Throttling #{req.ip} for #{req.path}" |
| 38 | + req.ip |
| 39 | + end |
26 | 40 | end |
0 commit comments