-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b07aa92
commit 4545658
Showing
2 changed files
with
20 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
class Rack::Attack | ||
throttle('limit_form_submissions', limit: 6, period: 1.minute) do |req| | ||
if req.post? && route_matches?(req, :submit_form) | ||
throttle('limit_form_submissions_per_minute', limit: 6, period: 1.minute) do |req| | ||
if req.post? && (route_matches?(req, :touchpoint_submissions) || route_matches?(req, :form_submissions)) | ||
req.ip # Throttle based on the request's IP address | ||
end | ||
end | ||
|
||
# Helper method to check if the route matches the named route | ||
# Check if the route matches the named route | ||
def self.route_matches?(req, named_route) | ||
begin | ||
recognized_route = Rails.application.routes.recognize_path(req.path, method: req.request_method) | ||
recognized_route[:controller] == 'forms' && recognized_route[:action] == 'submit' | ||
recognized_route[:controller] == 'submissions' && | ||
recognized_route[:action] == 'create' && | ||
recognized_route[:touchpoint_id].present? && | ||
recognized_route[:format] == 'json' | ||
rescue ActionController::RoutingError | ||
false | ||
end | ||
end | ||
|
||
# Custom response for throttled requests | ||
self.throttled_response = lambda do |env| | ||
self.throttled_responder = lambda do |env| | ||
[429, { 'Content-Type' => 'text/plain' }, ["Too many requests. Please try again later."]] | ||
end | ||
end |