Skip to content

Commit 998a1a6

Browse files
Merge pull request #63721 from nextcloud/backport/63705/stable32
[stable32] fix: Check rememberme cookie previous session id matches uid
2 parents 976c0d6 + 93cb3f5 commit 998a1a6

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

lib/private/User/Session.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,26 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
901901
]);
902902
return false;
903903
}
904+
905+
try {
906+
$oldToken = $this->tokenProvider->getToken($oldSessionId);
907+
} catch (InvalidTokenException $ex) {
908+
$this->logger->error('Could not find the session token to renew', [
909+
'app' => 'core',
910+
'user' => $uid,
911+
'exception' => $ex,
912+
]);
913+
return false;
914+
}
915+
916+
if ($oldToken->getUID() !== $user->getUID()) {
917+
$this->logger->warning('Tried to renew a session token belonging to a different user', [
918+
'app' => 'core',
919+
'user' => $uid,
920+
]);
921+
return false;
922+
}
923+
904924
// replace successfully used token with a new one
905925
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
906926
$newToken = $this->random->generate(32);

tests/lib/User/SessionTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,14 @@ public function testRememberLoginValidToken(): void {
731731
->with($oldSessionId, $sessionId)
732732
->willReturn($tokenObject);
733733

734-
$this->tokenProvider->expects($this->never())
735-
->method('getToken');
734+
$oldTokenObject = $this->createMock(IToken::class);
735+
$oldTokenObject->expects($this->once())
736+
->method('getUID')
737+
->willReturn('foo');
738+
739+
$this->tokenProvider->expects($this->once())
740+
->method('getToken')
741+
->willReturn($oldTokenObject);
736742

737743
$user->expects($this->any())
738744
->method('getUID')
@@ -809,7 +815,16 @@ public function testRememberLoginInvalidSessionToken(): void {
809815
->with($oldSessionId, $sessionId)
810816
->willThrowException(new InvalidTokenException());
811817

812-
$user->expects($this->never())
818+
$oldTokenObject = $this->createMock(IToken::class);
819+
$oldTokenObject->expects($this->once())
820+
->method('getUID')
821+
->willReturn('foo');
822+
823+
$this->tokenProvider->expects($this->once())
824+
->method('getToken')
825+
->willReturn($oldTokenObject);
826+
827+
$user->expects($this->once())
813828
->method('getUID')
814829
->willReturn('foo');
815830
$userSession->expects($this->never())

0 commit comments

Comments
 (0)