-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] New Password Policy listener #49821
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
Closed
Closed
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
45 changes: 45 additions & 0 deletions
45
src/Symfony/Bundle/SecurityBundle/Tests/Functional/PasswordPolicyTest.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,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
|
||
class PasswordPolicyTest extends AbstractWebTestCase | ||
{ | ||
/** | ||
* @dataProvider providePassword | ||
*/ | ||
public function testLoginFailBecauseThePasswordIsBlacklisted(string $password, string $expectedMessage) | ||
{ | ||
// Given | ||
$client = $this->createClient(['test_case' => 'PasswordPolicy', 'root_config' => 'config.yml']); | ||
|
||
// When | ||
$client->request('POST', '/chk', [], [], ['CONTENT_TYPE' => 'application/json'], '{"user": {"login": "dunglas", "password": "'.$password.'"}}'); | ||
$response = $client->getResponse(); | ||
|
||
// Then | ||
$this->assertInstanceOf(JsonResponse::class, $response); | ||
$this->assertSame(401, $response->getStatusCode()); | ||
$this->assertSame(['error' => $expectedMessage], json_decode($response->getContent(), true)); | ||
} | ||
|
||
public static function providePassword(): iterable | ||
{ | ||
yield ['foo', 'The password does not fulfill the password policy.']; | ||
yield ['short?', 'The password does not fulfill the password policy.']; | ||
yield ['Good password?', 'The password does not fulfill the password policy.']; | ||
|
||
// The following password fulfills the password policy, but is not valid. | ||
yield ['Is it a v4l1d pasw0rd?', 'Invalid credentials.']; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ony/Bundle/SecurityBundle/Tests/Functional/app/PasswordPolicy/BlocklistPasswordPolicy.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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\app\PasswordPolicy; | ||
|
||
use Symfony\Component\Security\Http\Authenticator\Passport\Policy\PasswordPolicyInterface; | ||
|
||
final class BlocklistPasswordPolicy implements PasswordPolicyInterface | ||
{ | ||
public function verify(#[\SensitiveParameter] string $plaintextPassword): bool | ||
{ | ||
return 'foo' !== $plaintextPassword; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/PasswordPolicy/bundles.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,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
return [ | ||
new Symfony\Bundle\SecurityBundle\SecurityBundle(), | ||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\TestBundle(), | ||
]; |
51 changes: 51 additions & 0 deletions
51
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/PasswordPolicy/config.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,51 @@ | ||
imports: | ||
- { resource: ./../config/framework.yml } | ||
|
||
framework: | ||
http_method_override: false | ||
serializer: ~ | ||
|
||
security: | ||
password_policies: | ||
- 'policy.blocklist' | ||
- 'policy.constraint_password' | ||
|
||
password_hashers: | ||
Symfony\Component\Security\Core\User\InMemoryUser: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
dunglas: { password: foo, roles: [ROLE_USER] } | ||
|
||
firewalls: | ||
main: | ||
pattern: ^/ | ||
json_login: | ||
check_path: /chk | ||
username_path: user.login | ||
password_path: user.password | ||
|
||
access_control: | ||
- { path: ^/foo, roles: ROLE_USER } | ||
|
||
|
||
services: | ||
policy.constraint_password.length: | ||
class: Symfony\Component\Validator\Constraints\Length | ||
arguments: | ||
- min: 8 | ||
policy.constraint_password.strength: | ||
class: Symfony\Component\Validator\Constraints\PasswordStrength | ||
arguments: | ||
- minScore: 2 | ||
|
||
policy.constraint_password: | ||
class: Symfony\Component\Security\Http\Authenticator\Passport\Policy\PasswordConstraintPolicy | ||
arguments: | ||
- '@validator' | ||
- ['@policy.constraint_password.length', '@policy.constraint_password.strength'] | ||
|
||
policy.blocklist: | ||
class: Symfony\Bundle\SecurityBundle\Tests\Functional\app\PasswordPolicy\BlocklistPasswordPolicy |
3 changes: 3 additions & 0 deletions
3
src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/PasswordPolicy/routing.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,3 @@ | ||
login_check: | ||
path: /chk | ||
defaults: { _controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\JsonLoginBundle\Controller\TestController::loginCheckAction } |
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
20 changes: 20 additions & 0 deletions
20
src/Symfony/Component/Security/Core/Exception/PasswordPolicyException.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,20 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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; | ||
|
||
final class PasswordPolicyException extends AuthenticationException | ||
{ | ||
public function getMessageKey(): string | ||
{ | ||
return 'The password does not fulfill the password policy.'; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...ymfony/Component/Security/Http/Authenticator/Passport/Policy/PasswordConstraintPolicy.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,38 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Authenticator\Passport\Policy; | ||
|
||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
|
||
final class PasswordConstraintPolicy implements PasswordPolicyInterface | ||
{ | ||
/** | ||
* @param array<Constraint> $constraints | ||
*/ | ||
public function __construct( | ||
private readonly ValidatorInterface $validator, | ||
private readonly array $constraints = [] | ||
) { | ||
} | ||
|
||
/** | ||
* @throws AuthenticationException | ||
*/ | ||
public function verify(#[\SensitiveParameter] string $plaintextPassword): bool | ||
{ | ||
$errors = $this->validator->validate($plaintextPassword, $this->constraints); | ||
|
||
return !$errors->count(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...Symfony/Component/Security/Http/Authenticator/Passport/Policy/PasswordPolicyInterface.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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Authenticator\Passport\Policy; | ||
|
||
use Symfony\Component\Security\Core\Exception\AuthenticationException; | ||
|
||
interface PasswordPolicyInterface | ||
{ | ||
/** | ||
* @throws AuthenticationException | ||
*/ | ||
public function verify(#[\SensitiveParameter] string $plaintextPassword): bool; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we stipulate that, instead of returning a bool, it must either throw a |
||
} |
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
55 changes: 55 additions & 0 deletions
55
src/Symfony/Component/Security/Http/EventListener/PasswordPolicyListener.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,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\EventListener; | ||
|
||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Security\Core\Exception\PasswordPolicyException; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | ||
use Symfony\Component\Security\Http\Authenticator\Passport\Policy\PasswordPolicyInterface; | ||
use Symfony\Component\Security\Http\Event\CheckPassportEvent; | ||
|
||
final class PasswordPolicyListener implements EventSubscriberInterface | ||
{ | ||
/** | ||
* @param PasswordPolicyInterface[] $policies | ||
*/ | ||
public function __construct(private readonly array $policies) | ||
{ | ||
} | ||
|
||
public function checkPassport(CheckPassportEvent $event): void | ||
{ | ||
$passport = $event->getPassport(); | ||
if (!$passport->hasBadge(PasswordCredentials::class)) { | ||
return; | ||
} | ||
|
||
$badge = $passport->getBadge(PasswordCredentials::class); | ||
if ($badge->isResolved()) { | ||
return; | ||
} | ||
|
||
$plaintextPassword = $badge->getPassword(); | ||
foreach ($this->policies as $policy) { | ||
if (!$policy->verify($plaintextPassword)) { | ||
throw new PasswordPolicyException(); | ||
} | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
CheckPassportEvent::class => ['checkPassport', 512], | ||
]; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing comma at the end