diff options
author | Côme Chilliet <91878298+come-nc@users.noreply.github.com> | 2022-08-22 11:11:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-22 11:11:32 +0200 |
commit | b48a87e6e0c6f1e7bb447ea44a7be380fde96c99 (patch) | |
tree | 95b54338fd206acc34d2bdcbe2d32cbe73454478 /apps/encryption | |
parent | b10c3a5f482647b359507a56eeb9b06b6c4f71c7 (diff) | |
parent | 73702e6350355b6961f92925fc411840b837c340 (diff) | |
download | nextcloud-server-b48a87e6e0c6f1e7bb447ea44a7be380fde96c99.tar.gz nextcloud-server-b48a87e6e0c6f1e7bb447ea44a7be380fde96c99.zip |
Merge pull request #33433 from nextcloud/fix/add-option-to-fix-encrypted
Fix encryption:fix-encrypted-version command when encrypted is set to 0
Diffstat (limited to 'apps/encryption')
-rw-r--r-- | apps/encryption/lib/Command/FixEncryptedVersion.php | 52 | ||||
-rw-r--r-- | apps/encryption/tests/Command/FixEncryptedVersionTest.php | 17 |
2 files changed, 43 insertions, 26 deletions
diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index 073c1f1438a..d4c5eddbfe5 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -94,11 +94,6 @@ class FixEncryptedVersion extends Command { ); } - /** - * @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 @@ class FixEncryptedVersion extends Command { $pathToWalk = "$pathToWalk/$pathOption"; } - if ($user === null) { + if ($user === '') { $output->writeln("<error>No user id provided.</error>\n"); return 1; } @@ -134,12 +129,9 @@ class FixEncryptedVersion extends Command { } /** - * @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("<error>Path \"$path\" does not exist. Please provide a valid path.</error>"); @@ -169,11 +161,9 @@ class FixEncryptedVersion extends Command { } /** - * @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 @@ -185,6 +175,22 @@ class FixEncryptedVersion extends Command { $handle = $this->view->fopen($path, 'rb'); if (\fread($handle, 9001) !== false) { + $fileInfo = $this->view->getFileInfo($path); + if (!$fileInfo) { + $output->writeln("<warning>File info not found for file: \"$path\"</warning>"); + 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("<info>Attempting to fix the path: \"$path\"</info>"); + return $this->correctEncryptedVersion($path, $output); + } + return false; + } $output->writeln("<info>The file \"$path\" is: OK</info>"); } @@ -201,9 +207,9 @@ class FixEncryptedVersion extends Command { 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("<info>Attempting to fix the path: \"$path\"</info>"); return $this->correctEncryptedVersion($path, $output); } @@ -212,18 +218,19 @@ class FixEncryptedVersion extends Command { } /** - * @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("<warning>File info not found for file: \"$path\"</warning>"); return true; } $fileId = $fileInfo->getId(); + if ($fileId === null) { + $output->writeln("<warning>File info contains no id for file: \"$path\"</warning>"); + return true; + } $encryptedVersion = $fileInfo->getEncryptedVersion(); $wrongEncryptedVersion = $encryptedVersion; @@ -255,7 +262,7 @@ class FixEncryptedVersion extends Command { } } - //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 +275,7 @@ class FixEncryptedVersion extends Command { $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 +308,8 @@ class FixEncryptedVersion extends Command { /** * 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); } diff --git a/apps/encryption/tests/Command/FixEncryptedVersionTest.php b/apps/encryption/tests/Command/FixEncryptedVersionTest.php index 14143223264..ee9ad1ac89f 100644 --- a/apps/encryption/tests/Command/FixEncryptedVersionTest.php +++ b/apps/encryption/tests/Command/FixEncryptedVersionTest.php @@ -333,9 +333,6 @@ The file \"/$this->userId/files/sub/hello.txt\" is: OK", $output); $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 @@ The file \"/$this->userId/files/sub/hello.txt\" is: OK", $output); $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); } |