From 6aefa3260e7b244239da45264c442357dc50ac8a Mon Sep 17 00:00:00 2001 From: kouhai Date: Mon, 15 Apr 2024 00:33:47 -0700 Subject: [PATCH] th: add config for unsafe throttle deactivation Suggested-by: Ariadne Conill --- config/initializers/rack_attack.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index 14fab7ecda..0f817e19ca 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -3,6 +3,9 @@ require 'doorkeeper/grape/authorization_decorator' class Rack::Attack + TH_DEACTIVATE_THROTTLES = !!ENV['TH_DEACTIVATE_THROTTLES'] + TH_DEACTIVATE_DANGEROUS_THROTTLES = !!ENV['TH_DEACTIVATE_DANGEROUS_THROTTLES'] + class Request def 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| 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| 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| 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| 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| 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_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| 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| 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| 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| req.session[:attempt_user_id] || req.params.dig('user', 'email').presence if req.post? && req.path_matches?('/auth/sign_in')