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

fix reading newly written encrypted files before their cache entry is written #34724

Merged
merged 2 commits into from
Dec 20, 2022
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
8 changes: 8 additions & 0 deletions apps/encryption/lib/Command/FixEncryptedVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

namespace OCA\Encryption\Command;

use OC\Files\Storage\Wrapper\Encryption;
use OC\Files\View;
use OC\ServerNotAvailableException;
use OCA\Encryption\Util;
Expand Down Expand Up @@ -165,6 +166,13 @@ private function walkPathOfUser(string $user, string $path, OutputInterface $out
*/
private function verifyFileContent(string $path, OutputInterface $output, bool $ignoreCorrectEncVersionCall = true): bool {
try {
// since we're manually poking around the encrypted state we need to ensure that this isn't cached in the encryption wrapper
$mount = $this->view->getMount($path);
$storage = $mount->getStorage();
if ($storage && $storage->instanceOfStorage(Encryption::class)) {
$storage->clearIsEncryptedCache();
}

/**
* In encryption, the files are read in a block size of 8192 bytes
* Read block size of 8192 and a bit more (808 bytes)
Expand Down
20 changes: 16 additions & 4 deletions lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\Storage\LocalTempFileTrait;
use OC\Memcache\ArrayCache;
use OCP\Cache\CappedMemoryCache;
use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\Encryption\IFile;
use OCP\Encryption\IManager;
Expand Down Expand Up @@ -95,6 +96,9 @@ class Encryption extends Wrapper {
/** @var ArrayCache */
private $arrayCache;

/** @var CappedMemoryCache<bool> */
private CappedMemoryCache $encryptedPaths;

/**
* @param array $parameters
*/
Expand Down Expand Up @@ -122,6 +126,7 @@ public function __construct(
$this->update = $update;
$this->mountManager = $mountManager;
$this->arrayCache = $arrayCache;
$this->encryptedPaths = new CappedMemoryCache();
parent::__construct($parameters);
}

Expand Down Expand Up @@ -461,6 +466,7 @@ public function fopen($path, $mode) {
}

if ($shouldEncrypt === true && $encryptionModule !== null) {
$this->encryptedPaths->set($this->util->stripPartialFileExtension($path), true);
$headerSize = $this->getHeaderSize($path);
$source = $this->storage->fopen($path, $mode);
if (!is_resource($source)) {
Expand Down Expand Up @@ -970,11 +976,13 @@ protected function getHeader($path) {

$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
$isEncrypted = $this->encryptedPaths->get($realFile);
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved
if (is_null($isEncrypted)) {
$info = $this->getCache()->get($path);
$isEncrypted = isset($info['encrypted']) && $info['encrypted'] === true;
}

$info = $this->getCache()->get($path);
if (isset($info['encrypted']) && $info['encrypted'] === true) {
if ($isEncrypted) {
$firstBlock = $this->readFirstBlock($path);
$result = $this->parseRawHeader($firstBlock);

Expand Down Expand Up @@ -1087,4 +1095,8 @@ public function writeStream(string $path, $stream, int $size = null): int {

return $count;
}

public function clearIsEncryptedCache(): void {
$this->encryptedPaths->clear();
}
}