Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable turnstile globally, or if redis is selected for storage #4164

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading