From d4a52da6ca4dc7e1f10affd3e2323f6613769e50 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Mon, 27 May 2019 17:52:51 +0200 Subject: [PATCH 1/3] Rate limit based on remote address IP, not on potential reverse proxy --- config/initializers/rack_attack.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index ae3eede668511..5f07f36195486 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -13,6 +13,10 @@ 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 @@ -42,7 +46,7 @@ def web_request? # (blocklist & throttles are skipped) Rack::Attack.safelist('allow from localhost') do |req| # Requests are allowed if the return value is truthy - req.ip == '127.0.0.1' || req.ip == '::1' + req.remote_ip == '127.0.0.1' || req.remote_ip == '::1' end throttle('throttle_authenticated_api', limit: 300, period: 5.minutes) do |req| @@ -50,7 +54,7 @@ def web_request? end throttle('throttle_unauthenticated_api', limit: 7_500, period: 5.minutes) do |req| - req.ip if req.api_request? + req.remote_ip if req.api_request? end throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req| @@ -58,11 +62,11 @@ def web_request? end throttle('throttle_media_proxy', limit: 30, period: 30.minutes) do |req| - req.ip if req.path.start_with?('/media_proxy') + req.remote_ip if req.path.start_with?('/media_proxy') end throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req| - req.ip if req.post? && req.path == '/api/v1/accounts' + req.remote_ip if req.post? && req.path == '/api/v1/accounts' end API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze @@ -73,7 +77,7 @@ def web_request? end throttle('protected_paths', limit: 25, period: 5.minutes) do |req| - req.ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX + req.remote_ip if req.post? && req.path =~ PROTECTED_PATHS_REGEX end self.throttled_response = lambda do |env| From e227a66044e04b869d82af1408ec5a8c0f6cf043 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Mon, 27 May 2019 18:14:11 +0200 Subject: [PATCH 2/3] Limit rate of unauthenticated API requests further --- config/initializers/rack_attack.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index 5f07f36195486..aa4fb1c2e7add 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -53,8 +53,8 @@ def web_request? req.authenticated_user_id if req.api_request? end - throttle('throttle_unauthenticated_api', limit: 7_500, period: 5.minutes) do |req| - req.remote_ip if req.api_request? + throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req| + req.remote_ip if req.api_request? && !req.authenticated? end throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req| From 4019e06b23b7dced426fde14cea75e67874bfb93 Mon Sep 17 00:00:00 2001 From: Thibaut Girka Date: Mon, 27 May 2019 18:36:14 +0200 Subject: [PATCH 3/3] Rate-limit paging requests to one every 3 seconds --- config/initializers/rack_attack.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/config/initializers/rack_attack.rb b/config/initializers/rack_attack.rb index aa4fb1c2e7add..e6c9f9bd4a534 100644 --- a/config/initializers/rack_attack.rb +++ b/config/initializers/rack_attack.rb @@ -32,6 +32,10 @@ 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( @@ -69,6 +73,15 @@ def web_request? 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? + end + API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+/.freeze