8000 fix potential timing attack issue · symfony/symfony@f1fd768 · GitHub
[go: up one dir, main page]

Skip to content

Commit f1fd768

Browse files
xabbuhfabpot
authored andcommitted
fix potential timing attack issue
1 parent 3dc2244 commit f1fd768

File tree

2 files changed

+4
-27
lines changed

2 files changed

+4
-27
lines changed

src/Symfony/Component/Security/Http/RememberMe/PersistentTokenBasedRememberMeServices.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2222
use Symfony\Component\Security\Core\Util\SecureRandomInterface;
2323
use Psr\Log\LoggerInterface;
24+
use Symfony\Component\Security\Core\Util\StringUtils;
2425

2526
/**
2627
* Concrete implementation of the RememberMeServicesInterface which needs
@@ -90,7 +91,7 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
9091
list($series, $tokenValue) = $cookieParts;
9192
$persistentToken = $this->tokenProvider->loadTokenBySeries($series);
9293

93-
if ($persistentToken->getTokenValue() !== $tokenValue) {
94+
if (!StringUtils::equals($persistentToken->getTokenValue(), $tokenValue)) {
9495
throw new CookieTheftException('This token was already used. The account is possibly compromised.');
9596
}
9697

src/Symfony/Component/Security/Http/RememberMe/TokenBasedRememberMeServices.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1818
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1919
use Symfony\Component\Security\Core\User\UserInterface;
20+
use Symfony\Component\Security\Core\Util\StringUtils;
2021

2122
/**
2223
* Concrete implementation of the RememberMeServicesInterface providing
@@ -53,7 +54,7 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
5354
throw new \RuntimeException(sprintf('The UserProviderInterface implementation must return an instance of UserInterface, but returned "%s".', get_class($user)));
5455
}
5556

56-
if (true !== $this->compareHashes($hash, $this->generateCookieHash($class, $username, $expires, $user->getPassword()))) {
57+
if (!StringUtils::equals($this->generateCookieHash($class, $username, $expires, $user->getPassword()), $hash)) {
5758
throw new AuthenticationException('The cookie\'s hash is invalid.');
5859
}
5960

@@ -64,31 +65,6 @@ protected function processAutoLoginCookie(array $cookieParts, Request $request)
6465
return $user;
6566
}
6667

67-
/**
68-
* Compares two hashes using a constant-time algorithm to avoid (remote)
69-
* timing attacks.
70-
*
71-
* This is the same implementation as used in the BasePasswordEncoder.
72-
*
73-
* @param string $hash1 The first hash
74-
* @param string $hash2 The second hash
75-
*
76-
* @return bool true if the two hashes are the same, false otherwise
77-
*/
78-
private function compareHashes($hash1, $hash2)
79-
{
80-
if (strlen($hash1) !== $c = strlen($hash2)) {
81-
return false;
82-
}
83-
84-
$result = 0;
85-
for ($i = 0; $i < $c; ++$i) {
86-
$result |= ord($hash1[$i]) ^ ord($hash2[$i]);
87-
}
88-
89-
return 0 === $result;
90-
}
91-
9268
/**
9369
* {@inheritdoc}
9470
*/

0 commit comments

Comments
 (0)
0