Skip to content

Commit 52ebee3

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 0a1ca29 commit 52ebee3

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

tests/lib/User/SessionTest.php

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

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

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

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

0 commit comments

Comments
 (0)