Skip to content

Commit 1c477c6

Browse files
committed
bug #40972 Avoid regenerating the remember me token if it is still fresh (Seldaek)
This PR was merged into the 5.3-dev branch. Discussion ---------- Avoid regenerating the remember me token if it is still fresh | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | ~yes | New feature? | no? | Deprecations? | no | Tickets | Refs symfony/symfony#40971 | License | MIT | Doc PR | <!-- required for new features --> Please see symfony/symfony#40971 for more information about the context of this change. As it was discussed in symfony/symfony#18384 - regenerating the remember me token/cookie is done to avoid old cookies being stolen and reused, this is a valid concern (although cookie theft is much harder these days with httpOnly and secure flags) and a good security practice, but if the token was refreshed very recently it seems a bit overkill to refresh it again, it leads to more DB writes, and for us who are trying to support concurrent re-authenticating requests it is causing further problems if every request triggers a new token update. I'd be happy to also update this in the old PersistentTokenBasedRememberMeServices if needed, but I find that it is perhaps better to just do this in the new auth system as it was until 5.3 considered experimental. Commits ------- a942b5f684 Avoid regenerating the remember me token if it is still fresh
2 parents c4bf213 + 22d2538 commit 1c477c6

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

RememberMe/PersistentRememberMeHandler.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ public function processRememberMe(RememberMeDetails $rememberMeDetails, UserInte
7474
throw new AuthenticationException('The cookie has expired.');
7575
}
7676

77-
$tokenValue = base64_encode(random_bytes(64));
78-
$this->tokenProvider->updateToken($series, $this->generateHash($tokenValue), new \DateTime());
77+
// if a token was regenerated less than a minute ago, there is no need to regenerate it
78+
// if multiple concurrent requests reauthenticate a user we do not want to update the token several times
79+
if ($persistentToken->getLastUsed()->getTimestamp() + 60 < time()) {
80+
$tokenValue = base64_encode(random_bytes(64));
81+
$this->tokenProvider->updateToken($series, $this->generateHash($tokenValue), new \DateTime());
82+
}
7983

8084
$this->createCookie($rememberMeDetails->withValue($tokenValue));
8185
}

0 commit comments

Comments
 (0)