Skip to content

Commit 1cf6b55

Browse files
authored
Merge pull request #1564 from sul-dlss/upgrade_bot_challenge
2 parents 8c17a4a + 7928679 commit 1cf6b55

File tree

5 files changed

+19
-21
lines changed

5 files changed

+19
-21
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ gem "stimulus-rails", "~> 1.3"
8888
gem "turbo-rails", "~> 2.0"
8989
gem 'jwt'
9090
gem "rack-cors", "~> 2.0"
91-
gem "bot_challenge_page", "~> 0.4.0"
91+
gem "bot_challenge_page", "~> 1.0"
9292
gem "racecar", "~> 2.12"
9393
gem "cocina_display", "~> 1.1"

Gemfile.lock

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,9 @@ GEM
110110
rails
111111
bootsnap (1.19.0)
112112
msgpack (~> 1.2)
113-
bot_challenge_page (0.4.0)
113+
bot_challenge_page (1.0.0)
114114
http (~> 5.2)
115-
rack-attack (~> 6.7)
116-
rails (>= 7.1, < 8.1)
115+
rails (>= 7.1, < 8.2)
117116
builder (3.3.0)
118117
bundler-audit (0.9.2)
119118
bundler (>= 1.2.0, < 3)
@@ -420,8 +419,6 @@ GEM
420419
king_konf (~> 1.0.0)
421420
rdkafka (>= 0.15.0)
422421
rack (3.2.4)
423-
rack-attack (6.8.0)
424-
rack (>= 1.0, < 4)
425422
rack-cors (2.0.2)
426423
rack (>= 2.0.0)
427424
rack-mini-profiler (2.3.4)
@@ -699,7 +696,7 @@ DEPENDENCIES
699696
blacklight (~> 8.6)
700697
blacklight_dynamic_sitemap (~> 0.3)
701698
bootsnap (>= 1.1.0)
702-
bot_challenge_page (~> 0.4.0)
699+
bot_challenge_page (~> 1.0)
703700
capistrano
704701
capistrano-bundler
705702
capistrano-passenger

app/controllers/application_controller.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
class ApplicationController < ActionController::Base
2+
include BotChallengePage::Controller
3+
24
# See config/initializers/bot_challenge_page.rb to control this behavior
3-
before_action do |controller|
4-
BotChallengePage::BotChallengePageController.bot_challenge_enforce_filter(controller)
5-
end
5+
bot_challenge
66

77
# Adds a few additional behaviors into the application controller
88
include Blacklight::Controller

app/controllers/catalog_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ class CatalogController < ApplicationController
99
# We protect requests for searches, but not for show pages, so we can still
1010
# crawl ourselves and let well-behaved search engines index our content via
1111
# the sitemap.
12-
before_action only: :index do |controller|
13-
BotChallengePage::BotChallengePageController.bot_challenge_enforce_filter(controller, immediate: true)
12+
#
13+
bot_challenge only: :index
1414

15+
before_action only: :index do
1516
# Additional fields needed for Bento
1617
if request.format.json?
1718
blacklight_config.add_index_field Settings.FIELDS.RESOURCE_CLASS, helper_method: :get_specific_field_type

config/initializers/bot_challenge_page.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
# rubocop:disable Layout/LineLength
2-
Rails.application.config.to_prepare do
2+
BotChallengePage.configure do |config|
33
# If disabled, no challenges will be issued
4-
BotChallengePage::BotChallengePageController.bot_challenge_config.enabled = Settings.turnstile.enabled
4+
config.enabled = Settings.turnstile.enabled
55

66
# Get from CloudFlare Turnstile: https://www.cloudflare.com/application-services/products/turnstile/
77
# Some testing keys are also available: https://developers.cloudflare.com/turnstile/troubleshooting/testing/
88
#
99
# This set of keys will always pass the challenge; the link above includes
1010
# sets that will always challenge or always fail, which is useful for local testing
11-
BotChallengePage::BotChallengePageController.bot_challenge_config.cf_turnstile_sitekey = Settings.turnstile.site_key
12-
BotChallengePage::BotChallengePageController.bot_challenge_config.cf_turnstile_secret_key = Settings.turnstile.secret_key
11+
config.cf_turnstile_sitekey = Settings.turnstile.site_key
12+
config.cf_turnstile_secret_key = Settings.turnstile.secret_key
1313

1414
# Do the challenge "in place" on the page the user was on
15-
BotChallengePage::BotChallengePageController.bot_challenge_config.redirect_for_challenge = false
15+
config.redirect_for_challenge = false
1616

1717
# How long will a challenge success exempt a session from further challenges?
18-
# BotChallengePage::BotChallengePageController.bot_challenge_config.session_passed_good_for = 36.hours
18+
# config.session_passed_good_for = 36.hours
1919

2020
# Exempt async JS facet requests from the challenge. Someone really determined could fake
2121
# this header, but until we see that behavior, we'll allow it so the facet UI works.
2222
# We also have an exception for index json so that the mini-bento frontend fetch in Searchworks doesn't get blocked.
2323
# Also exempt any IPs contained in the CIDR blocks in Settings.turnstile.safelist.
24-
BotChallengePage::BotChallengePageController.bot_challenge_config.allow_exempt = lambda do |controller, _config|
25-
(controller.is_a?(CatalogController) && controller.params[:action].in?(%w[facet index]) && controller.request.format.json? && controller.request.headers['sec-fetch-dest'] == 'empty') ||
26-
Settings.turnstile.safelist.map { |cidr| IPAddr.new(cidr) }.any? { |range| controller.request.remote_ip.in?(range) }
24+
config.skip_when = lambda do |_config|
25+
(is_a?(CatalogController) && params[:action].in?(%w[facet index]) && request.format.json? && request.headers['sec-fetch-dest'] == 'empty') ||
26+
Settings.turnstile.safelist.map { |cidr| IPAddr.new(cidr) }.any? { |range| request.remote_ip.in?(range) }
2727
end
2828

2929
# More configuration is available; see:

0 commit comments

Comments
 (0)