2018-03-17 18:26:18 +05:30
|
|
|
module Gitlab::Throttle
|
|
|
|
def self.settings
|
|
|
|
Gitlab::CurrentSettings.current_application_settings
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.unauthenticated_options
|
|
|
|
limit_proc = proc { |req| settings.throttle_unauthenticated_requests_per_period }
|
|
|
|
period_proc = proc { |req| settings.throttle_unauthenticated_period_in_seconds.seconds }
|
|
|
|
{ limit: limit_proc, period: period_proc }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.authenticated_api_options
|
|
|
|
limit_proc = proc { |req| settings.throttle_authenticated_api_requests_per_period }
|
|
|
|
period_proc = proc { |req| settings.throttle_authenticated_api_period_in_seconds.seconds }
|
|
|
|
{ limit: limit_proc, period: period_proc }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.authenticated_web_options
|
|
|
|
limit_proc = proc { |req| settings.throttle_authenticated_web_requests_per_period }
|
|
|
|
period_proc = proc { |req| settings.throttle_authenticated_web_period_in_seconds.seconds }
|
|
|
|
{ limit: limit_proc, period: period_proc }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Rack::Attack
|
|
|
|
throttle('throttle_unauthenticated', Gitlab::Throttle.unauthenticated_options) do |req|
|
|
|
|
Gitlab::Throttle.settings.throttle_unauthenticated_enabled &&
|
|
|
|
req.unauthenticated? &&
|
2018-11-08 19:23:39 +05:30
|
|
|
!req.should_be_skipped? &&
|
2018-03-17 18:26:18 +05:30
|
|
|
req.ip
|
|
|
|
end
|
|
|
|
|
|
|
|
throttle('throttle_authenticated_api', Gitlab::Throttle.authenticated_api_options) do |req|
|
|
|
|
Gitlab::Throttle.settings.throttle_authenticated_api_enabled &&
|
|
|
|
req.api_request? &&
|
2018-11-29 20:51:05 +05:30
|
|
|
req.authenticated_user_id([:api])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
throttle('throttle_authenticated_web', Gitlab::Throttle.authenticated_web_options) do |req|
|
|
|
|
Gitlab::Throttle.settings.throttle_authenticated_web_enabled &&
|
|
|
|
req.web_request? &&
|
2018-11-29 20:51:05 +05:30
|
|
|
req.authenticated_user_id([:api, :rss, :ics])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
class Request
|
|
|
|
def unauthenticated?
|
2018-11-29 20:51:05 +05:30
|
|
|
!authenticated_user_id([:api, :rss, :ics])
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
2018-11-29 20:51:05 +05:30
|
|
|
def authenticated_user_id(request_formats)
|
|
|
|
Gitlab::Auth::RequestAuthenticator.new(self).user(request_formats)&.id
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def api_request?
|
|
|
|
path.start_with?('/api')
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
def api_internal_request?
|
|
|
|
path =~ %r{^/api/v\d+/internal/}
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def should_be_skipped?
|
|
|
|
api_internal_request?
|
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
def web_request?
|
|
|
|
!api_request?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|