diff --git a/lib/private/Session/Internal.php b/lib/private/Session/Internal.php index 953544e374ca5..f473da6b39273 100644 --- a/lib/private/Session/Internal.php +++ b/lib/private/Session/Internal.php @@ -11,6 +11,7 @@ use OC\Authentication\Token\IProvider; use OC\Diagnostics\TLogSlowOperation; +use OC\User\Session as UserSession; use OCP\Authentication\Exceptions\InvalidTokenException; use OCP\Server; use OCP\Session\Exceptions\SessionNotAvailableException; @@ -148,6 +149,7 @@ public function regenerateId(bool $deleteOldSession = true, bool $updateToken = try { $tokenProvider->renewSessionToken($oldId, $newId); + Server::get(UserSession::class)->renewMagicSessionId($oldId); } catch (InvalidTokenException $e) { // Just ignore } diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 4395a6ee5020e..6516d4177d3a7 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -1049,6 +1049,21 @@ public function setMagicInCookie($username, $token) { } } + /** + * Point the remember-me cookie at the regenerated session id, so cookie + * login can still find the token that was renewed along with it. + */ + public function renewMagicSessionId(string $oldSessionId): void { + $request = Server::get(IRequest::class); + $username = $request->getCookie('nc_username'); + $token = $request->getCookie('nc_token'); + $sessionId = $request->getCookie('nc_session_id'); + if ($username === null || $token === null || $sessionId !== $oldSessionId) { + return; + } + $this->setMagicInCookie($username, $token); + } + /** * Remove cookie for "remember username" */ diff --git a/lib/private/legacy/OC_User.php b/lib/private/legacy/OC_User.php index 6375afd3ab391..9a50765c2f80f 100644 --- a/lib/private/legacy/OC_User.php +++ b/lib/private/legacy/OC_User.php @@ -174,7 +174,7 @@ public static function loginWithApache(IApacheBackend $backend): bool { /** @var IEventDispatcher $dispatcher */ $dispatcher->dispatchTyped(new BeforeUserLoggedInEvent($uid, $password, $backend)); - $userSession->createSessionToken($request, $uid, $uid, $password); + $userSession->createSessionToken($request, $uid, $uid, $password, IToken::REMEMBER); $userSession->createRememberMeToken($userSession->getUser()); if (empty($password)) { diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 042a9fc634739..cb8413800d162 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -1148,6 +1148,27 @@ public function testCreateRememberMeToken(): void { $this->userSession->createRememberMeToken($user); } + public static function renewMagicSessionIdData(): array { + return [ + 'cookie holds the old session id' => [['nc_username' => 'u', 'nc_token' => 't', 'nc_session_id' => 'old'], true], + 'cookie holds another session id' => [['nc_username' => 'u', 'nc_token' => 't', 'nc_session_id' => 'other'], false], + 'no remember-me cookies' => [[], false], + ]; + } + + #[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'renewMagicSessionIdData')] + public function testRenewMagicSessionId(array $cookies, bool $expectRenewal): void { + $this->userSession->expects($expectRenewal ? $this->once() : $this->never()) + ->method('setMagicInCookie') + ->with('u', 't'); + + $request = $this->createMock(IRequest::class); + $request->method('getCookie')->willReturnCallback(fn (string $key) => $cookies[$key] ?? null); + $this->overwriteService(IRequest::class, $request); + + $this->userSession->renewMagicSessionId('old'); + } + public function testTryBasicAuthLoginValid(): void { $request = $this->createMock(Request::class); $request->method('__get')