Skip to content

Commit d7889de

Browse files
come-ncbackportbot[bot]
authored andcommitted
fix: Check rememberme cookie previous session id matches uid
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent d6273e6 commit d7889de

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
@@ -900,6 +900,26 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
900900
]);
901901
return false;
902902
}
903+
904+
try {
905+
$oldToken = $this->tokenProvider->getToken($oldSessionId);
906+
} catch (InvalidTokenException $ex) {
907+
$this->logger->error('Could not find the session token to renew', [
908+
'app' => 'core',
909+
'user' => $uid,
910+
'exception' => $ex,
911+
]);
912+
return false;
913+
}
914+
915+
if ($oldToken->getUID() !== $user->getUID()) {
916+
$this->logger->warning('Tried to renew a session token belonging to a different user', [
917+
'app' => 'core',
918+
'user' => $uid,
919+
]);
920+
return false;
921+
}
922+
903923
// replace successfully used token with a new one
904924
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
905925
$newToken = $this->random->generate(32);

tests/lib/User/SessionTest.php

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

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

738744
$user->expects($this->any())
739745
->method('getUID')
@@ -810,7 +816,16 @@ public function testRememberLoginInvalidSessionToken(): void {
810816
->with($oldSessionId, $sessionId)
811817
->will($this->throwException(new InvalidTokenException()));
812818

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

0 commit comments

Comments
 (0)