8000 [RateLimiter][Security] Allow to use no lock in the rate limiter/login throttling by wouterj · Pull Request #40284 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[RateLimiter][Security] Allow to use no lock in the rate limiter/login throttling #40284

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

Merged
merged 1 commit into from
Feb 26, 2021
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 UPGRADE-5.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ Security
SecurityBundle
--------------

* [BC break] Add `login_throttling.lock_factory` setting defaulting to `null`. Set this option
to `lock.factory` if you need precise login rate limiting with synchronous requests.
* Deprecate `UserPasswordEncoderCommand` class and the corresponding `user:encode-password` command,
use `UserPasswordHashCommand` and `user:hash-password` instead
* Deprecate the `security.encoder_factory.generic` service, the `security.encoder_factory` and `Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface` aliases,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1874,7 +1874,7 @@ private function addRateLimiterSection(ArrayNodeDefinition $rootNode, callable $
->arrayPrototype()
->children()
->scalarNode('lock_factory')
->info('The service ID of the lock factory used by this limiter')
->info('The service ID of the lock factory used by this limiter (or null to disable locking)')
->defaultValue('lock.factory')
->end()
->scalarNode('cache_pool')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class FrameworkExtension extends Extension
private $httpClientConfigEnabled = false;
private $notifierConfigEnabled = false;
private $propertyAccessConfigEnabled = false;
private $lockConfigEnabled = false;
private static $lockConfigEnabled = false;

/**
* Responds to the app.config configuration parameter.
Expand Down Expand Up @@ -438,7 +438,7 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerPropertyInfoConfiguration($container, $loader);
}

if ($this->lockConfigEnabled = $this->isConfigEnabled($container, $config['lock'])) {
if (self::$lockConfigEnabled = $this->isConfigEnabled($container, $config['lock'])) {
$this->registerLockConfiguration($config['lock'], $container, $loader);
}

Expand Down Expand Up @@ -2344,10 +2344,6 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $

private function registerRateLimiterConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)
{
if (!$this->lockConfigEnabled) {
throw new LogicException('Rate limiter support cannot be enabled without enabling the Lock component.');
}

$loader->load('rate_limiter.php');

foreach ($config['limiters'] as $name => $limiterConfig) {
Expand All @@ -2362,7 +2358,13 @@ public static function registerRateLimiter(ContainerBuilder $container, string $

$limiter = $container->setDefinition($limiterId = 'limiter.'.$name, new ChildDefinition('limiter'));

$limiter->addArgument(new Reference($limiterConfig['lock_factory']));
if (null !== $limiterConfig['lock_factory']) {
if (!self::$lockConfigEnabled) {
throw new LogicException(sprintf('Rate limiter "%s" requires the Lock component to be installed and configured.', $name));
}

$limiter->replaceArgument(2, new Reference($limiterConfig['lock_factory']));
}
unset($limiterConfig['lock_factory']);

$storageId = $limiterConfig['storage_service'] ?? null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
->args([
abstract_arg('config'),
abstract_arg('storage'),
null,
])
;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\OutOfBoundsException;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\Workflow\Exception\InvalidDefinitionException;

Expand Down Expand Up @@ -82,4 +84,49 @@ public function testWorkflowValidationStateMachine()
]);
});
}

public function testRateLimiterWithLockFactory()
{
try {
$this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'rate_limiter' => [
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
],
]);
});

$this->fail('No LogicException thrown');
} catch (LogicException $e) {
$this->assertEquals('Rate limiter "with_lock" requires the Lock component to be installed and configured.', $e->getMessage());
}

$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'lock' => true,
'rate_limiter' => [
'with_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour'],
],
]);
});

$withLock = $container->getDefinition('limiter.with_lock');
$this->assertEquals('lock.factory', (string) $withLock->getArgument(2));
}

public function testRateLimiterLockFactory()
{
$container = $this->createContainerFromClosure(function (ContainerBuilder $container) {
$container->loadFromExtension('framework', [
'rate_limiter' => [
'without_lock' => ['policy' => 'fixed_window', 'limit' => 10, 'interval' => '1 hour', 'lock_factory' => null],
],
]);
});

$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage('The argument "2" doesn\'t exist.');

$container->getDefinition('limiter.without_lock')->getArgument(2);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"symfony/messenger": "^5.2",
"symfony/mime": "^4.4|^5.0",
"symfony/process": "^4.4|^5.0",
"symfony/rate-limiter": "^5.2",
"symfony/security-bundle": "^5.3",
"symfony/serializer": "^5.2",
"symfony/stopwatch": "^4.4|^5.0",
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/SecurityBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
5.3
---

* [BC break] Add `login_throttling.lock_factory` setting defaulting to `null` (instead of `lock.factory`)
* Add the `debug:firewall` command.
* Deprecate `UserPasswordEncoderCommand` class and the corresponding `user:encode-password` command,
use `UserPasswordHashCommand` and `user:hash-password` instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function addConfiguration(NodeDefinition $builder)
->children()
->scalarNode('limiter')->info(sprintf('A service id implementing "%s".', RequestRateLimiterInterface::class))->end()
->integerNode('max_attempts')->defaultValue(5)->end()
->scalarNode('lock_factory')->info('The service ID of the lock factory used by the login rate limiter (or null to disable locking)')->defaultNull()->end()
->end();
}

Expand All @@ -76,6 +77,7 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
'policy' => 'fixed_window',
'limit' => $config['max_attempts'],
'interval' => '1 minute',
'lock_factory' => $config['lock_factory'],
];
FrameworkExtension::registerRateLimiter($container, $localId = '_login_local_'.$firewallName, $limiterOptions);

Expand Down
0