Skip to content

Commit 8b19b3e

Browse files
committed
fixes request throttling policies that are screwing with api
1 parent a4c3e6e commit 8b19b3e

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

config/initializers/rack_attack.rb

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ class Rack::Attack
1010

1111
Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new
1212

13-
throttle('nonexistent', limit: 5, period: 1.minute) do |req|
14-
# Normalize request path
15-
path = req.path.squeeze('/')
16-
17-
# Try recognizing the path with Rails' router
18-
begin
19-
Rails.application.routes.recognize_path(path, method: req.request_method)
20-
false # If it matches a valid route, do NOT throttle
21-
rescue ActionController::RoutingError
22-
true # If route doesn't exist, apply throttling
23-
end
24-
end
25-
2613
blocklist('block exploit paths') do |req|
2714
exploit_paths = [
2815
%r{^/wp-admin},
@@ -49,10 +36,27 @@ class Rack::Attack
4936
# Throttle all requests by IP (60rpm)
5037
#
5138
# Key: "rack::attack:#{Time.now.to_i/:period}:req/ip:#{req.ip}"
52-
EXEMPT_PATHS = ['/assets', '/api/v1/points', '/api/v1/cases'].freeze
39+
EXEMPT_PREFIXES = [
40+
'/assets',
41+
'/api/v1/points',
42+
'/api/v1/cases',
43+
'/points',
44+
'/cases',
45+
'/documents',
46+
'/services',
47+
].freeze
5348

5449
throttle('req/ip', limit: 50, period: 1.minute) do |req|
55-
req.ip unless EXEMPT_PATHS.any? { |path| req.path.start_with?(path) }
50+
Rails.logger.warn "DEBUG: req.path=#{req.path.inspect} method=#{req.request_method}"
51+
52+
unless EXEMPT_PREFIXES.any? { |prefix| req.path.start_with?(prefix) }
53+
req.ip
54+
end
55+
end
56+
57+
ActiveSupport::Notifications.subscribe("rack.attack") do |name, start, finish, request_id, payload|
58+
req = payload[:request]
59+
Rails.logger.warn "RACK::ATTACK blocked: #{req.request_method} #{req.path} from #{req.ip}"
5660
end
5761

5862
# Throttle POST requests to */services by IP address

0 commit comments

Comments
 (0)