Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/unreleased/39623
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Bugfix: Prevent encrypted files from being corrupted when overwriting them

Fixed an issue where overwriting an encrypted file by a share recipient
would corrupt it. This is a regression which was introduced by #39516.

https://github.com/owncloud/core/pull/39623
https://github.com/owncloud/encryption/issues/315
19 changes: 11 additions & 8 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -998,19 +998,22 @@ protected function getHeader($path) {
}
}

// Don't try to decrypt files which are marked unencrypted in file-cache
$info = $this->getCache()->get($path);
if (!isset($info['encrypted']) || $info['encrypted'] === false) {
return [];
}

$firstBlock = $this->readFirstBlock($path);
$result = $this->parseRawHeader($firstBlock);

// if the header doesn't contain a encryption module we check if it is a
// legacy file. If true, we add the default encryption module
if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY]) && $exists) {
$result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE';
if (!isset($result[Util::HEADER_ENCRYPTION_MODULE_KEY])) {
if (!empty($result)) {
$result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE';
} elseif ($exists) {
// if the header was empty we have to check first if it is a encrypted file at all
// We would do query to filecache only if we know that entry in filecache exists
$info = $this->getCache()->get($path);
if (isset($info['encrypted']) && $info['encrypted'] === true) {
$result[Util::HEADER_ENCRYPTION_MODULE_KEY] = 'OC_DEFAULT_MODULE';
}
}
}

return $result;
Expand Down
18 changes: 1 addition & 17 deletions tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,6 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) {
$this->arrayCache
]
)->getMock();

$cache = $this->getMockBuilder('\OC\Files\Cache\Cache')
->disableOriginalConstructor()->getMock();
$cache->expects($this->any())
->method('get')
->willReturnCallback(function ($path) {
return ['encrypted' => true, 'path' => $path];
});

$instance = $this->getMockBuilder(Encryption::class)
->setConstructorArgs(
[
Expand All @@ -591,14 +582,12 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) {
$this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
]
)
->setMethods(['getCache','readFirstBlock', 'parseRawHeader'])
->setMethods(['readFirstBlock', 'parseRawHeader'])
->getMock();

$instance->expects($this->once())->method(('parseRawHeader'))
->willReturn([Util::HEADER_ENCRYPTION_MODULE_KEY => 'OC_DEFAULT_MODULE']);

$instance->expects($this->once())->method('getCache')->willReturn($cache);

if ($strippedPathExists) {
$instance->expects($this->once())->method('readFirstBlock')
->with($strippedPath)->willReturn('');
Expand All @@ -613,11 +602,6 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) {
->method('file_exists')
->with($strippedPath)
->willReturn($strippedPathExists);

$instance->expects($this->once())
->method('getCache')
->willReturn($cache);

$this->invokePrivate($instance, 'getHeader', [$path]);
}

Expand Down