Skip to content

Commit b48a87e

Browse files
authored
Merge pull request #33433 from nextcloud/fix/add-option-to-fix-encrypted
Fix encryption:fix-encrypted-version command when encrypted is set to 0
2 parents b10c3a5 + 73702e6 commit b48a87e

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

apps/encryption/lib/Command/FixEncryptedVersion.php

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,6 @@ protected function configure(): void {
9494
);
9595
}
9696

97-
/**
98-
* @param InputInterface $input
99-
* @param OutputInterface $output
100-
* @return int
101-
*/
10297
protected function execute(InputInterface $input, OutputInterface $output): int {
10398
$skipSignatureCheck = $this->config->getSystemValue('encryption_skip_signature_check', false);
10499
$this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false);
@@ -121,7 +116,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
121116
$pathToWalk = "$pathToWalk/$pathOption";
122117
}
123118

124-
if ($user === null) {
119+
if ($user === '') {
125120
$output->writeln("<error>No user id provided.</error>\n");
126121
return 1;
127122
}
@@ -134,12 +129,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
134129
}
135130

136131
/**
137-
* @param string $user
138-
* @param string $path
139-
* @param OutputInterface $output
140132
* @return int 0 for success, 1 for error
141133
*/
142-
private function walkPathOfUser($user, $path, OutputInterface $output): int {
134+
private function walkPathOfUser(string $user, string $path, OutputInterface $output): int {
143135
$this->setupUserFs($user);
144136
if (!$this->view->file_exists($path)) {
145137
$output->writeln("<error>Path \"$path\" does not exist. Please provide a valid path.</error>");
@@ -169,11 +161,9 @@ private function walkPathOfUser($user, $path, OutputInterface $output): int {
169161
}
170162

171163
/**
172-
* @param string $path
173-
* @param OutputInterface $output
174164
* @param bool $ignoreCorrectEncVersionCall, setting this variable to false avoids recursion
175165
*/
176-
private function verifyFileContent($path, OutputInterface $output, $ignoreCorrectEncVersionCall = true): bool {
166+
private function verifyFileContent(string $path, OutputInterface $output, bool $ignoreCorrectEncVersionCall = true): bool {
177167
try {
178168
/**
179169
* In encryption, the files are read in a block size of 8192 bytes
@@ -185,6 +175,22 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
185175
$handle = $this->view->fopen($path, 'rb');
186176

187177
if (\fread($handle, 9001) !== false) {
178+
$fileInfo = $this->view->getFileInfo($path);
179+
if (!$fileInfo) {
180+
$output->writeln("<warning>File info not found for file: \"$path\"</warning>");
181+
return true;
182+
}
183+
$encryptedVersion = $fileInfo->getEncryptedVersion();
184+
$stat = $this->view->stat($path);
185+
if (($encryptedVersion == 0) && isset($stat['hasHeader']) && ($stat['hasHeader'] == true)) {
186+
// The file has encrypted to false but has an encryption header
187+
if ($ignoreCorrectEncVersionCall === true) {
188+
// Lets rectify the file by correcting encrypted version
189+
$output->writeln("<info>Attempting to fix the path: \"$path\"</info>");
190+
return $this->correctEncryptedVersion($path, $output);
191+
}
192+
return false;
193+
}
188194
$output->writeln("<info>The file \"$path\" is: OK</info>");
189195
}
190196

@@ -201,9 +207,9 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
201207
return false;
202208
} catch (HintException $e) {
203209
$this->logger->warning("Issue: " . $e->getMessage());
204-
//If allowOnce is set to false, this becomes recursive.
210+
// If allowOnce is set to false, this becomes recursive.
205211
if ($ignoreCorrectEncVersionCall === true) {
206-
//Lets rectify the file by correcting encrypted version
212+
// Lets rectify the file by correcting encrypted version
207213
$output->writeln("<info>Attempting to fix the path: \"$path\"</info>");
208214
return $this->correctEncryptedVersion($path, $output);
209215
}
@@ -212,18 +218,19 @@ private function verifyFileContent($path, OutputInterface $output, $ignoreCorrec
212218
}
213219

214220
/**
215-
* @param string $path
216-
* @param OutputInterface $output
217221
* @param bool $includeZero whether to try zero version for unencrypted file
218-
* @return bool
219222
*/
220-
private function correctEncryptedVersion($path, OutputInterface $output, bool $includeZero = false): bool {
223+
private function correctEncryptedVersion(string $path, OutputInterface $output, bool $includeZero = false): bool {
221224
$fileInfo = $this->view->getFileInfo($path);
222225
if (!$fileInfo) {
223226
$output->writeln("<warning>File info not found for file: \"$path\"</warning>");
224227
return true;
225228
}
226229
$fileId = $fileInfo->getId();
230+
if ($fileId === null) {
231+
$output->writeln("<warning>File info contains no id for file: \"$path\"</warning>");
232+
return true;
233+
}
227234
$encryptedVersion = $fileInfo->getEncryptedVersion();
228235
$wrongEncryptedVersion = $encryptedVersion;
229236

@@ -255,7 +262,7 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i
255262
}
256263
}
257264

258-
//test by decrementing the value till 1 and if nothing works try incrementing
265+
// Test by decrementing the value till 1 and if nothing works try incrementing
259266
$encryptedVersion--;
260267
while ($encryptedVersion > 0) {
261268
$cacheInfo = ['encryptedVersion' => $encryptedVersion, 'encrypted' => $encryptedVersion];
@@ -268,7 +275,7 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i
268275
$encryptedVersion--;
269276
}
270277

271-
//So decrementing did not work. Now lets increment. Max increment is till 5
278+
// So decrementing did not work. Now lets increment. Max increment is till 5
272279
$increment = 1;
273280
while ($increment <= 5) {
274281
/**
@@ -301,9 +308,8 @@ private function correctEncryptedVersion($path, OutputInterface $output, bool $i
301308

302309
/**
303310
* Setup user file system
304-
* @param string $uid
305311
*/
306-
private function setupUserFs($uid): void {
312+
private function setupUserFs(string $uid): void {
307313
\OC_Util::tearDownFS();
308314
\OC_Util::setupFS($uid);
309315
}

apps/encryption/tests/Command/FixEncryptedVersionTest.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,6 @@ public function testExecuteWithDirectoryPathOption() {
333333
$this->assertStringNotContainsString('world.txt', $output);
334334
}
335335

336-
/**
337-
* Test commands with a directory path
338-
*/
339336
public function testExecuteWithNoUser() {
340337
$this->util->expects($this->once())->method('isMasterKeyEnabled')
341338
->willReturn(true);
@@ -347,6 +344,20 @@ public function testExecuteWithNoUser() {
347344

348345
$output = $this->commandTester->getDisplay();
349346

347+
$this->assertStringContainsString('No user id provided', $output);
348+
}
349+
350+
public function testExecuteWithBadUser() {
351+
$this->util->expects($this->once())->method('isMasterKeyEnabled')
352+
->willReturn(true);
353+
354+
$this->commandTester->execute([
355+
'user' => 'nonexisting',
356+
'--path' => "/"
357+
]);
358+
359+
$output = $this->commandTester->getDisplay();
360+
350361
$this->assertStringContainsString('does not exist', $output);
351362
}
352363

0 commit comments

Comments
 (0)