Skip to content

Commit 0a3c50b

Browse files
committed
Compare codes with constant-time hash_equals, fixes #318
Signed-off-by: Christian Scheb <git@christianscheb.de>
1 parent 191b0a6 commit 0a3c50b

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

src/email/Security/TwoFactor/Provider/Email/EmailTwoFactorProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
1313
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorProviderInterface;
1414
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
15+
use function hash_equals;
1516
use function str_replace;
1617

1718
/**
@@ -57,12 +58,17 @@ public function validateAuthenticationCode(object $user, string $authenticationC
5758
return false;
5859
}
5960

61+
$currentAuthCode = $user->getEmailAuthCode();
62+
if (null === $currentAuthCode) {
63+
return false;
64+
}
65+
6066
$event = new TwoFactorCodeEvent($user, $authenticationCode);
6167
$this->eventDispatcher->dispatch($event, EmailCodeEvents::CHECK);
6268

6369
// Strip any user added spaces
6470
$authenticationCode = str_replace(' ', '', $authenticationCode);
65-
$isValid = $user->getEmailAuthCode() === $authenticationCode;
71+
$isValid = hash_equals($currentAuthCode, $authenticationCode);
6672
$this->eventDispatcher->dispatch($event, $isValid ? EmailCodeEvents::VALID : EmailCodeEvents::INVALID);
6773

6874
return $isValid;

0 commit comments

Comments
 (0)