Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable19] avoid fread on directories and unencrypted files #27405

Closed
wants to merge 7 commits into from
Closed
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
25 changes: 12 additions & 13 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -923,21 +923,20 @@ protected function getHeader($path) {
$path = $realFile;
}

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

// first check if it is an 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) {
$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])) {
if (!empty($result)) {
// 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]) && (!empty($result) || $exists)) {
$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';
}
}
}

Expand Down
18 changes: 14 additions & 4 deletions tests/lib/Files/Storage/Wrapper/EncryptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,14 @@ 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('\OC\Files\Storage\Wrapper\Encryption')
->setConstructorArgs(
Expand All @@ -586,9 +594,11 @@ public function testGetHeader($path, $strippedPathExists, $strippedPath) {
$this->encryptionManager, $util, $this->logger, $this->file, null, $this->keyStore, $this->update, $this->mountManager, $this->arrayCache
]
)
->setMethods(['readFirstBlock', 'parseRawHeader'])
->setMethods(['getCache','readFirstBlock', 'parseRawHeader'])
->getMock();


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

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

Expand Down Expand Up @@ -661,8 +671,8 @@ public function testGetHeaderAddLegacyModule($header, $isEncrypted, $exists, $ex
->setMethods(['readFirstBlock', 'parseRawHeader', 'getCache'])
->getMock();

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

$result = $this->invokePrivate($instance, 'getHeader', ['test.txt']);
$this->assertSameSize($expected, $result);
Expand Down