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

Token verification may fail for DateTime instances with milliseconds resolution #259

Open
spetters opened this issue Feb 1, 2023 · 0 comments

Comments

@spetters
Copy link

spetters commented Feb 1, 2023

In our project we had issues with wrong password reset requests. We were able to track this down to a rounding issue with the expiresAt field for the ResetPasswordRequest.

Setup:
Symfony 6.2.6
PHP 8.1.13
Doctrine MySQL

For the hashed token, the unix timestamp of the expiresAt is used and stored. Also expiresAt is stored as DateTimeImmutable. With Doctrine entries with milliseconds will get rounded and stored with an one second higher value in the database.

During verification based on the expiresAt value, the hashes then do not match and the user is confronted with an error.

Our solution is to strip the milliseconds while creating a ResetPasswordRequest:

...
class ResetPasswordRequest implements ResetPasswordRequestInterface {

	use ResetPasswordRequestTrait;

	...

	public function __construct(User $user, \DateTimeInterface $expiresAt, string $selector, string $hashedToken) {
		$this->user = $user;

		// strip milliseconds
		$mutable = \DateTime::createFromInterface($expiresAt);
		$mutable->setTime(
			(int) $expiresAt->format('G'),
			(int) $expiresAt->format('i'),
			(int) $expiresAt->format('s')
		);

		$this->initialize($mutable, $selector, $hashedToken);
	}
...

This could be added to the maker script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant