From 6bbe4169f973aae51c799ff227197a14fe5bdb9e Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 15 May 2020 14:26:22 +0200 Subject: [PATCH] [Security/Core] fix compat of `NativePasswordEncoder` with pre-PHP74 values of `PASSWORD_*` consts --- .../Core/Encoder/NativePasswordEncoder.php | 14 ++++++++++++-- .../Tests/Encoder/NativePasswordEncoderTest.php | 8 ++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Security/Core/Encoder/NativePasswordEncoder.php b/src/Symfony/Component/Security/Core/Encoder/NativePasswordEncoder.php index 10b96dc506de1..a65236213181d 100644 --- a/src/Symfony/Component/Security/Core/Encoder/NativePasswordEncoder.php +++ b/src/Symfony/Component/Security/Core/Encoder/NativePasswordEncoder.php @@ -24,7 +24,7 @@ final class NativePasswordEncoder implements PasswordEncoderInterface, SelfSalti { private const MAX_PASSWORD_LENGTH = 4096; - private $algo; + private $algo = PASSWORD_BCRYPT; private $options; /** @@ -48,7 +48,17 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos throw new \InvalidArgumentException('$cost must be in the range of 4-31.'); } - $this->algo = (string) ($algo ?? (\defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : (\defined('PASSWORD_ARGON2I') ? PASSWORD_ARGON2I : PASSWORD_BCRYPT))); + $legacyAlgo = [1 => PASSWORD_BCRYPT]; + + if (\defined('PASSWORD_ARGON2I')) { + $this->algo = $legacyAlgo[2] = (string) PASSWORD_ARGON2I; + } + + if (\defined('PASSWORD_ARGON2ID')) { + $this->algo = $legacyAlgo[3] = (string) PASSWORD_ARGON2ID; + } + + $this->algo = $legacyAlgo[$algo] ?? $algo ?? $this->algo; $this->options = [ 'cost' => $cost, 'time_cost' => $opsLimit, diff --git a/src/Symfony/Component/Security/Core/Tests/Encoder/NativePasswordEncoderTest.php b/src/Symfony/Component/Security/Core/Tests/Encoder/NativePasswordEncoderTest.php index 47b8ac09eaa69..9388e9c2c53cc 100644 --- a/src/Symfony/Component/Security/Core/Tests/Encoder/NativePasswordEncoderTest.php +++ b/src/Symfony/Component/Security/Core/Tests/Encoder/NativePasswordEncoderTest.php @@ -73,6 +73,14 @@ public function testConfiguredAlgorithm() $this->assertStringStartsWith('$2', $result); } + public function testConfiguredAlgorithmWithLegacyConstValue() + { + $encoder = new NativePasswordEncoder(null, null, null, '1'); + $result = $encoder->encodePassword('password', null); + $this->assertTrue($encoder->isPasswordValid($result, 'password', null)); + $this->assertStringStartsWith('$2', $result); + } + public function testCheckPasswordLength() { $encoder = new NativePasswordEncoder(null, null, 4);