Module: Gitlab::RackAttack
- Defined in:
- lib/gitlab/rack_attack.rb,
lib/gitlab/rack_attack/store.rb,
lib/gitlab/rack_attack/request.rb,
lib/gitlab/rack_attack/user_allowlist.rb,
lib/gitlab/rack_attack/request_throttle_data.rb
Defined Under Namespace
Modules: Request Classes: RequestThrottleData, Store, ThrottleDefinition, UserAllowlist
Class Method Summary collapse
- .configure(rack_attack) ⇒ Object
- .configure_throttles(rack_attack) ⇒ Object
- .configure_user_allowlist ⇒ Object
- .throttle_definitions ⇒ Object
- .throttle_definitions_authenticated_git_http ⇒ Object
- .throttle_definitions_unauthenticated_git_http ⇒ Object
- .throttle_or_track(rack_attack, throttle_name, *args, &block) ⇒ Object
- .track?(name) ⇒ Boolean
- .user_allowlist ⇒ Object
Class Method Details
.configure(rack_attack) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/gitlab/rack_attack.rb', line 10 def self.configure(rack_attack) # This adds some methods used by our throttles to the `Rack::Request` rack_attack::Request.include(Gitlab::RackAttack::Request) # This is Rack::Attack::DEFAULT_THROTTLED_RESPONSE, modified to allow a custom response rack_attack.throttled_responder = ->(request) do throttled_headers = Gitlab::RackAttack::RequestThrottleData.from_rack_attack( request.env['rack.attack.matched'], request.env['rack.attack.match_data'] )&.throttled_response_headers [429, { 'Content-Type' => 'text/plain' }.merge(throttled_headers || {}), [Gitlab::Throttle.rate_limiting_response_text]] end rack_attack.cache.store = Gitlab::RackAttack::Store.new configure_throttles(rack_attack) configure_user_allowlist end |
.configure_throttles(rack_attack) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/gitlab/rack_attack.rb', line 103 def self.configure_throttles(rack_attack) # Each of these settings follows the same pattern of specifying separate # authenticated and unauthenticated rates via settings Gitlab::Throttle::REGULAR_THROTTLES.each do |throttle| = Gitlab::Throttle.(throttle, authenticated: false) throttle_or_track(rack_attack, "throttle_unauthenticated_#{throttle}", ) do |req| if req.throttle?(throttle, authenticated: false) req.ip end end = Gitlab::Throttle.(throttle, authenticated: true) throttle_or_track(rack_attack, "throttle_authenticated_#{throttle}", ) do |req| if req.throttle?(throttle, authenticated: true) req.throttled_identifer([:api]) end end end throttle_definitions.each do |name, definition| throttle_or_track(rack_attack, name, definition., &definition.request_identifier) end rack_attack.safelist('throttle_bypass_header') do |req| Gitlab::Throttle.bypass_header.present? && req.get_header(Gitlab::Throttle.bypass_header) == '1' end end |
.configure_user_allowlist ⇒ Object
28 29 30 31 |
# File 'lib/gitlab/rack_attack.rb', line 28 def self.configure_user_allowlist @user_allowlist = nil user_allowlist end |
.throttle_definitions ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gitlab/rack_attack.rb', line 35 def self.throttle_definitions { 'throttle_unauthenticated_web' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.ip if req.throttle_unauthenticated_web? } ), # Product analytics feature is in experimental stage. # At this point we want to limit amount of events registered # per application (aid stands for application id). 'throttle_product_analytics_collector' => ThrottleDefinition.new( { limit: 100, period: 60 }, ->(req) { req.params['aid'] if req.product_analytics_collector_request? } ), 'throttle_authenticated_web' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api, :rss, :ics]) if req.throttle_authenticated_web? } ), 'throttle_unauthenticated_protected_paths' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.ip if req.throttle_unauthenticated_protected_paths? } ), 'throttle_authenticated_protected_paths_api' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api]) if req.throttle_authenticated_protected_paths_api? } ), 'throttle_authenticated_protected_paths_web' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api, :rss, :ics]) if req.throttle_authenticated_protected_paths_web? } ), 'throttle_unauthenticated_get_protected_paths' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.ip if req.throttle_unauthenticated_get_protected_paths? } ), 'throttle_authenticated_get_protected_paths_api' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api]) if req.throttle_authenticated_get_protected_paths_api? } ), 'throttle_authenticated_get_protected_paths_web' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api, :rss, :ics]) if req.throttle_authenticated_get_protected_paths_web? } ), 'throttle_authenticated_git_lfs' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api]) if req.throttle_authenticated_git_lfs? } ), **throttle_definitions_unauthenticated_git_http, **throttle_definitions_authenticated_git_http } end |
.throttle_definitions_authenticated_git_http ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/gitlab/rack_attack.rb', line 94 def self.throttle_definitions_authenticated_git_http { 'throttle_authenticated_git_http' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.throttled_identifer([:api]) if req.throttle_authenticated_git_http? } ) } end |
.throttle_definitions_unauthenticated_git_http ⇒ Object
85 86 87 88 89 90 91 92 |
# File 'lib/gitlab/rack_attack.rb', line 85 def self.throttle_definitions_unauthenticated_git_http { 'throttle_unauthenticated_git_http' => ThrottleDefinition.new( Gitlab::Throttle., ->(req) { req.ip if req.throttle_unauthenticated_git_http? } ) } end |
.throttle_or_track(rack_attack, throttle_name, *args, &block) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/gitlab/rack_attack.rb', line 132 def self.throttle_or_track(rack_attack, throttle_name, *args, &block) if track?(throttle_name) rack_attack.track(throttle_name, *args, &block) else rack_attack.throttle(throttle_name, *args, &block) end end |
.track?(name) ⇒ Boolean
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/gitlab/rack_attack.rb', line 140 def self.track?(name) dry_run_config = ENV['GITLAB_THROTTLE_DRY_RUN'].to_s.strip return false if dry_run_config.empty? return true if dry_run_config == '*' dry_run_throttles = dry_run_config.split(',').map(&:strip) # `throttle_unauthenticated` was split into API and web, so to maintain backwards-compatibility # this throttle name now controls both rate limits. if dry_run_throttles.include?('throttle_unauthenticated') dry_run_throttles += %w[throttle_unauthenticated_api throttle_unauthenticated_web] end dry_run_throttles.include?(name) end |
.user_allowlist ⇒ Object
157 158 159 160 161 162 163 |
# File 'lib/gitlab/rack_attack.rb', line 157 def self.user_allowlist @user_allowlist ||= begin list = UserAllowlist.new(ENV['GITLAB_THROTTLE_USER_ALLOWLIST']) Gitlab::AuthLogger.info(gitlab_throttle_user_allowlist: list.to_a) list end end |