|
| 1 | +# Some explanation at https://sciencehistory.atlassian.net/wiki/spaces/HDC/pages/2645098498/Cloudflare+Turnstile+bot+detection |
| 2 | +Rails.application.config.to_prepare do |
| 3 | + config = BotChallengePage::BotChallengePageController.bot_challenge_config |
| 4 | + |
| 5 | + # allow rate_limit_count requests in rate_limit_period, before issuing challenge |
| 6 | + config.rate_limit_period = 12.hour |
| 7 | + config.rate_limit_count = 2 # seriously reduced to see if that helps |
| 8 | + |
| 9 | + # How long a challenge pass is good for |
| 10 | + config.session_passed_good_for = 24.hours |
| 11 | + |
| 12 | + config.enabled = ScihistDigicoll::Env.lookup(:cf_turnstile_enabled) |
| 13 | + config.cf_turnstile_sitekey = ScihistDigicoll::Env.lookup(:cf_turnstile_sitekey) |
| 14 | + config.cf_turnstile_secret_key = ScihistDigicoll::Env.lookup(:cf_turnstile_secret_key) |
| 15 | + |
| 16 | + # any custom collection controllers or other controllers that offer search have to be listed here |
| 17 | + # to rate-limit them! |
| 18 | + config.rate_limited_locations = [ |
| 19 | + '/catalog', |
| 20 | + '/focus', |
| 21 | + # we want to omit `/collections` list page, so we do these by controller |
| 22 | + { controller: "collection_show" }, |
| 23 | + { controller: "collection_show_controllers/immigrants_and_innovation_collection" }, |
| 24 | + { controller: "collection_show_controllers/oral_history_collection"}, |
| 25 | + { controller: "collection_show_controllers/bredig_collection"} |
| 26 | + ] |
| 27 | + |
| 28 | + config.allow_exempt = ->(controller, config) { |
| 29 | + # Excempt any Catalog #facet action that looks like an ajax/fetch request, the redirect |
| 30 | + # ain't gonna work there, we just exempt it. |
| 31 | + # |
| 32 | + # sec-fetch-dest is set to 'empty' by browser on fetch requests, to limit us further; |
| 33 | + # sure an attacker could fake it, we don't mind if someone determined can avoid rate-limiting on this one action |
| 34 | + ( controller.params[:action] == "facet" && |
| 35 | + controller.request.headers["sec-fetch-dest"] == "empty" && |
| 36 | + controller.kind_of?(CatalogController) |
| 37 | + ) || |
| 38 | + # Exempt honeybadger token from uptime checker |
| 39 | + # https://docs.honeybadger.io/guides/security/ |
| 40 | + ( |
| 41 | + ENV['HONEYBADGER_TOKEN'].present? && |
| 42 | + controller.request.headers['Honeybadger-Token'] == ENV['HONEYBADGER_TOKEN'] |
| 43 | + ) || |
| 44 | + # Exempt a collection controller (or sub-class!) with _no query params_, we want to |
| 45 | + # let Google and other bots into colleciton home pages, even though they show search results. |
| 46 | + ( |
| 47 | + controller.kind_of?(CollectionShowController) && |
| 48 | + controller.respond_to?(:has_search_parameters?) && |
| 49 | + !controller.has_search_parameters? |
| 50 | + ) || |
| 51 | + ## exempt PDF original downloads, which are protected with an 'immediate' filter |
| 52 | + ( |
| 53 | + controller.kind_of?(DownloadsController) && |
| 54 | + controller.params[:file_category] == "pdf" |
| 55 | + ) |
| 56 | + } |
| 57 | + |
| 58 | + BotChallengePage::BotChallengePageController.rack_attack_init |
| 59 | +end |
0 commit comments