Skip to content

Commit daa16cc

Browse files
authored
Merge pull request #37535 from nextcloud/backport/35961/stable25
[stable25] clear encrypted flag when moving away from encrypted storage
2 parents a234bb2 + cab80f3 commit daa16cc

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\encryption\tests;
25+
26+
use OC\Files\Storage\Temporary;
27+
use OC\Files\Storage\Wrapper\Encryption;
28+
use OC\Files\View;
29+
use OCP\Files\Mount\IMountManager;
30+
use OCP\Files\Storage\IDisableEncryptionStorage;
31+
use Test\TestCase;
32+
use Test\Traits\EncryptionTrait;
33+
use Test\Traits\MountProviderTrait;
34+
use Test\Traits\UserTrait;
35+
36+
class TemporaryNoEncrypted extends Temporary implements IDisableEncryptionStorage {
37+
38+
}
39+
40+
/**
41+
* @group DB
42+
*/
43+
class EncryptedStorageTest extends TestCase {
44+
use MountProviderTrait;
45+
use EncryptionTrait;
46+
use UserTrait;
47+
48+
public function testMoveFromEncrypted() {
49+
$this->createUser("test1", "test2");
50+
$this->setupForUser("test1", 'test2');
51+
52+
$unwrapped = new Temporary();
53+
54+
$this->registerMount("test1", new TemporaryNoEncrypted(), "/test1/files/unenc");
55+
$this->registerMount("test1", $unwrapped, "/test1/files/enc");
56+
57+
$this->loginWithEncryption("test1");
58+
59+
$view = new View("/test1/files");
60+
61+
/** @var IMountManager $mountManager */
62+
$mountManager = \OC::$server->get(IMountManager::class);
63+
64+
$encryptedMount = $mountManager->find("/test1/files/enc");
65+
$unencryptedMount = $mountManager->find("/test1/files/unenc");
66+
$encryptedStorage = $encryptedMount->getStorage();
67+
$unencryptedStorage = $unencryptedMount->getStorage();
68+
$encryptedCache = $encryptedStorage->getCache();
69+
$unencryptedCache = $unencryptedStorage->getCache();
70+
71+
$this->assertTrue($encryptedStorage->instanceOfStorage(Encryption::class));
72+
$this->assertFalse($unencryptedStorage->instanceOfStorage(Encryption::class));
73+
74+
$encryptedStorage->file_put_contents("foo.txt", "bar");
75+
$this->assertEquals("bar", $encryptedStorage->file_get_contents("foo.txt"));
76+
$this->assertStringStartsWith("HBEGIN:oc_encryption_module:", $unwrapped->file_get_contents("foo.txt"));
77+
78+
$this->assertTrue($encryptedCache->get("foo.txt")->isEncrypted());
79+
80+
$view->rename("enc/foo.txt", "unenc/foo.txt");
81+
82+
$this->assertEquals("bar", $unencryptedStorage->file_get_contents("foo.txt"));
83+
$this->assertFalse($unencryptedCache->get("foo.txt")->isEncrypted());
84+
}
85+
}

lib/private/Files/Cache/Cache.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
4444
use OC\Files\Search\SearchComparison;
4545
use OC\Files\Search\SearchQuery;
46+
use OC\Files\Storage\Wrapper\Encryption;
4647
use OCP\DB\QueryBuilder\IQueryBuilder;
4748
use OCP\EventDispatcher\IEventDispatcher;
4849
use OCP\Files\Cache\CacheEntryInsertedEvent;
@@ -639,6 +640,10 @@ protected function getMoveInfo($path) {
639640
return [$this->getNumericStorageId(), $path];
640641
}
641642

643+
protected function hasEncryptionWrapper(): bool {
644+
return $this->storage->instanceOfStorage(Encryption::class);
645+
}
646+
642647
/**
643648
* Move a file or folder in the cache
644649
*
@@ -690,6 +695,11 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
690695
->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
691696
->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
692697

698+
// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
699+
if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
700+
$query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
701+
}
702+
693703
try {
694704
$query->execute();
695705
} catch (\OC\DatabaseException $e) {
@@ -706,6 +716,12 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
706716
->set('name', $query->createNamedParameter(basename($targetPath)))
707717
->set('parent', $query->createNamedParameter($newParentId, IQueryBuilder::PARAM_INT))
708718
->whereFileId($sourceId);
719+
720+
// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
721+
if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
722+
$query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
723+
}
724+
709725
$query->execute();
710726

711727
$this->connection->commit();
@@ -1060,6 +1076,12 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
10601076
throw new \RuntimeException("Invalid source cache entry on copyFromCache");
10611077
}
10621078
$data = $this->cacheEntryToArray($sourceEntry);
1079+
1080+
// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
1081+
if ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
1082+
$data['encrypted'] = 0;
1083+
}
1084+
10631085
$fileId = $this->put($targetPath, $data);
10641086
if ($fileId <= 0) {
10651087
throw new \RuntimeException("Failed to copy to " . $targetPath . " from cache with source data " . json_encode($data) . " ");

lib/private/Files/Cache/Wrapper/CacheWrapper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ protected function getCache() {
5656
return $this->cache;
5757
}
5858

59+
protected function hasEncryptionWrapper(): bool {
60+
$cache = $this->getCache();
61+
if ($cache instanceof Cache) {
62+
return $cache->hasEncryptionWrapper();
63+
} else {
64+
return false;
65+
}
66+
}
67+
5968
/**
6069
* Make it easy for wrappers to modify every returned cache entry
6170
*

0 commit comments

Comments
 (0)