Skip to content

Commit 883e620

Browse files
authored
Merge pull request #62339 from nextcloud/backport/62255/stable32
[stable32] fix(files): preserve encryptedVersion when copying cache entries
2 parents 5a55428 + 98d2a99 commit 883e620

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

lib/private/Files/Cache/Cache.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,9 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
11971197
&& $sourceCache->hasEncryptionWrapper()
11981198
&& !$this->shouldEncrypt($targetPath)) {
11991199
$data['encrypted'] = 0;
1200+
// normalizeData() prefers 'encryptedVersion' over 'encrypted' when both are
1201+
// set, so it has to be cleared too or the mark above gets ignored
1202+
unset($data['encryptedVersion']);
12001203
}
12011204

12021205
$fileId = $this->put($targetPath, $data);
@@ -1230,6 +1233,11 @@ private function cacheEntryToArray(ICacheEntry $entry): array {
12301233
if ($entry instanceof CacheEntry && isset($entry['scan_permissions'])) {
12311234
$data['permissions'] = $entry['scan_permissions'];
12321235
}
1236+
1237+
if ($entry->isEncrypted() && isset($entry['encryptedVersion'])) {
1238+
$data['encryptedVersion'] = $entry['encryptedVersion'];
1239+
}
1240+
12331241
return $data;
12341242
}
12351243

tests/lib/Files/Cache/CacheTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,49 @@ public function testMoveFromCacheJail(): void {
540540
$this->assertEquals($this->cache->getId(''), $this->cache->get('targetsub')->getParentId());
541541
}
542542

543+
public function testCopyFromCachePreservesEncryptedVersion(): void {
544+
$data = [
545+
'size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar',
546+
'encrypted' => true, 'encryptedVersion' => 3,
547+
];
548+
$this->cache->put('source', $data);
549+
$sourceEntry = $this->cache->get('source');
550+
$this->assertSame(3, $sourceEntry['encryptedVersion']);
551+
552+
$this->cache->copyFromCache($this->cache, $sourceEntry, 'target');
553+
554+
$targetEntry = $this->cache->get('target');
555+
$this->assertTrue($targetEntry->isEncrypted());
556+
$this->assertSame(3, $targetEntry['encryptedVersion']);
557+
}
558+
559+
public function testCopyFromCacheClearsEncryptedVersionWhenCopyingToNonEncryptedStorage(): void {
560+
$data = [
561+
'size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar',
562+
'encrypted' => true, 'encryptedVersion' => 3,
563+
];
564+
$this->cache2->put('source', $data);
565+
$sourceEntry = $this->cache2->get('source');
566+
567+
$sourceCache = $this->getMockBuilder(Cache::class)
568+
->setConstructorArgs([$this->storage2])
569+
->onlyMethods(['hasEncryptionWrapper'])
570+
->getMock();
571+
$sourceCache->method('hasEncryptionWrapper')->willReturn(true);
572+
573+
$targetCache = $this->getMockBuilder(Cache::class)
574+
->setConstructorArgs([$this->storage])
575+
->onlyMethods(['shouldEncrypt'])
576+
->getMock();
577+
$targetCache->method('shouldEncrypt')->willReturn(false);
578+
579+
$targetCache->copyFromCache($sourceCache, $sourceEntry, 'target');
580+
581+
$targetEntry = $targetCache->get('target');
582+
$this->assertFalse($targetEntry->isEncrypted());
583+
$this->assertSame(0, $targetEntry['encryptedVersion']);
584+
}
585+
543586
public function testGetIncomplete(): void {
544587
$file1 = 'folder1';
545588
$file2 = 'folder2';

0 commit comments

Comments
 (0)