yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

Fix login cookie destroyed after authKey change (#19621)

Open MarkoNV opened this issue 3 years ago • 8 comments

I needed to modify yiiunit\framework\web\UserIdentity to be able to mock change of authKey for tests. UserIdentity::reset() restores authKey for other tests and is called from tearDown() method.

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues #19621

MarkoNV avatar Oct 18 '22 10:10 MarkoNV

Sorry, I noticed that I didn't update issue number in branch name. Is it issue?

MarkoNV avatar Oct 18 '22 11:10 MarkoNV

Not a problem.

bizley avatar Oct 18 '22 12:10 bizley

The change is controversial. The change of auth key should invalidate sessions.

samdark avatar Oct 26 '22 13:10 samdark

@MarkoNV would you please explain the motivation of the PR?

samdark avatar Oct 26 '22 13:10 samdark

@samdark

After #18540, change of authKey invalidates sessions. But documentation says calling yii\web\User::switchIdentity() or yii\web\User::login() will prevent logout of current session, which is done for user experience (only session that caused change of authKey is restored).

Issue with that solution is, while session is restored, remember me cookie is destroyed. My solution keeps both session and remember me cookie (if exists for current user), but still only for session which caused change of authKey - all other sessions are still logged out as intended.

Why I think preserving remember me cookie is important? User thinks his account is compromised and changes password to log out other sessions. If he is logged out on next visit, despite fact that he was logged in with "remember me" option, he will think his account is still compromised (someone logged him out).

See #19621

MarkoNV avatar Oct 26 '22 13:10 MarkoNV

@samdark can you check failing tests? My commit passed tests, only after merging master branch to my branch errors occurred. Also, I see that many other pull requests have failed tests. Is something broken with tests or pull request which fails tests is merged into master?

MarkoNV avatar Nov 02 '22 09:11 MarkoNV

Test issues aren't related to this PR.

samdark avatar Nov 07 '22 06:11 samdark

Changing authKey of a user is meant to invalidate sessions including authentication cookies. But if the user himself initiates it then he's unlikely want to be logged out in the process.

samdark avatar Nov 15 '22 08:11 samdark