8000 [Security/Core] add fast path when encoded password cannot match anyt… · dunglas/symfony@c57f8f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit c57f8f7

Browse files
[Security/Core] add fast path when encoded password cannot match anything
1 parent bfd308f commit c57f8f7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class MessageDigestPasswordEncoder extends BasePasswordEncoder
2222
{
2323
private $algorithm;
2424
private $encodeHashAsBase64;
25-
private $iterations;
25+
private $iterations = 0;
26+
private $encodedLength = -1;
2627

2728
/**
2829
* @param string $algorithm The digest algorithm to use
@@ -33,6 +34,13 @@ public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase
3334
{
3435
$this->algorithm = $algorithm;
3536
$this->encodeHashAsBase64 = $encodeHashAsBase64;
37+
38+
try {
39+
$this->encodedLength = \strlen($this->encodePassword('', 'salt'));
40+
} catch (\LogicException $e) {
41+
// ignore algorithm not supported
42+
}
43+
3644
$this->iterations = $iterations;
3745
}
3846

@@ -65,6 +73,10 @@ public function encodePassword($raw, $salt)
6573
*/
6674
public function isPasswordValid($encoded, $raw, $salt)
6775
{
76+
if (\strlen($encoded) !== $this->encodedLength || false !== strpos($encoded, '$')) {
77+
return false;
78+
}
79+
6880
return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
6981
}
7082
}

src/Symfony/Component/Security/Core/Encoder/Pbkdf2PasswordEncoder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Pbkdf2PasswordEncoder extends BasePasswordEncoder
3232
private $encodeHashAsBase64;
3333
private $iterations;
3434
private $length;
35+
private $encodedLength;
3536

3637
/**
3738
* @param string $algorithm The digest algorithm to use
@@ -45,6 +46,7 @@ public function __construct(string $algorithm = 'sha512', bool $encodeHashAsBase
4546
$this->encodeHashAsBase64 = $encodeHashAsBase64;
4647
$this->iterations = $iterations;
4748
$this->length = $length;
49+
$this->encodedLength = $encodeHashAsBase64 ? intdiv($length + 2, 3) << 2 : ($length << 1);
4850
}
4951

5052
/**
@@ -72,6 +74,10 @@ public function encodePassword($raw, $salt)
7274
*/
7375
public function isPasswordValid($encoded, $raw, $salt)
7476
{
77+
if ((0 < $this->length && \strlen($encoded) !== $this->encodedLength) || false !== strpos($encoded, '$')) {
78+
return false;
79+
}
80+
7581
return !$this->isPasswordTooLong($raw) && $this->comparePasswords($encoded, $this->encodePassword($raw, $salt));
7682
}
7783
}

0 commit comments

Comments
 (0)
0