Skip to content

Commit

Permalink
Improve rate limiting (mastodon#10860)
Browse files Browse the repository at this point in the history
* Rate limit based on remote address IP, not on potential reverse proxy

* Limit rate of unauthenticated API requests further

* Rate-limit paging requests to one every 3 seconds
  • Loading branch information
ClearlyClaire authored and Gargron committed May 27, 2019
1 parent c4c6ddc commit 17b6fc7
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,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(
Expand All @@ -42,27 +50,36 @@ 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|
req.authenticated_user_id if req.api_request?
end

throttle('throttle_unauthenticated_api', limit: 7_500, period: 5.minutes) do |req|
req.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|
req.authenticated_user_id if req.post? && req.path.start_with?('/api/v1/media')
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

# 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
Expand All @@ -73,7 +90,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|
Expand Down

0 comments on commit 17b6fc7

Please sign in to comment.