Skip to content

Commit 3e4ef8c

Browse files
Merge branch '4.3' into 4.4
* 4.3: [OptionsResolve] Revert change in tests for a not-merged change in code [HttpClient] fix handling of 3xx with no Location header - ignore Content-Length when no body is expected [Workflow] Made the configuration more robust for the 'property' key [Security/Core] make NativePasswordEncoder use sodium to validate passwords when possible #30432 fix an error message fix paths to detect code owners [HttpClient] ignore the body of responses to HEAD requests [Validator] Ensure numeric subpaths do not cause errors on PHP 7.4 [SecurityBundle] Fix wrong assertion Remove unused local variables in tests [Yaml][Parser] Remove the getLastLineNumberBeforeDeprecation() internal unused method Make sure to collect child forms created on *_SET_DATA events [WebProfilerBundle] Improve display in Email panel for dark theme do not render errors for checkboxes twice
2 parents 04f74a1 + 8c46ea7 commit 3e4ef8c

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

Encoder/NativePasswordEncoder.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
4848
throw new \InvalidArgumentException('$cost must be in the range of 4-31.');
4949
}
5050

51-
$this->algo = $algo ?? (\defined('PASSWORD_ARGON2I') ? max(PASSWORD_DEFAULT, \defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : PASSWORD_ARGON2I) : PASSWORD_DEFAULT);
51+
$this->algo = (string) ($algo ?? \defined('PASSWORD_ARGON2ID') ? PASSWORD_ARGON2ID : (\defined('PASSWORD_ARGON2I') ? PASSWORD_ARGON2I : PASSWORD_BCRYPT));
5252
$this->options = [
5353
'cost' => $cost,
5454
'time_cost' => $opsLimit,
@@ -62,33 +62,38 @@ public function __construct(int $opsLimit = null, int $memLimit = null, int $cos
6262
*/
6363
public function encodePassword($raw, $salt): string
6464
{
65-
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH) {
65+
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH || ((string) PASSWORD_BCRYPT === $this->algo && 72 < \strlen($raw))) {
6666
throw new BadCredentialsException('Invalid password.');
6767
}
6868

6969
// Ignore $salt, the auto-generated one is always the best
7070

71-
$encoded = password_hash($raw, $this->algo, $this->options);
72-
73-
if (72 < \strlen($raw) && 0 === strpos($encoded, '$2')) {
74-
// BCrypt encodes only the first 72 chars
75-
throw new BadCredentialsException('Invalid password.');
76-
}
77-
78-
return $encoded;
71+
return password_hash($raw, $this->algo, $this->options);
7972
}
8073

8174
/**
8275
* {@inheritdoc}
8376
*/
8477
public function isPasswordValid($encoded, $raw, $salt): bool
8578
{
86-
if (72 < \strlen($raw) && 0 === strpos($encoded, '$2')) {
87-
// BCrypt encodes only the first 72 chars
79+
if (\strlen($raw) > self::MAX_PASSWORD_LENGTH) {
8880
return false;
8981
}
9082

91-
return \strlen($raw) <= self::MAX_PASSWORD_LENGTH && password_verify($raw, $encoded);
83+
if (0 === strpos($encoded, '$2')) {
84+
// BCrypt encodes only the first 72 chars
85+
return 72 >= \strlen($raw) && password_verify($raw, $encoded);
86+
}
87+
88+
if (\extension_loaded('sodium') && version_compare(\SODIUM_LIBRARY_VERSION, '1.0.14', '>=')) {
89+
return sodium_crypto_pwhash_str_verify($encoded, $raw);
90+
}
91+
92+
if (\extension_loaded('libsodium') && version_compare(phpversion('libsodium'), '1.0.14', '>=')) {
93+
return \Sodium\crypto_pwhash_str_verify($encoded, $raw);
94+
}
95+
96+
return password_verify($raw, $encoded);
9297
}
9398

9499
/**

Encoder/SodiumPasswordEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function isPasswordValid($encoded, $raw, $salt): bool
9393
return \Sodium\crypto_pwhash_str_verify($encoded, $raw);
9494
}
9595

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

9999
/**

Tests/Encoder/EncoderFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testGetInvalidNamedEncoderForEncoderAware()
117117

118118
$user = new EncAwareUser('user', 'pass');
119119
$user->encoderName = 'invalid_encoder_name';
120-
$encoder = $factory->getEncoder($user);
120+
$factory->getEncoder($user);
121121
}
122122

123123
public function testGetEncoderForEncoderAwareWithClassName()

0 commit comments

Comments
 (0)