Skip to content

Commit e697d00

Browse files
authored
Merge pull request #63877 from nextcloud/backport/63730/stable22
[stable22] fix: Check rememberme cookie previous session id matches uid
2 parents 2473e53 + c19928a commit e697d00

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
if (!in_array($currentToken, $tokens, true)) {
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
@@ -652,8 +652,14 @@ public function testRememberLoginValidToken() {
652652
->with($oldSessionId, $sessionId)
653653
->willReturn($tokenObject);
654654

655-
$this->tokenProvider->expects($this->never())
656-
->method('getToken');
655+
$oldTokenObject = $this->createMock(IToken::class);
656+
$oldTokenObject->expects($this->once())
657+
->method('getUID')
658+
->willReturn('foo');
659+
660+
$this->tokenProvider->expects($this->once())
661+
->method('getToken')
662+
->willReturn($oldTokenObject);
657663

658664
$user->expects($this->any())
659665
->method('getUID')
@@ -730,7 +736,16 @@ public function testRememberLoginInvalidSessionToken() {
730736
->with($oldSessionId, $sessionId)
731737
->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException()));
732738

733-
$user->expects($this->never())
739+
$oldTokenObject = $this->createMock(IToken::class);
740+
$oldTokenObject->expects($this->once())
741+
->method('getUID')
742+
->willReturn('foo');
743+
744+
$this->tokenProvider->expects($this->once())
745+
->method('getToken')
746+
->willReturn($oldTokenObject);
747+
748+
$user->expects($this->once())
734749
->method('getUID')
735750
->willReturn('foo');
736751
$userSession->expects($this->never())

0 commit comments

Comments
 (0)