Skip to content

Commit cc33374

Browse files
Merge pull request #62338 from nextcloud/backport/62255/stable33
[stable33] fix(files): preserve encryptedVersion when copying cache entries
2 parents 4c8cb95 + ba1d172 commit cc33374

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
@@ -1210,6 +1210,9 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
12101210
&& $sourceCache->hasEncryptionWrapper()
12111211
&& !$this->shouldEncrypt($targetPath)) {
12121212
$data['encrypted'] = 0;
1213+
// normalizeData() prefers 'encryptedVersion' over 'encrypted' when both are
1214+
// set, so it has to be cleared too or the mark above gets ignored
1215+
unset($data['encryptedVersion']);
12131216
}
12141217

12151218
$fileId = $this->put($targetPath, $data);
@@ -1243,6 +1246,11 @@ private function cacheEntryToArray(ICacheEntry $entry): array {
12431246
if ($entry instanceof CacheEntry && isset($entry['scan_permissions'])) {
12441247
$data['permissions'] = $entry['scan_permissions'];
12451248
}
1249+
1250+
if ($entry->isEncrypted() && isset($entry['encryptedVersion'])) {
1251+
$data['encryptedVersion'] = $entry['encryptedVersion'];
1252+
}
1253+
12461254
return $data;
12471255
}
12481256

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)