Skip to content

Commit 9518c57

Browse files
committed
[B] Update rate limiting to use remote IP instead of request IP
Addresses an issue where requests routed through reverse proxies / load balancers were being aggressively throttled due to obscured client IP addresses
1 parent 5067608 commit 9518c57

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

api/config/application.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class Application < Rails::Application
8383
# Skip views, helpers and assets when generating a new resource.
8484
config.api_only = true
8585

86+
config.middleware.use ActionDispatch::RemoteIp
8687
config.middleware.use Rack::MethodOverride
8788
config.middleware.use ActionDispatch::Flash
8889
config.middleware.use ActionDispatch::Cookies

api/config/initializers/rack_attack.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
end
3939

4040
Rack::Attack.throttle throttler.ip_key, **throttler.options do |request|
41-
request.ip if request.env["manifold_env.throttled_category"] == throttler.category
41+
next unless if request.env["manifold_env.throttled_category"] == throttler.category
42+
request.env["action_dispatch.remote_ip"]&.calculate_ip || request.ip
4243
end
4344
end
4445

@@ -56,13 +57,13 @@
5657

5758
Rack::Attack.blocklisted_responder = lambda do |request|
5859
# :nocov:
59-
[503, {}, ["Internal Server Error\n"]]
60+
[429, {}, ["Rate Limit Exceeded\n"]]
6061
# :nocov:
6162
end
6263

6364
Rack::Attack.throttled_responder = lambda do |request|
6465
# :nocov:
65-
[503, {}, ["Internal Server Error\n"]]
66+
[429, {}, ["Rate Limit Exceeded\n"]]
6667
# :nocov:
6768
end
6869
end

0 commit comments

Comments
 (0)