From 335e8d7b0bf6174ee6a24cd0177bfd475b724f71 Mon Sep 17 00:00:00 2001 From: noellabo Date: Tue, 28 May 2019 11:49:11 +0900 Subject: [PATCH] Revert "Improve rate limiting (#10860)" This reverts commit 0e9b8be18ab0c3d2b0fe24eaee1cfe0e711a52cd. --- config/initializers/rack_attack.rb | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index e6c9f9bd4a5347..ae3eede6685110 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -13,10 +13,6 @@ def authenticated_token ) end - def remote_ip - @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s - end - def authenticated_user_id authenticated_token&.resource_owner_id end @@ -32,10 +28,6 @@ def api_request? def web_request? !api_request? end - - def paging_request? - params['page'].present? || params['min_id'].present? || params['max_id'].present? || params['since_id'].present? - end end PROTECTED_PATHS = %w( @@ -50,15 +42,15 @@ def paging_request? # (blocklist & throttles are skipped) Rack::Attack.safelist('allow from localhost') do |req| # Requests are allowed if the return value is truthy - req.remote_ip == '127.0.0.1' || req.remote_ip == '::1' + req.ip == '127.0.0.1' || req.ip == '::1' end throttle('throttle_authenticated_api', limit: 300, period: 5.minutes) do |req| req.authenticated_user_id if req.api_request? end - throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req| - req.remote_ip if req.api_request? && !req.authenticated? + throttle('throttle_unauthenticated_api', limit: 7_500, period: 5.minutes) do |req| + req.ip if req.api_request? end throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req| @@ -66,20 +58,11 @@ def paging_request? end throttle('throttle_media_proxy', limit: 30, period: 30.minutes) do |req| - req.remote_ip if req.path.start_with?('/media_proxy') + req.ip if req.path.start_with?('/media_proxy') end throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req| - req.remote_ip if req.post? && req.path == '/api/v1/accounts' - end - - # Throttle paging, as it is mainly used for public pages and AP collections - throttle('throttle_authenticated_paging', limit: 300, period: 15.minutes) do |req| - req.authenticated_user_id if req.paging_request? - end - - throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req| - req.remote_ip if req.paging_request? && !req.authenticated? + req.ip if req.post? && req.path == '/api/v1/accounts' end API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze @@ -90,7 +73,7 @@ def paging_request? end throttle('protected_paths', limit: 25, period: 5.minutes) do |req| - req.remote_ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX + req.ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX end self.throttled_response = lambda do |env|