From 15d8cc1e36f72dc37c19bc6b8d4b8694cd0aa310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 2 Aug 2022 12:11:15 +0200 Subject: [PATCH 1/3] Improve typing in FixEncryptedVersion command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Command/FixEncryptedVersion.php | 36 +++++++------------ 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index 073c1f1438a88..b3c0d8c4d1692 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -94,11 +94,6 @@ protected function configure(): void { ); } - /** - * @param InputInterface $input - * @param OutputInterface $output - * @return int - */ protected function execute(InputInterface $input, OutputInterface $output): int { $skipSignatureCheck = $this->config->getSystemValue('encryption_skip_signature_check', false); $this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false); @@ -121,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $pathToWalk = "$pathToWalk/$pathOption"; } - if ($user === null) { + if ($user === '') { $output->writeln("No user id provided.\n"); return 1; } @@ -134,12 +129,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int } /** - * @param string $user - * @param string $path - * @param OutputInterface $output * @return int 0 for success, 1 for error */ - private function walkPathOfUser($user, $path, OutputInterface $output): int { + private function walkPathOfUser(string $user, string $path, OutputInterface $output): int { $this->setupUserFs($user); if (!$this->view->file_exists($path)) { $output->writeln("Path \"$path\" does not exist. Please provide a valid path."); @@ -169,11 +161,9 @@ private function walkPathOfUser($user, $path, OutputInterface $output): int { } /** - * @param string $path - * @param OutputInterface $output * @param bool $ignoreCorrectEncVersionCall, setting this variable to false avoids recursion */ - private function verifyFileContent($path, OutputInterface $output, $ignoreCorrectEncVersionCall = true): bool { + private function verifyFileContent(string $path, OutputInterface $output, bool $ignoreCorrectEncVersionCall = true): bool { try { /** * In encryption, the files are read in a block size of 8192 bytes @@ -201,9 +191,9 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec return false; } catch (HintException $e) { $this->logger->warning("Issue: " . $e->getMessage()); - //If allowOnce is set to false, this becomes recursive. + // If allowOnce is set to false, this becomes recursive. if ($ignoreCorrectEncVersionCall === true) { - //Lets rectify the file by correcting encrypted version + // Lets rectify the file by correcting encrypted version $output->writeln("Attempting to fix the path: \"$path\""); return $this->correctEncryptedVersion($path, $output); } @@ -212,18 +202,19 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec } /** - * @param string $path - * @param OutputInterface $output * @param bool $includeZero whether to try zero version for unencrypted file - * @return bool */ - private function correctEncryptedVersion($path, OutputInterface $output, bool $includeZero = false): bool { + private function correctEncryptedVersion(string $path, OutputInterface $output, bool $includeZero = false): bool { $fileInfo = $this->view->getFileInfo($path); if (!$fileInfo) { $output->writeln("File info not found for file: \"$path\""); return true; } $fileId = $fileInfo->getId(); + if ($fileId === null) { + $output->writeln("File info contains no id for file: \"$path\""); + return true; + } $encryptedVersion = $fileInfo->getEncryptedVersion(); $wrongEncryptedVersion = $encryptedVersion; @@ -255,7 +246,7 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i } } - //test by decrementing the value till 1 and if nothing works try incrementing + // Test by decrementing the value till 1 and if nothing works try incrementing $encryptedVersion--; while ($encryptedVersion > 0) { $cacheInfo = ['encryptedVersion' => $encryptedVersion, 'encrypted' => $encryptedVersion]; @@ -268,7 +259,7 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i $encryptedVersion--; } - //So decrementing did not work. Now lets increment. Max increment is till 5 + // So decrementing did not work. Now lets increment. Max increment is till 5 $increment = 1; while ($increment <= 5) { /** @@ -301,9 +292,8 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i /** * Setup user file system - * @param string $uid */ - private function setupUserFs($uid): void { + private function setupUserFs(string $uid): void { \OC_Util::tearDownFS(); \OC_Util::setupFS($uid); } From 6090807d0b44bddd30473f875dcb9002b79dd169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 2 Aug 2022 12:47:26 +0200 Subject: [PATCH 2/3] Fix encryption:fix-encrypted-version command when encrypted is set to 0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../lib/Command/FixEncryptedVersion.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index b3c0d8c4d1692..d4c5eddbfe5ab 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -175,6 +175,22 @@ private function verifyFileContent(string $path, OutputInterface $output, bool $ $handle = $this->view->fopen($path, 'rb'); if (\fread($handle, 9001) !== false) { + $fileInfo = $this->view->getFileInfo($path); + if (!$fileInfo) { + $output->writeln("File info not found for file: \"$path\""); + return true; + } + $encryptedVersion = $fileInfo->getEncryptedVersion(); + $stat = $this->view->stat($path); + if (($encryptedVersion == 0) && isset($stat['hasHeader']) && ($stat['hasHeader'] == true)) { + // The file has encrypted to false but has an encryption header + if ($ignoreCorrectEncVersionCall === true) { + // Lets rectify the file by correcting encrypted version + $output->writeln("Attempting to fix the path: \"$path\""); + return $this->correctEncryptedVersion($path, $output); + } + return false; + } $output->writeln("The file \"$path\" is: OK"); } From 0c1d0ea1fd4d90de3f9fbc6f36e92c9526fed307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 4 Aug 2022 10:06:36 +0200 Subject: [PATCH 3/3] Adapt test to fixed command output. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit No user and non-existing user are now correctly treated as two separated cases Signed-off-by: Côme Chilliet --- .../tests/Command/FixEncryptedVersionTest.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/encryption/tests/Command/FixEncryptedVersionTest.php b/apps/encryption/tests/Command/FixEncryptedVersionTest.php index 14143223264dd..ee9ad1ac89fa2 100644 --- a/apps/encryption/tests/Command/FixEncryptedVersionTest.php +++ b/apps/encryption/tests/Command/FixEncryptedVersionTest.php @@ -333,9 +333,6 @@ public function testExecuteWithDirectoryPathOption() { $this->assertStringNotContainsString('world.txt', $output); } - /** - * Test commands with a directory path - */ public function testExecuteWithNoUser() { $this->util->expects($this->once())->method('isMasterKeyEnabled') ->willReturn(true); @@ -347,6 +344,20 @@ public function testExecuteWithNoUser() { $output = $this->commandTester->getDisplay(); + $this->assertStringContainsString('No user id provided', $output); + } + + public function testExecuteWithBadUser() { + $this->util->expects($this->once())->method('isMasterKeyEnabled') + ->willReturn(true); + + $this->commandTester->execute([ + 'user' => 'nonexisting', + '--path' => "/" + ]); + + $output = $this->commandTester->getDisplay(); + $this->assertStringContainsString('does not exist', $output); }