-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] Added login throttling feature #38204
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/LoginThrottlingFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory; | ||
|
||
use Symfony\Bundle\FrameworkBundle\DependencyInjection\FrameworkExtension; | ||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; | ||
use Symfony\Component\Config\Definition\Builder\NodeDefinition; | ||
use Symfony\Component\DependencyInjection\ChildDefinition; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\Security\Http\EventListener\LoginThrottlingListener; | ||
|
||
/** | ||
* @author Wouter de Jong <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
class LoginThrottlingFactory implements AuthenticatorFactoryInterface, SecurityFactoryInterface | ||
{ | ||
public function create(ContainerBuilder $container, string $id, array $config, string $userProvider, ?string $defaultEntryPoint) | ||
{ | ||
throw new \LogicException('Login throttling is not supported when "security.enable_authenticator_manager" is not set to true.'); | ||
} | ||
|
||
public function getPosition(): string | ||
{ | ||
// this factory doesn't register any authenticators, this position doesn't matter | ||
return 'pre_auth'; | ||
} | ||
|
||
public function getKey(): string | ||
{ | ||
return 'login_throttling'; | ||
} | ||
|
||
/** | ||
* @param ArrayNodeDefinition $builder | ||
*/ | ||
public function addConfiguration(NodeDefinition $builder) | ||
{ | ||
$builder | ||
->children() | ||
->scalarNode('limiter')->info('The name of the limiter that you defined under "framework.rate_limiter".')->end() | ||
->integerNode('max_attempts')->defaultValue(5)->end() | ||
->end(); | ||
} | ||
|
||
public function createAuthenticator(ContainerBuilder $container, string $firewallName, array $config, string $userProviderId): array | ||
{ | ||
if (!class_exists(LoginThrottlingListener::class)) { | ||
throw new \LogicException('Login throttling requires symfony/security-http:^5.2.'); | ||
} | ||
|
||
if (!isset($config['limiter'])) { | ||
if (!class_exists(FrameworkExtension::class) || !method_exists(FrameworkExtension::class, 'registerRateLimiter')) { | ||
throw new \LogicException('You must either configure a rate limiter for "security.firewalls.'.$firewallName.'.login_throttling" or install symfony/framework-bundle:^5.2'); | ||
} | ||
|
||
FrameworkExtension::registerRateLimiter($container, $config['limiter'] = '_login_'.$firewallName, [ | ||
'strategy' => 'fixed_window', | ||
'limit' => $config['max_attempts'], | ||
'interval' => '1 minute', | ||
'lock_factory' => 'lock.factory', | ||
'cache_pool' => 'cache.app', | ||
]); | ||
} | ||
|
||
$container | ||
->setDefinition('security.listener.login_throttling.'.$firewallName, new ChildDefinition('security.listener.login_throttling')) | ||
->replaceArgument(1, new Reference('limiter.'.$config['limiter'])) | ||
->addTag('kernel.event_subscriber', ['dispatcher' => 'security.event_dispatcher.'.$firewallName]); | ||
|
||
return []; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...Symfony/Bundle/SecurityBundle/Tests/Functional/app/StandardFormLogin/login_throttling.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
imports: | ||
- { resource: ./config.yml } | ||
|
||
framework: | ||
lock: ~ | ||
rate_limiter: ~ | ||
|
||
security: | ||
firewalls: | ||
default: | ||
login_throttling: | ||
max_attempts: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...Symfony/Component/Security/Core/Exception/TooManyLoginAttemptsAuthenticationException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Exception; | ||
|
||
/** | ||
* This exception is thrown if there where too many failed login attempts in | ||
* this session. | ||
* | ||
* @author Wouter de Jong <[email protected]> | ||
*/ | ||
class TooManyLoginAttemptsAuthenticationException extends AuthenticationException | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMessageKey(): string | ||
{ | ||
return 'Too many failed login attempts, please try again later.'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.