Skip to content
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

[Security/Core] fix compat of NativePasswordEncoder with pre-PHP74 values of PASSWORD_* consts #36824

Merged
merged 1 commit into from May 16, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -24,7 +24,7 @@ final class NativePasswordEncoder implements PasswordEncoderInterface, SelfSalti
{
private const MAX_PASSWORD_LENGTH = 4096;

private $algo;
private $algo = PASSWORD_BCRYPT;
private $options;

/**
Expand All @@ -48,7 +48,20 @@ 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)));
$algos = [1 => PASSWORD_BCRYPT, '2y' => PASSWORD_BCRYPT];

if (\defined('PASSWORD_ARGON2I')) {
$this->algo = $algos[2] = $algos['argon2i'] = (string) PASSWORD_ARGON2I;
}

if (\defined('PASSWORD_ARGON2ID')) {
$this->algo = $algos[3] = $algos['argon2id'] = (string) PASSWORD_ARGON2ID;
}

if (null !== $algo) {
$this->algo = $algos[$algo] ?? $algo;
}

$this->options = [
'cost' => $cost,
'time_cost' => $opsLimit,
Expand Down
Expand Up @@ -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);
Expand Down