2023-02-22 11:55:31 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
require 'rails_helper'
|
|
|
|
|
2023-04-30 14:43:28 +10:00
|
|
|
describe Rack::Attack, type: :request do
|
2022-11-15 06:26:31 +11:00
|
|
|
def app
|
|
|
|
Rails.application
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'throttled endpoint' do
|
2023-02-22 20:28:52 +11:00
|
|
|
before do
|
|
|
|
# Rack::Attack periods are not rolling, so avoid flaky tests by setting the time in a way
|
|
|
|
# to avoid crossing period boundaries.
|
|
|
|
|
|
|
|
# The code Rack::Attack uses to set periods is the following:
|
|
|
|
# https://github.com/rack/rack-attack/blob/v6.6.1/lib/rack/attack/cache.rb#L64-L66
|
|
|
|
# So we want to minimize `Time.now.to_i % period`
|
|
|
|
|
|
|
|
travel_to Time.zone.at((Time.now.to_i / period.seconds).to_i * period.seconds)
|
|
|
|
end
|
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
context 'when the number of requests is lower than the limit' do
|
|
|
|
it 'does not change the request status' do
|
|
|
|
limit.times do
|
|
|
|
request.call
|
2023-04-30 14:43:28 +10:00
|
|
|
expect(response).to_not have_http_status(429)
|
2022-11-15 06:26:31 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the number of requests is higher than the limit' do
|
2023-02-22 20:28:52 +11:00
|
|
|
it 'returns http too many requests after limit and returns to normal status after period' do
|
2022-11-15 06:26:31 +11:00
|
|
|
(limit * 2).times do |i|
|
|
|
|
request.call
|
2023-04-30 14:43:28 +10:00
|
|
|
expect(response).to have_http_status(429) if i > limit
|
2022-11-15 06:26:31 +11:00
|
|
|
end
|
2023-02-22 20:28:52 +11:00
|
|
|
|
|
|
|
travel period
|
|
|
|
|
|
|
|
request.call
|
2023-04-30 14:43:28 +10:00
|
|
|
expect(response).to_not have_http_status(429)
|
2022-11-15 06:26:31 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:remote_ip) { '1.2.3.5' }
|
|
|
|
|
|
|
|
describe 'throttle excessive sign-up requests by IP address' do
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when accessed through the website' do
|
2023-02-22 20:28:52 +11:00
|
|
|
let(:limit) { 25 }
|
|
|
|
let(:period) { 5.minutes }
|
2023-04-30 14:43:28 +10:00
|
|
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
2022-11-15 06:26:31 +11:00
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with exact path' do
|
2023-02-18 08:56:20 +11:00
|
|
|
let(:path) { '/auth' }
|
2023-02-19 09:10:19 +11:00
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
it_behaves_like 'throttled endpoint'
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with path with format' do
|
2023-02-18 08:56:20 +11:00
|
|
|
let(:path) { '/auth.html' }
|
2023-02-19 09:10:19 +11:00
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
it_behaves_like 'throttled endpoint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'when accessed through the API' do
|
2023-02-22 20:28:52 +11:00
|
|
|
let(:limit) { 5 }
|
|
|
|
let(:period) { 30.minutes }
|
2023-04-30 14:43:28 +10:00
|
|
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
2022-11-15 06:26:31 +11:00
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with exact path' do
|
2023-02-18 08:56:20 +11:00
|
|
|
let(:path) { '/api/v1/accounts' }
|
2023-02-19 09:10:19 +11:00
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
it_behaves_like 'throttled endpoint'
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with path with format' do
|
2022-11-15 06:26:31 +11:00
|
|
|
let(:path) { '/api/v1/accounts.json' }
|
|
|
|
|
|
|
|
it 'returns http not found' do
|
|
|
|
request.call
|
2023-04-30 14:43:28 +10:00
|
|
|
expect(response).to have_http_status(404)
|
2022-11-15 06:26:31 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'throttle excessive sign-in requests by IP address' do
|
2023-02-22 20:28:52 +11:00
|
|
|
let(:limit) { 25 }
|
|
|
|
let(:period) { 5.minutes }
|
2023-04-30 14:43:28 +10:00
|
|
|
let(:request) { -> { post path, headers: { 'REMOTE_ADDR' => remote_ip } } }
|
2022-11-15 06:26:31 +11:00
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with exact path' do
|
2023-02-18 08:56:20 +11:00
|
|
|
let(:path) { '/auth/sign_in' }
|
2023-02-19 09:10:19 +11:00
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
it_behaves_like 'throttled endpoint'
|
|
|
|
end
|
|
|
|
|
2023-05-04 13:49:08 +10:00
|
|
|
context 'with path with format' do
|
2023-02-18 08:56:20 +11:00
|
|
|
let(:path) { '/auth/sign_in.html' }
|
2023-02-19 09:10:19 +11:00
|
|
|
|
2022-11-15 06:26:31 +11:00
|
|
|
it_behaves_like 'throttled endpoint'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|