From f26ff290c3352010468aa6acee08b4a29ddeee75 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 22 Jan 2018 13:18:30 +1100 Subject: [PATCH] FEATURE: Shorten setting name to max_reqs So it is consistent with other settings --- config/discourse_defaults.conf | 8 +++--- lib/middleware/request_tracker.rb | 18 ++++++------- .../middleware/request_tracker_spec.rb | 26 +++++++++---------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.conf index 488ead77d54..fe949ee810a 100644 --- a/config/discourse_defaults.conf +++ b/config/discourse_defaults.conf @@ -176,10 +176,10 @@ max_user_api_reqs_per_day = 2880 max_admin_api_reqs_per_key_per_minute = 60 -max_requests_per_ip_per_minute = 200 -max_requests_per_ip_per_10_seconds = 50 +max_reqs_per_ip_per_minute = 200 +max_reqs_per_ip_per_10_seconds = 50 # global rate limiter will simply warn if the limit is exceeded, can be warn+block, warn, block or none -max_requests_per_ip_mode = none +max_reqs_per_ip_mode = none # bypass rate limiting any IP resolved as a private IP -max_requests_rate_limit_on_private = false +max_reqs_rate_limit_on_private = false diff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rb index 2df14da6b04..6f802ec16f6 100644 --- a/lib/middleware/request_tracker.rb +++ b/lib/middleware/request_tracker.rb @@ -156,21 +156,21 @@ class Middleware::RequestTracker def rate_limit(env) if ( - GlobalSetting.max_requests_per_ip_mode == "block" || - GlobalSetting.max_requests_per_ip_mode == "warn" || - GlobalSetting.max_requests_per_ip_mode == "warn+block" + GlobalSetting.max_reqs_per_ip_mode == "block" || + GlobalSetting.max_reqs_per_ip_mode == "warn" || + GlobalSetting.max_reqs_per_ip_mode == "warn+block" ) ip = Rack::Request.new(env).ip - if !GlobalSetting.max_requests_rate_limit_on_private + if !GlobalSetting.max_reqs_rate_limit_on_private return false if is_private_ip?(ip) end limiter10 = RateLimiter.new( nil, "global_ip_limit_10_#{ip}", - GlobalSetting.max_requests_per_ip_per_10_seconds, + GlobalSetting.max_reqs_per_ip_per_10_seconds, 10, global: true ) @@ -178,7 +178,7 @@ class Middleware::RequestTracker limiter60 = RateLimiter.new( nil, "global_ip_limit_60_#{ip}", - GlobalSetting.max_requests_per_ip_per_10_seconds, + GlobalSetting.max_reqs_per_ip_per_10_seconds, 10, global: true ) @@ -190,11 +190,11 @@ class Middleware::RequestTracker limiter60.performed! rescue RateLimiter::LimitExceeded if ( - GlobalSetting.max_requests_per_ip_mode == "warn" || - GlobalSetting.max_requests_per_ip_mode == "warn+block" + GlobalSetting.max_reqs_per_ip_mode == "warn" || + GlobalSetting.max_reqs_per_ip_mode == "warn+block" ) Rails.logger.warn("Global IP rate limit exceeded for #{ip}: #{type} second rate limit, uri: #{env["REQUEST_URI"]}") - !(GlobalSetting.max_requests_per_ip_mode == "warn") + !(GlobalSetting.max_reqs_per_ip_mode == "warn") else true end diff --git a/spec/components/middleware/request_tracker_spec.rb b/spec/components/middleware/request_tracker_spec.rb index f6cf1849c76..e56c708a227 100644 --- a/spec/components/middleware/request_tracker_spec.rb +++ b/spec/components/middleware/request_tracker_spec.rb @@ -105,7 +105,7 @@ describe Middleware::RequestTracker do end it "does nothing by default" do - global_setting :max_requests_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_per_10_seconds, 1 status, _ = middleware.call(env) status, _ = middleware.call(env) @@ -114,9 +114,9 @@ describe Middleware::RequestTracker do end it "blocks private IPs if not skipped" do - global_setting :max_requests_per_ip_per_10_seconds, 1 - global_setting :max_requests_per_ip_mode, 'warn+block' - global_setting :max_requests_rate_limit_on_private, true + global_setting :max_reqs_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_mode, 'warn+block' + global_setting :max_reqs_rate_limit_on_private, true env1 = env("REMOTE_ADDR" => "127.0.0.2") @@ -128,9 +128,9 @@ describe Middleware::RequestTracker do end it "does nothing for private IPs if skipped" do - global_setting :max_requests_per_ip_per_10_seconds, 1 - global_setting :max_requests_per_ip_mode, 'warn+block' - global_setting :max_requests_rate_limit_on_private, false + global_setting :max_reqs_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_mode, 'warn+block' + global_setting :max_reqs_rate_limit_on_private, false env1 = env("REMOTE_ADDR" => "127.0.3.1") @@ -142,8 +142,8 @@ describe Middleware::RequestTracker do end it "does warn if rate limiter is enabled via warn+block" do - global_setting :max_requests_per_ip_per_10_seconds, 1 - global_setting :max_requests_per_ip_mode, 'warn+block' + global_setting :max_reqs_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_mode, 'warn+block' status, _ = middleware.call(env) status, _ = middleware.call(env) @@ -153,8 +153,8 @@ describe Middleware::RequestTracker do end it "does warn if rate limiter is enabled" do - global_setting :max_requests_per_ip_per_10_seconds, 1 - global_setting :max_requests_per_ip_mode, 'warn' + global_setting :max_reqs_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_mode, 'warn' status, _ = middleware.call(env) status, _ = middleware.call(env) @@ -164,8 +164,8 @@ describe Middleware::RequestTracker do end it "does block if rate limiter is enabled" do - global_setting :max_requests_per_ip_per_10_seconds, 1 - global_setting :max_requests_per_ip_mode, 'block' + global_setting :max_reqs_per_ip_per_10_seconds, 1 + global_setting :max_reqs_per_ip_mode, 'block' env1 = env("REMOTE_ADDR" => "1.1.1.1") env2 = env("REMOTE_ADDR" => "1.1.1.2")