TYPO3 v15 dev-main snapshot ()
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\RateLimiter;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\RateLimiter\LimiterInterface;
|
||||
use Symfony\Component\RateLimiter\RateLimiterFactory as SymfonyRateLimiterFactory;
|
||||
use Symfony\Component\RateLimiter\Storage\InMemoryStorage;
|
||||
use TYPO3\CMS\Core\Http\NormalizedParams;
|
||||
use TYPO3\CMS\Core\RateLimiter\Storage\CachingFrameworkStorage;
|
||||
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
||||
|
||||
#[Autoconfigure(public: true, shared: false)]
|
||||
readonly class RateLimiterFactory implements RateLimiterFactoryInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected CachingFrameworkStorage $storage,
|
||||
protected array $config = [],
|
||||
) {}
|
||||
|
||||
public function create(?string $key = null): LimiterInterface
|
||||
{
|
||||
if ($this->config === []) {
|
||||
throw new \LogicException(
|
||||
'Cannot call create() on a RateLimiterFactory without configuration. '
|
||||
. 'Use a pre-configured named service or call createLimiter() with an explicit config array.',
|
||||
1740000001
|
||||
);
|
||||
}
|
||||
|
||||
$config = $this->applyConfigOverrides($this->config);
|
||||
$factory = new SymfonyRateLimiterFactory($config, $this->storage);
|
||||
return $factory->create($key);
|
||||
}
|
||||
|
||||
public function createLimiter(array $config, ?string $key = null): LimiterInterface
|
||||
{
|
||||
$config = $this->applyConfigOverrides($config);
|
||||
$factory = new SymfonyRateLimiterFactory($config, $this->storage);
|
||||
return $factory->create($key);
|
||||
}
|
||||
|
||||
public function createRequestBasedLimiter(ServerRequestInterface $request, array $configuration): LimiterInterface
|
||||
{
|
||||
$normalizedParams = $request->getAttribute('normalizedParams') ?? NormalizedParams::createFromRequest($request);
|
||||
$remoteIp = $normalizedParams->getRemoteAddress();
|
||||
return $this->createLimiter($configuration, $remoteIp);
|
||||
}
|
||||
|
||||
public function createLoginRateLimiter(ServerRequestInterface $request, string $loginType): LimiterInterface
|
||||
{
|
||||
$normalizedParams = $request->getAttribute('normalizedParams') ?? NormalizedParams::createFromRequest($request);
|
||||
$remoteIp = $normalizedParams->getRemoteAddress();
|
||||
$limiterId = 'login-' . strtolower($loginType);
|
||||
$limit = (int)($GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimit'] ?? 5);
|
||||
$interval = $GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimitInterval'] ?? '15 minutes';
|
||||
|
||||
$enabled = !$this->isIpExcluded($loginType, $remoteIp) && $limit > 0;
|
||||
|
||||
if (!$enabled) {
|
||||
$config = [
|
||||
'id' => $limiterId,
|
||||
'policy' => 'no_limit',
|
||||
'limit' => $limit,
|
||||
'interval' => $interval,
|
||||
];
|
||||
$factory = new SymfonyRateLimiterFactory($config, new InMemoryStorage());
|
||||
return $factory->create($remoteIp);
|
||||
}
|
||||
|
||||
return $this->createLimiter(
|
||||
[
|
||||
'id' => $limiterId,
|
||||
'policy' => 'sliding_window',
|
||||
'limit' => $limit,
|
||||
'interval' => $interval,
|
||||
],
|
||||
$remoteIp
|
||||
);
|
||||
}
|
||||
|
||||
protected function applyConfigOverrides(array $config): array
|
||||
{
|
||||
$overrides = $GLOBALS['TYPO3_CONF_VARS']['SYS']['rateLimiter'][$config['id']] ?? [];
|
||||
if ($overrides !== []) {
|
||||
$config = array_replace($config, $overrides);
|
||||
}
|
||||
return $config;
|
||||
}
|
||||
|
||||
protected function isIpExcluded(string $loginType, string $remoteAddress): bool
|
||||
{
|
||||
$ipMask = trim($GLOBALS['TYPO3_CONF_VARS'][$loginType]['loginRateLimitIpExcludeList'] ?? '');
|
||||
return GeneralUtility::cmpIP($remoteAddress, $ipMask);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\RateLimiter;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Symfony\Component\RateLimiter\LimiterInterface;
|
||||
|
||||
/**
|
||||
* TYPO3's own rate limiter factory interface extending Symfony's RateLimiterFactoryInterface
|
||||
* with additional convenience methods for request-based and login rate limiting.
|
||||
*/
|
||||
interface RateLimiterFactoryInterface extends \Symfony\Component\RateLimiter\RateLimiterFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Create a limiter with custom configuration.
|
||||
*/
|
||||
public function createLimiter(array $config, ?string $key = null): LimiterInterface;
|
||||
|
||||
/**
|
||||
* Create a limiter based on the request input.
|
||||
*/
|
||||
public function createRequestBasedLimiter(ServerRequestInterface $request, array $configuration): LimiterInterface;
|
||||
|
||||
/**
|
||||
* Create a limiter for user login.
|
||||
*/
|
||||
public function createLoginRateLimiter(ServerRequestInterface $request, string $loginType): LimiterInterface;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\RateLimiter;
|
||||
|
||||
use TYPO3\CMS\Core\Error\Http\AbstractClientErrorException;
|
||||
|
||||
/**
|
||||
* Exception thrown when a rate limiter has disallowed further processing
|
||||
*/
|
||||
class RequestRateLimitedException extends AbstractClientErrorException {}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/*
|
||||
* This file is part of the TYPO3 CMS project.
|
||||
*
|
||||
* It is free software; you can redistribute it and/or modify it under
|
||||
* the terms of the GNU General Public License, either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* For the full copyright and license information, please read the
|
||||
* LICENSE.txt file that was distributed with this source code.
|
||||
*
|
||||
* The TYPO3 project - inspiring people to share!
|
||||
*/
|
||||
|
||||
namespace TYPO3\CMS\Core\RateLimiter\Storage;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
|
||||
use Symfony\Component\RateLimiter\LimiterStateInterface;
|
||||
use Symfony\Component\RateLimiter\Policy\SlidingWindow;
|
||||
use Symfony\Component\RateLimiter\Policy\TokenBucket;
|
||||
use Symfony\Component\RateLimiter\Policy\Window;
|
||||
use Symfony\Component\RateLimiter\Storage\StorageInterface;
|
||||
use TYPO3\CMS\Core\Cache\CacheManager;
|
||||
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
|
||||
|
||||
/**
|
||||
* A rate limiter storage utilizing TYPO3's Caching Framework.
|
||||
*
|
||||
* @internal This is not part of the official TYPO3 Core API due to a limitation of the Symfony Rate Limiter API.
|
||||
*/
|
||||
#[Autoconfigure(public: true)]
|
||||
class CachingFrameworkStorage implements StorageInterface
|
||||
{
|
||||
private FrontendInterface $cacheInstance;
|
||||
|
||||
public function __construct(CacheManager $cacheInstance)
|
||||
{
|
||||
$this->cacheInstance = $cacheInstance->getCache('ratelimiter');
|
||||
$this->cacheInstance->collectGarbage();
|
||||
}
|
||||
|
||||
public function save(LimiterStateInterface $limiterState): void
|
||||
{
|
||||
$this->cacheInstance->set(
|
||||
sha1($limiterState->getId()),
|
||||
serialize($limiterState),
|
||||
[],
|
||||
$limiterState->getExpirationTime()
|
||||
);
|
||||
}
|
||||
|
||||
public function fetch(string $limiterStateId): ?LimiterStateInterface
|
||||
{
|
||||
$cacheItem = $this->cacheInstance->get(sha1($limiterStateId));
|
||||
if ($cacheItem) {
|
||||
$value = unserialize($cacheItem, ['allowed_classes' => [Window::class, SlidingWindow::class, TokenBucket::class]]);
|
||||
if ($value instanceof LimiterStateInterface) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function delete(string $limiterStateId): void
|
||||
{
|
||||
$this->cacheInstance->remove(sha1($limiterStateId));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user