Skip to content

Commit 27d91aa

Browse files
author
John Pinto
committed
Updated Rack Attack configuration to address vulnerabilities in password updates.
Changes: The fix involves adding a new Rack Attack rule "profile_updates/ip" and rewriting the body of the rules "password_resets/ip" and "logins/ip" so the the request ip is returned if the rule is triggered.
1 parent 56759df commit 27d91aa

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Changelog
22

3+
- Updated Rack Attack configuration to address vulnerabilities in password updates.
4+
35
## v4.2.0
46

57
**Note this upgrade is mainly a migration from Bootstrap 3 to Bootstrap 5.**

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ gem 'jwt'
109109
gem 'pundit'
110110

111111
# Gem for throttling malicious attacks
112+
gem 'dalli', '~> 3.2', '>= 3.2.8'
112113
gem 'rack-attack', '~> 6.6', '>= 6.6.1'
113114

114115
# ========== #

config/initializers/rack_attack.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@
66
Rack::Attack.enabled = true
77

88
# 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")
1013

1114
# Throttle should send a 429 Error responsec code and display public/429.html
1215
Rack::Attack.throttled_responder = lambda do |_env|
1316
html = ActionView::Base.empty.render(file: 'public/429.html')
1417
[429, { 'Content-Type' => 'text/html' }, [html]]
1518
end
1619

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
2124

2225
# Throttle attempts to a particular path. 4 POSTs to /users/sign_in every 30 seconds
2326
Rack::Attack.throttle "logins/ip", limit: 4, period: 30.seconds do |req|
2427
# 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
2640
end

0 commit comments

Comments
 (0)