Skip to content

Commit

Permalink
[Security] Fix best encoder not wired using migrate_from
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin Chalas committed Nov 17, 2019
1 parent 7ed6d35 commit 4132a60
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Expand Up @@ -114,7 +114,9 @@ private function getEncoderConfigFromAlgorithm(array $config): array
}

if ($fromEncoders = ($config['migrate_from'] ?? false)) {
$encoderChain = [];
unset($config['migrate_from']);
$encoderChain = [$this->createEncoder($config, true)];

foreach ($fromEncoders as $name) {
if ($encoder = $this->encoders[$name] ?? false) {
$encoder = $encoder instanceof PasswordEncoderInterface ? $encoder : $this->createEncoder($encoder, true);
Expand Down
Expand Up @@ -143,9 +143,7 @@ public function testMigrateFrom()

$factory = new EncoderFactory([
'digest_encoder' => $digest = new MessageDigestPasswordEncoder('sha256'),
'pbdkf2' => $digest = new MessageDigestPasswordEncoder('sha256'),
'bcrypt_encoder' => ['algorithm' => 'bcrypt'],
SomeUser::class => ['algorithm' => 'sodium', 'migrate_from' => ['bcrypt_encoder', 'digest_encoder']],
SomeUser::class => ['algorithm' => 'sodium', 'migrate_from' => ['bcrypt', 'digest_encoder']],
]);

$encoder = $factory->getEncoder(SomeUser::class);
Expand All @@ -154,6 +152,7 @@ public function testMigrateFrom()
$this->assertTrue($encoder->isPasswordValid((new SodiumPasswordEncoder())->encodePassword('foo', null), 'foo', null));
$this->assertTrue($encoder->isPasswordValid((new NativePasswordEncoder(null, null, null, \PASSWORD_BCRYPT))->encodePassword('foo', null), 'foo', null));
$this->assertTrue($encoder->isPasswordValid($digest->encodePassword('foo', null), 'foo', null));
$this->assertStringStartsWith(SODIUM_CRYPTO_PWHASH_STRPREFIX, $encoder->encodePassword('foo', null));
}

public function testDefaultMigratingEncoders()
Expand Down

0 comments on commit 4132a60

Please sign in to comment.