discourse/spec/requests/api/rate_limits_spec.rb
Alan Guo Xiang Tan 859d61003e
DEV: API to register custom request rate limiting conditions (#30239)
This commit adds the `add_request_rate_limiter` plugin API which allows plugins to add custom rate limiters on top of the default rate limiters which requests by a user's id or the request's IP address.

Example to add a rate limiter that rate limits all requests from Googlebot under the same rate limit bucket:

```
add_request_rate_limiter(
  identifier: :country,
  key: ->(request) { "country/#{DiscourseIpInfo.get(request.ip)[:country]}" },
  activate_when: ->(request) { DiscourseIpInfo.get(request.ip)[:country].present? },
)
```
2024-12-23 09:57:18 +08:00

154 lines
2.9 KiB
Ruby

# frozen_string_literal: true
class MockRateLimiter
LimitExceeded = RateLimiter::LimitExceeded
def self.disable
end
def self.enable
end
def self.performed!(*args, **kwargs)
end
def self.rollback!(*args, **kwargs)
end
def initialize(*args, **kwargs)
@args = args
@kwargs = kwargs
end
def can_perform?
true
end
def performed!
MockRateLimiter.performed!(*@args, **@kwargs)
end
def rollback!
MockRateLimiter.rollback!(*@args, **@kwargs)
end
end
RSpec.describe "rate limits" do
before_all { @key = Fabricate(:api_key).key }
let(:api_key) { @key }
let!(:api_username) { "system" }
around(:each) { |example| stub_const(Object, :RateLimiter, MockRateLimiter) { example.run } }
it "doesn't rate limit authenticated admin api requests" do
MockRateLimiter
.expects(:performed!)
.with(
nil,
"global_limit_60_ip/192.0.2.1",
200,
60,
global: true,
error_code: "ip_60_secs_limit",
aggressive: true,
)
.once
MockRateLimiter
.expects(:performed!)
.with(
nil,
"global_limit_10_ip/192.0.2.1",
50,
10,
global: true,
error_code: "ip_10_secs_limit",
aggressive: true,
)
.once
MockRateLimiter
.expects(:performed!)
.with(nil, "admin_api_min", 60, 60, error_code: "admin_api_key_rate_limit")
.once
MockRateLimiter
.expects(:rollback!)
.with(
nil,
"global_limit_60_ip/192.0.2.1",
200,
60,
global: true,
error_code: "ip_60_secs_limit",
aggressive: true,
)
.once
MockRateLimiter
.expects(:rollback!)
.with(
nil,
"global_limit_10_ip/192.0.2.1",
50,
10,
global: true,
error_code: "ip_10_secs_limit",
aggressive: true,
)
.once
get(
"/admin/backups.json",
headers: {
"Api-Key" => api_key,
"Api-Username" => api_username,
},
env: {
REMOTE_ADDR: "192.0.2.1",
},
)
expect(response.status).to eq(200)
end
it "doesn't rollback rate limits for unauthenticated admin api requests" do
MockRateLimiter
.expects(:performed!)
.with(
nil,
"global_limit_60_ip/192.0.2.1",
200,
60,
global: true,
error_code: "ip_60_secs_limit",
aggressive: true,
)
.once
MockRateLimiter
.expects(:performed!)
.with(
nil,
"global_limit_10_ip/192.0.2.1",
50,
10,
global: true,
error_code: "ip_10_secs_limit",
aggressive: true,
)
.once
get(
"/admin/backups.json",
headers: {
"Api-Key" => "bogus key",
"Api-Username" => api_username,
},
env: {
REMOTE_ADDR: "192.0.2.1",
},
)
expect(response.status).to eq(404)
end
end