adapt remote_ip determination for CF

This commit is contained in:
robcolbert 2019-07-04 04:05:34 -04:00
parent 80d47a33a4
commit 5443767b3f
1 changed files with 12 additions and 10 deletions

View File

@ -14,7 +14,9 @@ class Rack::Attack
end end
def remote_ip def remote_ip
@remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s # @remote_ip ||= (@env["action_dispatch.remote_ip"] || ip).to_s
# @env['HTTP_CF_CONNECTING_IP'] ? @env['HTTP_CF_CONNECTING_IP'] : ip
@remote_ip ||= (@env["HTTP_CF_CONNECTING_IP"] || ip).to_s
end end
def authenticated_user_id def authenticated_user_id
@ -53,43 +55,43 @@ class Rack::Attack
req.remote_ip == '127.0.0.1' || req.remote_ip == '::1' req.remote_ip == '127.0.0.1' || req.remote_ip == '::1'
end end
throttle('throttle_authenticated_api', limit: 250000, period: 5.minutes) do |req| throttle('throttle_authenticated_api', limit: 300, period: 5.minutes) do |req|
req.authenticated_user_id if req.api_request? req.authenticated_user_id if req.api_request?
end end
throttle('throttle_unauthenticated_api', limit: 250000, period: 5.minutes) do |req| throttle('throttle_unauthenticated_api', limit: 300, period: 5.minutes) do |req|
req.remote_ip if req.api_request? && req.unauthenticated? req.remote_ip if req.api_request? && req.unauthenticated?
end end
throttle('throttle_api_media', limit: 250000, period: 5.minutes) do |req| throttle('throttle_api_media', limit: 30, period: 30.minutes) do |req|
req.authenticated_user_id if req.post? && req.path.start_with?('/api/v1/media') req.authenticated_user_id if req.post? && req.path.start_with?('/api/v1/media')
end end
throttle('throttle_media_proxy', limit: 250000, period: 5.minutes) do |req| throttle('throttle_media_proxy', limit: 30, period: 30.minutes) do |req|
req.remote_ip if req.path.start_with?('/media_proxy') req.remote_ip if req.path.start_with?('/media_proxy')
end end
throttle('throttle_api_sign_up', limit: 250000, period: 5.minutes) do |req| throttle('throttle_api_sign_up', limit: 5, period: 30.minutes) do |req|
req.remote_ip if req.post? && req.path == '/api/v1/accounts' req.remote_ip if req.post? && req.path == '/api/v1/accounts'
end end
# Throttle paging, as it is mainly used for public pages and AP collections # Throttle paging, as it is mainly used for public pages and AP collections
throttle('throttle_authenticated_paging', limit: 250000, period: 5.minutes) do |req| throttle('throttle_authenticated_paging', limit: 300, period: 15.minutes) do |req|
req.authenticated_user_id if req.paging_request? req.authenticated_user_id if req.paging_request?
end end
throttle('throttle_unauthenticated_paging', limit: 250000, period: 5.minutes) do |req| throttle('throttle_unauthenticated_paging', limit: 300, period: 15.minutes) do |req|
req.remote_ip if req.paging_request? && req.unauthenticated? req.remote_ip if req.paging_request? && req.unauthenticated?
end end
API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze API_DELETE_REBLOG_REGEX = /\A\/api\/v1\/statuses\/[\d]+\/unreblog/.freeze
API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+/.freeze API_DELETE_STATUS_REGEX = /\A\/api\/v1\/statuses\/[\d]+/.freeze
throttle('throttle_api_delete', limit: 250000, period: 5.minutes) do |req| throttle('throttle_api_delete', limit: 30, period: 30.minutes) do |req|
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: 250000, period: 5.minutes) do |req| throttle('protected_paths', 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 =~ PROTECTED_PATHS_REGEX
end end