Skip to content

Commit 37cf5e9

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 9d05327 commit 37cf5e9

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
@@ -929,6 +929,26 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
929929
]);
930930
return false;
931931
}
932+
933+
try {
934+
$oldToken = $this->tokenProvider->getToken($oldSessionId);
935+
} catch (InvalidTokenException $ex) {
936+
$this->logger->error('Could not find the session token to renew', [
937+
'app' => 'core',
938+
'user' => $uid,
939+
'exception' => $ex,
940+
]);
941+
return false;
942+
}
943+
944+
if ($oldToken->getUID() !== $user->getUID()) {
945+
$this->logger->warning('Tried to renew a session token belonging to a different user', [
946+
'app' => 'core',
947+
'user' => $uid,
948+
]);
949+
return false;
950+
}
951+
932952
// replace successfully used token with a new one
933953
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
934954
$newToken = $this->random->generate(32);

tests/lib/User/SessionTest.php

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

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

739745
$user->expects($this->any())
740746
->method('getUID')
@@ -811,7 +817,16 @@ public function testRememberLoginInvalidSessionToken() {
811817
->with($oldSessionId, $sessionId)
812818
->will($this->throwException(new InvalidTokenException()));
813819

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

0 commit comments

Comments
 (0)