A platform for community discussion. Free, open, simple.
FIX: don't block api requests when whitelisted_crawler_user_agents is set
Neil Lalondeb87a089822bd
diff --git a/lib/middleware/anonymous_cache.rb b/lib/middleware/anonymous_cache.rbindex f2539b06106..2cd57198767 100644--- a/lib/middleware/anonymous_cache.rb+++ b/lib/middleware/anonymous_cache.rb@@ -26,6 +26,8 @@ module Middleware[email protected]_with?('robots.txt') &&[email protected]_with?('srv/status') &&+ @request[Auth::DefaultCurrentUserProvider::API_KEY].nil? &&+ @env[Auth::DefaultCurrentUserProvider::USER_API_KEY].nil? &&CrawlerDetection.is_blocked_crawler?(@request.env['HTTP_USER_AGENT'])enddiff --git a/spec/components/middleware/anonymous_cache_spec.rb b/spec/components/middleware/anonymous_cache_spec.rbindex 4d63d4f5e45..de52d9a205f 100644--- a/spec/components/middleware/anonymous_cache_spec.rb+++ b/spec/components/middleware/anonymous_cache_spec.rb@@ -189,6 +189,16 @@ describe Middleware::AnonymousCache::Helper doexpect(@status).to eq(200)end+ it "doesn't block api requests" do+ SiteSetting.whitelisted_crawler_user_agents = 'Googlebot'+ api_key = Fabricate(:api_key)++ get "/latest?api_key=#{api_key.key}&api_username=system", headers: {+ "QUERY_STRING" => "api_key=#{api_key.key}&api_username=system"+ }+ expect(@status).to eq(200)+ end+it "applies blacklisted_crawler_user_agents correctly" doSiteSetting.blacklisted_crawler_user_agents = 'Googlebot'