Skip to content

Commit

Permalink
fix: handle nil match_data in throttling (#2179)
Browse files Browse the repository at this point in the history
  • Loading branch information
rabbitz committed Sep 6, 2024
1 parent ca8faea commit 4098e91
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ class Rack::Attack
self.throttled_responder =
lambda do |env|
match_data = env["rack.attack.match_data"]
now = match_data[:epoch_time]

headers = {
"RateLimit-Limit" => match_data[:limit].to_s,
"RateLimit-Remaining" => "0",
"RateLimit-Reset" => (now + (match_data[:period] - now % match_data[:period])).to_s,
}

[429, headers, ["Throttled\n"]]
if match_data
now = match_data[:epoch_time]
headers = {
"RateLimit-Limit" => match_data[:limit].to_s,
"RateLimit-Remaining" => "0",
"RateLimit-Reset" => (now + (match_data[:period] - now % match_data[:period])).to_s,
}
[429, headers, ["Throttled\n"]]
else
[429, {}, ["Throttled but no match data\n"]]
end
end
end

0 comments on commit 4098e91

Please sign in to comment.