th: add config for unsafe throttle deactivation

Suggested-by: Ariadne Conill <ariadne@dereferenced.org>
main
kouhai 2024-04-15 00:33:47 -07:00
parent ced041cb1e
commit 4a72eacbc6
1 changed files with 8 additions and 5 deletions

View File

@ -3,6 +3,9 @@
require 'doorkeeper/grape/authorization_decorator' require 'doorkeeper/grape/authorization_decorator'
class Rack::Attack class Rack::Attack
TH_DEACTIVATE_THROTTLES = !!ENV['TH_DEACTIVATE_THROTTLES']
TH_DEACTIVATE_DANGEROUS_THROTTLES = !!ENV['TH_DEACTIVATE_DANGEROUS_THROTTLES']
class Request class Request
def authenticated_token def authenticated_token
return @authenticated_token if defined?(@authenticated_token) return @authenticated_token if defined?(@authenticated_token)
@ -76,7 +79,7 @@ class Rack::Attack
throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req| throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req|
req.throttleable_remote_ip if req.api_request? && req.unauthenticated? req.throttleable_remote_ip if req.api_request? && req.unauthenticated?
end end unless TH_DEACTIVATE_THROTTLES
throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req| throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req|
req.authenticated_user_id if req.post? && req.path.match?(%r{\A/api/v\d+/media\z}i) req.authenticated_user_id if req.post? && req.path.match?(%r{\A/api/v\d+/media\z}i)
@ -84,7 +87,7 @@ class Rack::Attack
throttle('throttle_media_proxy', limit: 30, period: 10.minutes) do |req| throttle('throttle_media_proxy', limit: 30, period: 10.minutes) do |req|
req.throttleable_remote_ip if req.path.start_with?('/media_proxy') req.throttleable_remote_ip if req.path.start_with?('/media_proxy')
end end unless TH_DEACTIVATE_THROTTLES
throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req| throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req|
req.throttleable_remote_ip if req.post? && req.path == '/api/v1/accounts' req.throttleable_remote_ip if req.post? && req.path == '/api/v1/accounts'
@ -96,7 +99,7 @@ class Rack::Attack
throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req| throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req|
req.throttleable_remote_ip if req.paging_request? && req.unauthenticated? req.throttleable_remote_ip if req.paging_request? && req.unauthenticated?
end end unless TH_DEACTIVATE_THROTTLES
API_DELETE_REBLOG_REGEX = %r{\A/api/v1/statuses/\d+/unreblog\z} API_DELETE_REBLOG_REGEX = %r{\A/api/v1/statuses/\d+/unreblog\z}
API_DELETE_STATUS_REGEX = %r{\A/api/v1/statuses/\d+\z} API_DELETE_STATUS_REGEX = %r{\A/api/v1/statuses/\d+\z}
@ -115,7 +118,7 @@ class Rack::Attack
throttle('throttle_password_resets/ip', limit: 25, period: 5.minutes) do |req| throttle('throttle_password_resets/ip', limit: 25, period: 5.minutes) do |req|
req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/password') req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/password')
end end unless TH_DEACTIVATE_DANGEROUS_THROTTLES
throttle('throttle_password_resets/email', limit: 5, period: 30.minutes) do |req| throttle('throttle_password_resets/email', limit: 5, period: 30.minutes) do |req|
req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/password') req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/password')
@ -135,7 +138,7 @@ class Rack::Attack
throttle('throttle_login_attempts/ip', limit: 25, period: 5.minutes) do |req| throttle('throttle_login_attempts/ip', limit: 25, period: 5.minutes) do |req|
req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/sign_in') req.throttleable_remote_ip if req.post? && req.path_matches?('/auth/sign_in')
end end unless TH_DEACTIVATE_DANGEROUS_THROTTLES
throttle('throttle_login_attempts/email', limit: 25, period: 1.hour) do |req| 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_matches?('/auth/sign_in') req.session[:attempt_user_id] || req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/sign_in')