8000 [Security] add PasswordEncoderInterface::needsRehash() by nicolas-grekas · Pull Request #31594 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] add PasswordEncoderInterface::needsRehash() #31594

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
Jun 4, 2019
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
[Security] add PasswordEncoderInterface::needsRehash()
  • Loading branch information
nicolas-grekas committed Jun 3, 2019
commit 50590dce81cae24cc0dd97918403df59bc5cdfda
5 changes: 5 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ MonologBridge

* The `RouteProcessor` has been marked final.

Security
--------

* Implementations of `PasswordEncoderInterface` and `UserPasswordEncoderInterface` should add a new `needsRehash()` method

TwigBridge
----------

Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Routing
Security
--------

* Implementations of `PasswordEncoderInterface` and `UserPasswordEncoderInterface` must have a new `needsRehash()` method
* The `Role` and `SwitchUserRole` classes have been removed.
* The `getReachableRoles()` method of the `RoleHierarchy` class has been removed. It has been replaced by the new
`getReachableRoleNames()` method.
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* Added method `needsRehash()` to `PasswordEncoderInterface` and `UserPasswordEncoderInterface`

4.3.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ abstract class BasePasswordEncoder implements PasswordEncoderInterface
{
const MAX_PASSWORD_LENGTH = 4096;

/**
* {@inheritdoc}
*/
public function needsRehash(string $encoded): bool
{
return false;
}

/**
* Demerges a merge password and salt string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ public function isPasswordValid($encoded, $raw, $salt)

return \strlen($raw) <= self::MAX_PASSWORD_LENGTH && password_verify($raw, $encoded);
}

/**
* {@inheritdoc}
*/
public function needsRehash(string $encoded): bool
{
return password_needs_rehash($encoded, $this->algo, $this->options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* PasswordEncoderInterface is the interface for all encoders.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @method bool needsRehash(string $encoded)
*/
interface PasswordEncoderInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,20 @@ public function isPasswordValid($encoded, $raw, $salt)

throw new LogicException('Libsodium is not available. You should either install the sodium extension, upgrade to PHP 7.2+ or use a different encoder.');
}

/**
* {@inheritdoc}
*/
public function needsRehash(string $encoded): bool
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a check for BCrypt passwords too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

28BE

It's deprecated so I don't think we should improve it further.

{
if (\function_exists('sodium_crypto_pwhash_str_needs_rehash')) {
return \sodium_crypto_pwhash_str_needs_rehash($encoded, $this->opsLimit, $this->memLimit);
}

if (\extension_loaded('libsodium')) {
return \Sodium\crypto_pwhash_str_needs_rehash($encoded, $this->opsLimit, $this->memLimit);
}

throw new LogicException('Libsodium is not available. You should either install the sodium extension, upgrade to PHP 7.2+ or use a different encoder.');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,14 @@ public function isPasswordValid(UserInterface $user, $raw)

return $encoder->isPasswordValid($user->getPassword(), $raw, $user->getSalt());
}

/**
* {@inheritdoc}
*/
public function needsRehash(UserInterface $user, string $encoded): bool
{
$encoder = $this->encoderFactory->getEncoder($user);

return method_exists($encoder, 'needsRehash') && $encoder->needsRehash($encoded);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* UserPasswordEncoderInterface is the interface for the password encoder service.
*
* @author Ariel Ferrandini <arielferrandini@gmail.com>
*
* @method bool needsRehash(UserInterface $user, string $encoded)
*/
interface UserPasswordEncoderInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public function testIsPasswordTooLong()
$this->assertFalse($this->invokeIsPasswordTooLong(str_repeat('a', 10)));
}

public function testNeedsRehash()
{
$encoder = new PasswordEncoder();
$this->assertFalse($encoder->needsRehash('foo'));
}

protected function invokeDemergePasswordAndSalt($password)
{
$encoder = new PasswordEncoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ public function testCheckPasswordLength()
$this->assertFalse($encoder->isPasswordValid($result, str_repeat('a', 73), 'salt'));
$this->assertTrue($encoder->isPasswordValid($result, str_repeat('a', 72), 'salt'));
}

public function testNeedsRehash()
{
$encoder = new NativePasswordEncoder(4, 11000, 4);

$this->assertTrue($encoder->needsRehash('dummyhash'));

$hash = $encoder->encodePassword('foo', 'salt');
$this->assertFalse($encoder->needsRehash($hash));

$encoder = new NativePasswordEncoder(5, 11000, 5);
$this->assertTrue($encoder->needsRehash($hash));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,17 @@ public function testUserProvidedSaltIsNotUsed()
$result = $encoder->encodePassword('password', 'salt');
$this->assertTrue($encoder->isPasswordValid($result, 'password', 'anotherSalt'));
}

public function testNeedsRehash()
{
$encoder = new SodiumPasswordEncoder(4, 11000);

$this->assertTrue($encoder->needsRehash('dummyhash'));

$hash = $encoder->encodePassword('foo', 'salt');
$this->assertFalse($encoder->needsRehash($hash));

$encoder = new SodiumPasswordEncoder(5, 11000);
$this->assertTrue($encoder->needsRehash($hash));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
namespace Symfony\Component\Security\Core\Tests\Encoder;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder;
use Symfony\Component\Security\Core\User\User;

class UserPasswordEncoderTest extends TestCase
{
Expand Down Expand Up @@ -68,4 +71,23 @@ public function testIsPasswordValid()
$isValid = $passwordEncoder->isPasswordValid($userMock, 'plainPassword');
$this->assertTrue($isValid);
}

public function testNeedsRehash()
{
$user = new User('username', null);
$encoder = new NativePasswordEncoder(4, 20000, 4);

$mockEncoderFactory = $this->getMockBuilder(EncoderFactoryInterface::class)->getMock();
$mockEncoderFactory->expects($this->any())
->method('getEncoder')
->with($user)
->will($this->onConsecutiveCalls($encoder, $encoder, new NativePasswordEncoder(5, 20000, 5), $encoder));

$passwordEncoder = new UserPasswordEncoder($mockEncoderFactory);

$hash = $passwordEncoder->encodePassword($user, 'foo', 'salt');
$this->assertFalse($passwordEncoder->needsRehash($user, $hash));
$this->assertTrue($passwordEncoder->needsRehash($user, $hash));
$this->assertFalse($passwordEncoder->needsRehash($user, $hash));
}
}
0