Skip to content

Commit

Permalink
Restore encrypted version to original if no fix was found
Browse files Browse the repository at this point in the history
  • Loading branch information
IljaN committed May 19, 2021
1 parent 51a51ff commit c8d67f5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

## [Unreleased] - XXXX-XX-XX

### Fixed

- Use legacy-encoding setting for HSM also [#269](https://github.com/owncloud/encryption/issues/269)

- `fix-encrypted-version` command restores value to original if no fix is found [#269](https://github.com/owncloud/encryption/issues/269)

## [1.5.0] - 2021-03-11

### Added
Expand Down
8 changes: 8 additions & 0 deletions lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
/**
* @author Sujith Haridasan <[email protected]>
* @author Ilja Neumann <[email protected]>
*
* @copyright Copyright (c) 2019, ownCloud GmbH
* @license AGPL-3.0
Expand Down Expand Up @@ -185,6 +186,8 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
return true;
}

// Save original encrypted version so we can restore it if decryption fails with all version
$originalEncryptedVersion = $encryptedVersion;
if ($encryptedVersion >= 0) {
//test by decrementing the value till 1 and if nothing works try incrementing
$encryptedVersion--;
Expand Down Expand Up @@ -222,6 +225,11 @@ private function correctEncryptedVersion($path, OutputInterface $output) {
$increment++;
}
}

$cacheInfo = ['encryptedVersion' => $originalEncryptedVersion, 'encrypted' => $originalEncryptedVersion];
$cache->put($fileCache->getPath(), $cacheInfo);
$output->writeln("<info>No fix found for $path, restored version to original: $originalEncryptedVersion</info>");

return false;
}

Expand Down
28 changes: 28 additions & 0 deletions tests/unit/Command/FixEncryptedVersionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,34 @@ public function testEncryptedVersionGreaterThanOriginalValue() {
Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 9", $output);
}

public function testVersionIsRestoredToOriginalIfNoFixIsFound() {
\OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
$view = new View("/" . self::TEST_ENCRYPTION_VERSION_AFFECTED_USER . "/files");

$view->touch('bar.txt');
for ($i=0; $i < 40; $i++) {
$view->file_put_contents('bar.txt', 'a test string for hello ' . $i);
}

$fileInfo = $view->getFileInfo('bar.txt');

$storage = $fileInfo->getStorage();
$cache = $storage->getCache();
$fileCache = $cache->get($fileInfo->getId());

$cacheInfo = ['encryptedVersion' => 15, 'encrypted' => 15];
$cache->put($fileCache->getPath(), $cacheInfo);

$this->commandTester->execute([
'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER
]);

$cacheInfo = $cache->get($fileInfo->getId());
$encryptedVersion = $cacheInfo["encryptedVersion"];

$this->assertEquals(15, $encryptedVersion);
}

/**
* Test commands with a file path
*/
Expand Down

0 comments on commit c8d67f5

Please sign in to comment.