Skip to content

Commit 8eac60a

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 7a06163 commit 8eac60a

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
@@ -920,6 +920,26 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
920920
]);
921921
return false;
922922
}
923+
924+
try {
925+
$oldToken = $this->tokenProvider->getToken($oldSessionId);
926+
} catch (InvalidTokenException $ex) {
927+
$this->logger->error('Could not find the session token to renew', [
928+
'app' => 'core',
929+
'user' => $uid,
930+
'exception' => $ex,
931+
]);
932+
return false;
933+
}
934+
935+
if ($oldToken->getUID() !== $user->getUID()) {
936+
$this->logger->warning('Tried to renew a session token belonging to a different user', [
937+
'app' => 'core',
938+
'user' => $uid,
939+
]);
940+
return false;
941+
}
942+
923943
// replace successfully used token with a new one
924944
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
925945
$newToken = $this->random->generate(32);

tests/lib/User/SessionTest.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,14 @@ public function testRememberLoginValidToken() {
564564
->with($oldSessionId, $sessionId)
565565
->willReturn($tokenObject);
566566

567-
$this->tokenProvider->expects($this->never())
568-
->method('getToken');
567+
$oldTokenObject = $this->createMock(IToken::class);
568+
$oldTokenObject->expects($this->once())
569+
->method('getUID')
570+
->willReturn('foo');
571+
572+
$this->tokenProvider->expects($this->once())
573+
->method('getToken')
574+
->willReturn($oldTokenObject);
569575

570576
$user->expects($this->any())
571577
->method('getUID')
@@ -642,7 +648,16 @@ public function testRememberLoginInvalidSessionToken() {
642648
->with($oldSessionId, $sessionId)
643649
->will($this->throwException(new InvalidTokenException()));
644650

645-
$user->expects($this->never())
651+
$oldTokenObject = $this->createMock(IToken::class);
652+
$oldTokenObject->expects($this->once())
653+
->method('getUID')
654+
->willReturn('foo');
655+
656+
$this->tokenProvider->expects($this->once())
657+
->method('getToken')
658+
->willReturn($oldTokenObject);
659+
660+
$user->expects($this->once())
646661
->method('getUID')
647662
->willReturn('foo');
648663
$userSession->expects($this->never())

0 commit comments

Comments
 (0)