From 653a09eecb51f779ec12f6d1f3b581ec46cadac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 3 Nov 2026 16:23:11 +0100 Subject: [PATCH 1/4] fix: Clear session on login when a user was in session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/User/Session.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/private/User/Session.php b/lib/private/User/Session.php index 1edf8f4282cb5..09f78f250ad65 100644 --- a/lib/private/User/Session.php +++ b/lib/private/User/Session.php @@ -157,11 +157,16 @@ public function setSession(ISession $session) { * @param IUser|null $user */ #[\Override] - public function setUser($user) { - if (is_null($user)) { + public function setUser($user): void { + $previousUserId = $this->session->get('user_id'); + $newUserId = $user?->getUID(); + if ($previousUserId !== null && $previousUserId !== $newUserId) { + $this->session->clear(); + } + if (is_null($newUserId)) { $this->session->remove('user_id'); } else { - $this->session->set('user_id', $user->getUID()); + $this->session->set('user_id', $newUserId); } $this->activeUser = $user; } From bb4aac76a9c3afd65d2644a8e180fc2d6ef5cd92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 1 Dec 2026 18:19:49 +0100 Subject: [PATCH 2/4] fix: Fix encryption tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../Sabre/RequestTest/EncryptionUploadTest.php | 12 +++++------- .../Sabre/RequestTest/RequestTestCase.php | 6 +++++- .../Connector/Sabre/RequestTest/UploadTest.php | 4 +++- tests/lib/Traits/EncryptionTrait.php | 15 ++++++++------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php index c1ae71ac28f74..6cf5b6c9bab38 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php @@ -10,7 +10,6 @@ use OC\Files\View; use OCP\IConfig; -use OCP\ITempManager; use OCP\Server; use Test\Traits\EncryptionTrait; @@ -25,13 +24,12 @@ class EncryptionUploadTest extends UploadTest { use EncryptionTrait; protected function setupUser($name, $password): View { - $this->createUser($name, $password); - $tmpFolder = Server::get(ITempManager::class)->getTemporaryFolder(); - $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]); - // we use per-user keys Server::get(IConfig::class)->setAppValue('encryption', 'useMasterKey', '0'); - $this->setupForUser($name, $password); + return parent::setupUser($name, $password); + } + + protected function loginUser(string $name, string $password): void { $this->loginWithEncryption($name); - return new View('/' . $name . '/files'); + $this->setupForUser($name, $password); } } diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php index 404dc7fa5d7b4..f7f2510873454 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/RequestTestCase.php @@ -61,10 +61,14 @@ protected function setupUser($name, $password): View { $this->createUser($name, $password); $tmpFolder = \OCP\Server::get(ITempManager::class)->getTemporaryFolder(); $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]); - self::loginAsUser($name); + $this->loginUser($name, $password); return new View('/' . $name . '/files'); } + protected function loginUser(string $user, string $password): void { + self::loginAsUser($user); + } + /** * @param View $view the view to run the webdav server against * @param string $user diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php index 7432add82a85d..70ee90b7babe0 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/UploadTest.php @@ -68,7 +68,9 @@ public function testUploadOverWriteReadLocked(): void { public function testUploadOverWriteWriteLocked(): void { $user = self::getUniqueID(); $view = $this->setupUser($user, 'pass'); - $this->loginAsUser($user); + // Avoid first login event later + $userObject = \OCP\Server::get(\OCP\IUserManager::class)->get($user); + $userObject->updateLastLoginTimestamp(); $view->file_put_contents('foo.txt', 'bar'); diff --git a/tests/lib/Traits/EncryptionTrait.php b/tests/lib/Traits/EncryptionTrait.php index 2dc9213ff2431..656e22d65aaa8 100644 --- a/tests/lib/Traits/EncryptionTrait.php +++ b/tests/lib/Traits/EncryptionTrait.php @@ -96,6 +96,13 @@ protected function postLogin() { } protected function setUpEncryptionTrait() { + $this->config = Server::get(IConfig::class); + $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); + $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module'); + + $this->config->setAppValue('core', 'encryption_enabled', 'yes'); + $this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled()); + $isReady = Server::get(\OCP\Encryption\IManager::class)->isReady(); if (!$isReady) { $this->markTestSkipped('Encryption not ready'); @@ -105,15 +112,9 @@ protected function setUpEncryptionTrait() { $this->setupManager = Server::get(SetupManager::class); Server::get(IAppManager::class)->loadApp('encryption'); + $this->config->setAppValue('core', 'default_encryption_module', Encryption::ID); $this->encryptionApp = new Application([], $isReady); - - $this->config = Server::get(IConfig::class); - $this->encryptionWasEnabled = $this->config->getAppValue('core', 'encryption_enabled', 'no'); - $this->originalEncryptionModule = $this->config->getAppValue('core', 'default_encryption_module'); - $this->config->setAppValue('core', 'default_encryption_module', Encryption::ID); - $this->config->setAppValue('core', 'encryption_enabled', 'yes'); - $this->assertTrue(Server::get(\OCP\Encryption\IManager::class)->isEnabled()); } protected function tearDownEncryptionTrait() { From 23249d6ec0edc05de6391e6535c5fc7c76957c82 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 2 Dec 2026 13:50:22 +0100 Subject: [PATCH 3/4] test(files_sharing): Restore encryption keys after switching user Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Carl Schwan --- .../tests/EncryptedSizePropagationTest.php | 8 ++------ .../tests/SizePropagationTest.php | 18 +++++++++++------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/apps/files_sharing/tests/EncryptedSizePropagationTest.php b/apps/files_sharing/tests/EncryptedSizePropagationTest.php index 92773bdc72d78..889bdc8cf27ae 100644 --- a/apps/files_sharing/tests/EncryptedSizePropagationTest.php +++ b/apps/files_sharing/tests/EncryptedSizePropagationTest.php @@ -7,7 +7,6 @@ */ namespace OCA\Files_Sharing\Tests; -use OC\Files\View; use OCP\ITempManager; use OCP\Server; use Test\Traits\EncryptionTrait; @@ -21,12 +20,9 @@ protected function setUp(): void { $this->config->setAppValue('encryption', 'useMasterKey', '0'); } - protected function setupUser($name, $password = '') { - $this->createUser($name, $password); - $this->registerMountForUser($name); - $this->setupForUser($name, $password); + protected function loginUser(string $name): void { $this->loginWithEncryption($name); - return new View('/' . $name . '/files'); + $this->setupForUser($name, ''); } private function registerMountForUser($user): void { diff --git a/apps/files_sharing/tests/SizePropagationTest.php b/apps/files_sharing/tests/SizePropagationTest.php index 9b2f82cc99e0d..44a2b9f5859b0 100644 --- a/apps/files_sharing/tests/SizePropagationTest.php +++ b/apps/files_sharing/tests/SizePropagationTest.php @@ -28,10 +28,14 @@ protected function setupUser($name, $password = '') { $this->createUser($name, $password); $tmpFolder = Server::get(ITempManager::class)->getTemporaryFolder(); $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]); - $this->loginAsUser($name); + $this->loginUser($name); return new View('/' . $name . '/files'); } + protected function loginUser(string $name): void { + $this->loginAsUser($name); + } + public function testSizePropagationWhenOwnerChangesFile(): void { $recipientView = $this->setupUser(self::TEST_FILES_SHARING_API_USER1); @@ -48,21 +52,21 @@ public function testSizePropagationWhenOwnerChangesFile(): void { ); $ownerRootInfo = $ownerView->getFileInfo('', false); - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); + $this->loginUser(self::TEST_FILES_SHARING_API_USER1); $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt')); $recipientRootInfo = $recipientView->getFileInfo('', false); // when file changed as owner - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); + $this->loginUser(self::TEST_FILES_SHARING_API_USER2); $ownerView->file_put_contents('/sharedfolder/subfolder/foo.txt', 'foobar'); // size of recipient's root stays the same - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); + $this->loginUser(self::TEST_FILES_SHARING_API_USER1); $newRecipientRootInfo = $recipientView->getFileInfo('', false); $this->assertEquals($recipientRootInfo->getSize(), $newRecipientRootInfo->getSize()); // size of owner's root increases - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); + $this->loginUser(self::TEST_FILES_SHARING_API_USER2); $newOwnerRootInfo = $ownerView->getFileInfo('', false); $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize()); } @@ -83,7 +87,7 @@ public function testSizePropagationWhenRecipientChangesFile(): void { ); $ownerRootInfo = $ownerView->getFileInfo('', false); - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER1); + $this->loginUser(self::TEST_FILES_SHARING_API_USER1); $this->assertTrue($recipientView->file_exists('/sharedfolder/subfolder/foo.txt')); $recipientRootInfo = $recipientView->getFileInfo('', false); $recipientRootInfoWithMounts = $recipientView->getFileInfo('', true); @@ -101,7 +105,7 @@ public function testSizePropagationWhenRecipientChangesFile(): void { $this->assertEquals($oldRecipientSize + 3, $newRecipientRootInfo->getSize()); // size of owner's root increases - $this->loginAsUser(self::TEST_FILES_SHARING_API_USER2); + $this->loginUser(self::TEST_FILES_SHARING_API_USER2); $newOwnerRootInfo = $ownerView->getFileInfo('', false); $this->assertEquals($ownerRootInfo->getSize() + 3, $newOwnerRootInfo->getSize()); } From 42014141edd056940b58c60ce14fd0450006e151 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 2 Dec 2026 13:50:26 +0100 Subject: [PATCH 4/4] test(dav): Init encryption keys on login in EncryptionUploadTest Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Carl Schwan --- .../RequestTest/EncryptionUploadTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php b/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php index 6cf5b6c9bab38..97ffe6fb2d685 100644 --- a/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/RequestTest/EncryptionUploadTest.php @@ -9,8 +9,11 @@ namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest; use OC\Files\View; +use OCA\Encryption\Listeners\UserEventsListener; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; use OCP\Server; +use OCP\User\Events\UserLoggedInEvent; use Test\Traits\EncryptionTrait; /** @@ -23,6 +26,22 @@ class EncryptionUploadTest extends UploadTest { use EncryptionTrait; + /** @var callable */ + private $loginListener; + + protected function setUp(): void { + parent::setUp(); + + // Restore the user's keys on DAV login, as the encryption app does in production + $this->loginListener = [$this->encryptionApp->getContainer()->get(UserEventsListener::class), 'handle']; + Server::get(IEventDispatcher::class)->addListener(UserLoggedInEvent::class, $this->loginListener); + } + + protected function tearDown(): void { + Server::get(IEventDispatcher::class)->removeListener(UserLoggedInEvent::class, $this->loginListener); + parent::tearDown(); + } + protected function setupUser($name, $password): View { Server::get(IConfig::class)->setAppValue('encryption', 'useMasterKey', '0'); return parent::setupUser($name, $password);