Skip to content

Commit 83a5397

Browse files
Merge pull request #63719 from nextcloud/backport/63705/stable34
[stable34] fix: Check rememberme cookie previous session id matches uid
2 parents a7f2a0e + 0d0552c commit 83a5397

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
@@ -891,6 +891,26 @@ public function loginWithCookie($uid, $currentToken, $oldSessionId) {
891891
]);
892892
return false;
893893
}
894+
895+
try {
896+
$oldToken = $this->tokenProvider->getToken($oldSessionId);
897+
} catch (InvalidTokenException $ex) {
898+
$this->logger->error('Could not find the session token to renew', [
899+
'app' => 'core',
900+
'user' => $uid,
901+
'exception' => $ex,
902+
]);
903+
return false;
904+
}
905+
906+
if ($oldToken->getUID() !== $user->getUID()) {
907+
$this->logger->warning('Tried to renew a session token belonging to a different user', [
908+
'app' => 'core',
909+
'user' => $uid,
910+
]);
911+
return false;
912+
}
913+
894914
// replace successfully used token with a new one
895915
$this->config->deleteUserValue($uid, 'login_token', $currentToken);
896916
$newToken = $this->random->generate(32);

‎tests/lib/User/SessionTest.php‎

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

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

737743
$user->expects($this->any())
738744
->method('getUID')
@@ -809,7 +815,16 @@ public function testRememberLoginInvalidSessionToken(): void {
809815
->with($oldSessionId, $sessionId)
810816
->willThrowException(new InvalidTokenException());
811817

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

0 commit comments

Comments
 (0)