diff --git a/Model/Config.php b/Model/Config.php index 595484ff..ebda91a4 100644 --- a/Model/Config.php +++ b/Model/Config.php @@ -526,6 +526,8 @@ class Config extends \Magento\PageCache\Model\Config const XML_FASTLY_EXEMPT_GOOD_BOTS = 'system/full_page_cache/fastly/fastly_rate_limiting_settings/crawler_protection/exempt_good_bots'; + const XML_FASTLY_EXCLUDED_COUNTRIES + = 'system/full_page_cache/fastly/fastly_rate_limiting_settings/crawler_protection/excluded_countries'; /** * Request Header for VCL comparison */ @@ -1133,7 +1135,27 @@ public function isExemptGoodBotsEnabled() { return $this->_scopeConfig->getValue(self::XML_FASTLY_EXEMPT_GOOD_BOTS); } - + + /** + * Get the list of countries to be excluded from the rate limiting + * + * @return array + */ + public function getExcludedCountries() + { + $excludedCountries = $this->_scopeConfig->getValue(self::XML_FASTLY_EXCLUDED_COUNTRIES); + if (!empty($excludedCountries)) { + try { + $excludedCountries = json_decode($excludedCountries, true); + } catch (\Exception $e) { + $excludedCountries = []; // Return empty array on failure + } + } else { + $excludedCountries = []; + } + return $excludedCountries; + } + /** * Get store ID for country. * diff --git a/Model/FrontControllerPlugin.php b/Model/FrontControllerPlugin.php index ae3f1aba..57daf954 100644 --- a/Model/FrontControllerPlugin.php +++ b/Model/FrontControllerPlugin.php @@ -84,6 +84,11 @@ class FrontControllerPlugin */ private $logger; + /** + * @var ExcludeCountries + */ + private $excludedCountries; + /** * FrontControllerPlugin constructor. * @param Request $request @@ -113,6 +118,7 @@ public function __construct( $this->coreDate = $coreDate; $this->filesystem = $filesystem; $this->logger = $logger; + $this->excludedCountries = $this->config->getExcludedCountries(); } /** @@ -156,6 +162,13 @@ private function sensitivePathProtection($path) { $ip = $this->request->getServerValue('HTTP_FASTLY_CLIENT_IP') ?? $this->request->getClientIp(); + $countryCode = $this->request->getServerValue('HTTP_CLIENT_GEO_COUNTRY'); + + if (in_array($countryCode, $this->excludedCountries)) { + $this->log('Request from ' . $countryCode . ' bypassed crawler protection for IP: ' . $ip); + return false; + } + if ($this->readMaintenanceIp($ip)) { return false; } @@ -194,6 +207,13 @@ private function crawlerProtection($path) { $ip = $this->request->getServerValue('HTTP_FASTLY_CLIENT_IP') ?? $this->request->getClientIp(); + $countryCode = $this->request->getServerValue('HTTP_CLIENT_GEO_COUNTRY'); + + if (in_array($countryCode, $this->excludedCountries)) { + $this->log('Request from ' . $countryCode . ' bypassed crawler protection for IP: ' . $ip); + return false; + } + if ($this->config->isExemptGoodBotsEnabled()) { if ($this->verifyBots($ip)) { return false; diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 4cf09340..4c37a182 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -780,6 +780,15 @@ 1 + + + + Select countries to exclude from rate limiting. + Magento\Config\Model\Config\Source\Locale\Country + Magento\Config\Model\Config\Backend\Serialized\ArraySerialized +