Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow disabling search throttling via config or envvar #1738

Merged
merged 1 commit into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/initializers/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@

# Parse numeric values as integers instead of strings.
#
# config.env_parse_values = true
config.env_parse_values = true
end
43 changes: 23 additions & 20 deletions config/initializers/rack-attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,36 @@
req.ip if req.post? && req.path.start_with?("/users/sign_in")
end

Rack::Attack.throttle("searches/ip", limit: 15, period: 15.minutes) do |req|
# don't throttle requests with a q, because it's more likely to be a real user
next if req.params['q'].present?
# throttle bot-like search queries if configured
if Settings.throttle_searches
Rack::Attack.throttle("searches/ip", limit: 15, period: 15.minutes) do |req|
# don't throttle requests with a q, because it's more likely to be a real user
next if req.params['q'].present?

# only throttle search queries
next unless req.path.ends_with?("/catalog")
# only throttle search queries
next unless req.path.ends_with?("/catalog")

# don't throttle the first couple pages of a search result
next if req.params['page'].present? && req.params['page'].to_i < 10
# don't throttle the first couple pages of a search result
next if req.params['page'].present? && req.params['page'].to_i < 10

req.ip
end
req.ip
end

Rack::Attack.throttled_response_retry_after_header = true
Rack::Attack.throttled_response_retry_after_header = true

Rack::Attack.throttled_responder = lambda do |request|
match_data = request.env['rack.attack.match_data']
now = match_data[:epoch_time]
Rack::Attack.throttled_responder = lambda do |request|
match_data = request.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
}
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"]]
[ 429, headers, ["Throttled\n"]]
end
end

# Always allow Stanford traffic
# always allow Stanford traffic
Rack::Attack.safelist_ip("171.64.0.0/14")
1 change: 1 addition & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ contact:
data_dir: tmp/data

allow_robots: false
throttle_searches: false

feature_flags:
allow_json_upload: true
Expand Down