Skip to content

Commit

Permalink
Disable turnstile globally, or if redis is selected for storage
Browse files Browse the repository at this point in the history
  • Loading branch information
maccabeelevine committed Dec 16, 2024
1 parent e300edc commit 4a4da10
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config/vufind/RateLimiter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ Policies:
# - storage settings for the result cache
# And see the required config in contentsecuritypolicy.ini if CSP is used.
#Turnstile:
#enabled: false

# These two keys are required. See also values they can be set to for testing purposes:
# https://developers.cloudflare.com/turnstile/troubleshooting/testing/
#siteKey: 0x1234567890
Expand Down
23 changes: 20 additions & 3 deletions module/VuFind/src/VuFind/RateLimiter/RateLimiterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class RateLimiterManager implements LoggerAwareInterface, TranslatorAwareInterfa
use LoggerAwareTrait;
use TranslatorAwareTrait;

/**
* Turnstile service
*
* @var Turnstile
*/
protected $turnstile = null;

/**
* Current event description for logging
*
Expand All @@ -77,15 +84,13 @@ class RateLimiterManager implements LoggerAwareInterface, TranslatorAwareInterfa
* @param string $clientIp Client's IP address
* @param ?int $userId User ID or null if not logged in
* @param Closure $rateLimiterFactoryCallback Rate limiter factory callback
* @param Turnstile $turnstile Turnstile service
* @param IpAddressUtils $ipUtils IP address utilities
*/
public function __construct(
protected array $config,
protected string $clientIp,
protected ?int $userId,
protected Closure $rateLimiterFactoryCallback,
protected Turnstile $turnstile,
protected IpAddressUtils $ipUtils
) {
$this->clientLogDetails = "ip:$clientIp";
Expand All @@ -94,6 +99,18 @@ public function __construct(
}
}

/**
* Set the turnstile service instance.
*
* @param Turnstile $turnstile Turnstile service
*
* @return void
*/
public function setTurnstile(Turnstile $turnstile)
{
$this->turnstile = $turnstile;
}

/**
* Check if rate limiter is enabled
*
Expand Down Expand Up @@ -152,7 +169,7 @@ public function check(EventInterface $event): array
if (
$limit->isAccepted() &&
($this->config['Policies'][$policyId]['turnstileRateLimiterSettings'] ?? false) &&
$this->turnstile->isChallengeAllowed($event)
$this->turnstile?->isChallengeAllowed($event)
) {
$turnstileLimiter = ($this->rateLimiterFactoryCallback)(
$this->config,
Expand Down
13 changes: 11 additions & 2 deletions module/VuFind/src/VuFind/RateLimiter/RateLimiterManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,23 @@ public function __invoke(
$authManager = $container->get(\VuFind\Auth\Manager::class);
$request = $container->get('Request');

return new $requestedName(
$rateLimiterManager = new $requestedName(
$config,
$request->getServer('REMOTE_ADDR'),
$authManager->getUserObject()?->getId(),
Closure::fromCallable([$this, 'getRateLimiter']),
$container->get(\VuFind\RateLimiter\Turnstile\Turnstile::class),
$container->get(\VuFind\Net\IpAddressUtils::class)
);

if (
($config['Turnstile']['enabled'] ?? false)
&& (strtolower($config['Storage']['adapter']) != 'redis')
) {
$turnstile = $container->get(\VuFind\RateLimiter\Turnstile\Turnstile::class);
$rateLimiterManager->setTurnstile($turnstile);
}

return $rateLimiterManager;
}

/**
Expand Down

0 comments on commit 4a4da10

Please sign in to comment.