diff options
Diffstat (limited to 'spec/lib/gitlab/rack_attack_spec.rb')
-rw-r--r-- | spec/lib/gitlab/rack_attack_spec.rb | 212 |
1 files changed, 210 insertions, 2 deletions
diff --git a/spec/lib/gitlab/rack_attack_spec.rb b/spec/lib/gitlab/rack_attack_spec.rb index d72863b0103..5748e1e49e5 100644 --- a/spec/lib/gitlab/rack_attack_spec.rb +++ b/spec/lib/gitlab/rack_attack_spec.rb @@ -22,8 +22,7 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do stub_const("Rack::Attack", fake_rack_attack) stub_const("Rack::Attack::Request", fake_rack_attack_request) - # Expect rather than just allow, because this is actually fairly important functionality - expect(fake_rack_attack).to receive(:throttled_response_retry_after_header=).with(true) + allow(fake_rack_attack).to receive(:throttled_response=) allow(fake_rack_attack).to receive(:throttle) allow(fake_rack_attack).to receive(:track) allow(fake_rack_attack).to receive(:safelist) @@ -36,6 +35,12 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do expect(fake_rack_attack_request).to include(described_class::Request) end + it 'configures the throttle response' do + described_class.configure(fake_rack_attack) + + expect(fake_rack_attack).to have_received(:throttled_response=).with(an_instance_of(Proc)) + end + it 'configures the safelist' do described_class.configure(fake_rack_attack) @@ -93,4 +98,207 @@ RSpec.describe Gitlab::RackAttack, :aggregate_failures do end end end + + describe '.throttled_response_headers' do + where(:matched, :match_data, :headers) do + [ + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3600, + epoch_time: Time.utc(2021, 1, 5, 10, 29, 30).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '60', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1830' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3600, + epoch_time: Time.utc(2021, 1, 5, 10, 59, 59).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '60', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3600, + epoch_time: Time.utc(2021, 1, 5, 10, 0, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '60', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '3600' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3600, + epoch_time: Time.utc(2021, 1, 5, 23, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '60', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609891200', # Time.utc(2021, 1, 6, 0, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Wed, 06 Jan 2021 00:00:00 GMT', # Next day + 'Retry-After' => '1800' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3400, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '57', # 56.66 requests per minute + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1800' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 3700, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '62', # 61.66 requests per minute + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1800' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 59, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '1', # 0.9833 requests per minute + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1800' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 1.hour, + limit: 61, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '2', # 1.016 requests per minute + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609844400', # Time.utc(2021, 1, 5, 11, 0, 0).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 11:00:00 GMT', + 'Retry-After' => '1800' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 15.seconds, + limit: 10, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '40', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609842615', # Time.utc(2021, 1, 5, 10, 30, 15).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 10:30:15 GMT', + 'Retry-After' => '15' + } + ], + [ + 'throttle_unauthenticated', + { + discriminator: '127.0.0.1', + count: 3700, + period: 27.seconds, + limit: 10, + epoch_time: Time.utc(2021, 1, 5, 10, 30, 0).to_i + }, + { + 'RateLimit-Name' => 'throttle_unauthenticated', + 'RateLimit-Limit' => '23', + 'RateLimit-Observed' => '3700', + 'RateLimit-Remaining' => '0', + 'RateLimit-Reset' => '1609842627', # Time.utc(2021, 1, 5, 10, 30, 27).to_i.to_s + 'RateLimit-ResetTime' => 'Tue, 05 Jan 2021 10:30:27 GMT', + 'Retry-After' => '27' + } + ] + ] + end + + with_them do + it 'generates accurate throttled headers' do + expect(described_class.throttled_response_headers(matched, match_data)).to eql(headers) + end + end + end end |