Skip to content

Commit ef2e308

Browse files
authored
Merge pull request #52762 from nextcloud/backport/52707/stable31
[stable31] fix: throw a better error if we can't get the encrypted header size
2 parents a8029b3 + 7e454b2 commit ef2e308

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

apps/encryption/lib/Command/FixEncryptedVersion.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use OC\Files\View;
1313
use OC\ServerNotAvailableException;
1414
use OCA\Encryption\Util;
15+
use OCP\Encryption\Exceptions\InvalidHeaderException;
1516
use OCP\Files\IRootFolder;
1617
use OCP\HintException;
1718
use OCP\IConfig;
@@ -196,7 +197,7 @@ private function verifyFileContent(string $path, OutputInterface $output, bool $
196197
\fclose($handle);
197198

198199
return true;
199-
} catch (ServerNotAvailableException $e) {
200+
} catch (ServerNotAvailableException|InvalidHeaderException $e) {
200201
// not a "bad signature" error and likely "legacy cipher" exception
201202
// this could mean that the file is maybe not encrypted but the encrypted version is set
202203
if (!$this->supportLegacy && $ignoreCorrectEncVersionCall === true) {

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@
318318
'OCP\\DirectEditing\\IToken' => $baseDir . '/lib/public/DirectEditing/IToken.php',
319319
'OCP\\DirectEditing\\RegisterDirectEditorEvent' => $baseDir . '/lib/public/DirectEditing/RegisterDirectEditorEvent.php',
320320
'OCP\\Encryption\\Exceptions\\GenericEncryptionException' => $baseDir . '/lib/public/Encryption/Exceptions/GenericEncryptionException.php',
321+
'OCP\\Encryption\\Exceptions\\InvalidHeaderException' => $baseDir . '/lib/public/Encryption/Exceptions/InvalidHeaderException.php',
321322
'OCP\\Encryption\\IEncryptionModule' => $baseDir . '/lib/public/Encryption/IEncryptionModule.php',
322323
'OCP\\Encryption\\IFile' => $baseDir . '/lib/public/Encryption/IFile.php',
323324
'OCP\\Encryption\\IManager' => $baseDir . '/lib/public/Encryption/IManager.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
367367
'OCP\\DirectEditing\\IToken' => __DIR__ . '/../../..' . '/lib/public/DirectEditing/IToken.php',
368368
'OCP\\DirectEditing\\RegisterDirectEditorEvent' => __DIR__ . '/../../..' . '/lib/public/DirectEditing/RegisterDirectEditorEvent.php',
369369
'OCP\\Encryption\\Exceptions\\GenericEncryptionException' => __DIR__ . '/../../..' . '/lib/public/Encryption/Exceptions/GenericEncryptionException.php',
370+
'OCP\\Encryption\\Exceptions\\InvalidHeaderException' => __DIR__ . '/../../..' . '/lib/public/Encryption/Exceptions/InvalidHeaderException.php',
370371
'OCP\\Encryption\\IEncryptionModule' => __DIR__ . '/../../..' . '/lib/public/Encryption/IEncryptionModule.php',
371372
'OCP\\Encryption\\IFile' => __DIR__ . '/../../..' . '/lib/public/Encryption/IFile.php',
372373
'OCP\\Encryption\\IManager' => __DIR__ . '/../../..' . '/lib/public/Encryption/IManager.php',

lib/private/Files/Storage/Wrapper/Encryption.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OC\Files\Storage\LocalTempFileTrait;
1919
use OC\Memcache\ArrayCache;
2020
use OCP\Cache\CappedMemoryCache;
21+
use OCP\Encryption\Exceptions\InvalidHeaderException;
2122
use OCP\Encryption\IFile;
2223
use OCP\Encryption\IManager;
2324
use OCP\Encryption\Keys\IStorage;
@@ -344,6 +345,16 @@ public function fopen(string $path, string $mode) {
344345
if ($shouldEncrypt === true && $encryptionModule !== null) {
345346
$this->encryptedPaths->set($this->util->stripPartialFileExtension($path), true);
346347
$headerSize = $this->getHeaderSize($path);
348+
if ($mode === 'r' && $headerSize === 0) {
349+
$firstBlock = $this->readFirstBlock($path);
350+
if (!$firstBlock) {
351+
throw new InvalidHeaderException("Unable to get header block for $path");
352+
} elseif (!str_starts_with($firstBlock, Util::HEADER_START)) {
353+
throw new InvalidHeaderException("Unable to get header size for $path, file doesn't start with encryption header");
354+
} else {
355+
throw new InvalidHeaderException("Unable to get header size for $path, even though file does start with encryption header");
356+
}
357+
}
347358
$source = $this->storage->fopen($path, $mode);
348359
if (!is_resource($source)) {
349360
return false;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-License-Identifier: AGPL-3.0-only
6+
*/
7+
namespace OCP\Encryption\Exceptions;
8+
9+
use OCP\HintException;
10+
11+
/**
12+
* Class InvalidHeaderException
13+
*
14+
* @since 32.0.0
15+
*/
16+
class InvalidHeaderException extends HintException {
17+
}

0 commit comments

Comments
 (0)