Skip to content

Commit

Permalink
fix reading newly written encrypted files before their cache entry is…
Browse files Browse the repository at this point in the history
… written

Signed-off-by: Robin Appelman <[email protected]>
  • Loading branch information
icewind1991 committed Oct 24, 2022
1 parent 56fe33f commit 2a2d3e2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 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);
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

0 comments on commit 2a2d3e2

Please sign in to comment.