forked from fedi/mastodon
Change rate limits for various paths (#14253)
- Rate limit login attempts by target account - Rate limit password resets and e-mail re-confirmations by target account - Rate limit sign-up/login attempts, password resets, and e-mail re-confirmations by IP like before
This commit is contained in:
parent
844870273f
commit
81a3db1564
|
@ -38,15 +38,6 @@ class Rack::Attack
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
PROTECTED_PATHS = %w(
|
|
||||||
/auth/sign_in
|
|
||||||
/auth
|
|
||||||
/auth/password
|
|
||||||
/auth/confirmation
|
|
||||||
).freeze
|
|
||||||
|
|
||||||
PROTECTED_PATHS_REGEX = Regexp.union(PROTECTED_PATHS.map { |path| /\A#{Regexp.escape(path)}/ })
|
|
||||||
|
|
||||||
Rack::Attack.safelist('allow from localhost') do |req|
|
Rack::Attack.safelist('allow from localhost') do |req|
|
||||||
req.remote_ip == '127.0.0.1' || req.remote_ip == '::1'
|
req.remote_ip == '127.0.0.1' || req.remote_ip == '::1'
|
||||||
end
|
end
|
||||||
|
@ -86,8 +77,32 @@ class Rack::Attack
|
||||||
req.authenticated_user_id if (req.post? && req.path =~ API_DELETE_REBLOG_REGEX) || (req.delete? && req.path =~ API_DELETE_STATUS_REGEX)
|
req.authenticated_user_id if (req.post? && req.path =~ API_DELETE_REBLOG_REGEX) || (req.delete? && req.path =~ API_DELETE_STATUS_REGEX)
|
||||||
end
|
end
|
||||||
|
|
||||||
throttle('protected_paths', limit: 25, period: 5.minutes) do |req|
|
throttle('throttle_sign_up_attempts/ip', limit: 25, period: 5.minutes) do |req|
|
||||||
req.remote_ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX
|
req.remote_ip if req.post? && req.path == '/auth'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_password_resets/ip', limit: 25, period: 5.minutes) do |req|
|
||||||
|
req.remote_ip if req.post? && req.path == '/auth/password'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_password_resets/email', limit: 5, period: 30.minutes) do |req|
|
||||||
|
req.params.dig('user', 'email').presence if req.post? && req.path == '/auth/password'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_email_confirmations/ip', limit: 25, period: 5.minutes) do |req|
|
||||||
|
req.remote_ip if req.post? && req.path == '/auth/confirmation'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_email_confirmations/email', limit: 5, period: 30.minutes) do |req|
|
||||||
|
req.params.dig('user', 'email').presence if req.post? && req.path == '/auth/password'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_login_attempts/ip', limit: 25, period: 5.minutes) do |req|
|
||||||
|
req.remote_ip if req.post? && req.path == '/auth/sign_in'
|
||||||
|
end
|
||||||
|
|
||||||
|
throttle('throttle_login_attempts/email', limit: 25, period: 1.hour) do |req|
|
||||||
|
req.session[:attempt_user_id] || req.params.dig('user', 'email').presence if req.post? && req.path == '/auth/sign_in'
|
||||||
end
|
end
|
||||||
|
|
||||||
self.throttled_response = lambda do |env|
|
self.throttled_response = lambda do |env|
|
||||||
|
|
|
@ -2,5 +2,6 @@ ActiveSupport::Notifications.subscribe(/rack_attack/) do |_name, _start, _finish
|
||||||
req = payload[:request]
|
req = payload[:request]
|
||||||
|
|
||||||
next unless [:throttle, :blacklist].include? req.env['rack.attack.match_type']
|
next unless [:throttle, :blacklist].include? req.env['rack.attack.match_type']
|
||||||
|
|
||||||
Rails.logger.info("Rate limit hit (#{req.env['rack.attack.match_type']}): #{req.ip} #{req.request_method} #{req.fullpath}")
|
Rails.logger.info("Rate limit hit (#{req.env['rack.attack.match_type']}): #{req.ip} #{req.request_method} #{req.fullpath}")
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue