Skip to content

Commit 1cde57b

Browse files
Merge pull request #62851 from nextcloud/backport/62255/stable31
[stable31] fix(files): preserve encryptedVersion when copying cache entries
2 parents 4322183 + de96bc5 commit 1cde57b

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
@@ -1173,6 +1173,9 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
11731173
// when moving from an encrypted storage to a non-encrypted storage remove the `encrypted` mark
11741174
if ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
11751175
$data['encrypted'] = 0;
1176+
// normalizeData() prefers 'encryptedVersion' over 'encrypted' when both are
1177+
// set, so it has to be cleared too or the mark above gets ignored
1178+
unset($data['encryptedVersion']);
11761179
}
11771180

11781181
$fileId = $this->put($targetPath, $data);
@@ -1206,6 +1209,11 @@ private function cacheEntryToArray(ICacheEntry $entry): array {
12061209
if ($entry instanceof CacheEntry && isset($entry['scan_permissions'])) {
12071210
$data['permissions'] = $entry['scan_permissions'];
12081211
}
1212+
1213+
if ($entry->isEncrypted() && isset($entry['encryptedVersion'])) {
1214+
$data['encryptedVersion'] = $entry['encryptedVersion'];
1215+
}
1216+
12091217
return $data;
12101218
}
12111219

tests/lib/Files/Cache/CacheTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,49 @@ public function testMoveFromCache(): void {
500500
$this->assertTrue($this->cache->inCache('targetfolder/sub'));
501501
}
502502

503+
public function testCopyFromCachePreservesEncryptedVersion(): void {
504+
$data = [
505+
'size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar',
506+
'encrypted' => true, 'encryptedVersion' => 3,
507+
];
508+
$this->cache->put('source', $data);
509+
$sourceEntry = $this->cache->get('source');
510+
$this->assertSame(3, $sourceEntry['encryptedVersion']);
511+
512+
$this->cache->copyFromCache($this->cache, $sourceEntry, 'target');
513+
514+
$targetEntry = $this->cache->get('target');
515+
$this->assertTrue($targetEntry->isEncrypted());
516+
$this->assertSame(3, $targetEntry['encryptedVersion']);
517+
}
518+
519+
public function testCopyFromCacheClearsEncryptedVersionWhenCopyingToNonEncryptedStorage(): void {
520+
$data = [
521+
'size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar',
522+
'encrypted' => true, 'encryptedVersion' => 3,
523+
];
524+
$this->cache2->put('source', $data);
525+
$sourceEntry = $this->cache2->get('source');
526+
527+
$sourceCache = $this->getMockBuilder(Cache::class)
528+
->setConstructorArgs([$this->storage2])
529+
->onlyMethods(['hasEncryptionWrapper'])
530+
->getMock();
531+
$sourceCache->method('hasEncryptionWrapper')->willReturn(true);
532+
533+
$targetCache = $this->getMockBuilder(Cache::class)
534+
->setConstructorArgs([$this->storage])
535+
->onlyMethods(['hasEncryptionWrapper'])
536+
->getMock();
537+
$targetCache->method('hasEncryptionWrapper')->willReturn(false);
538+
539+
$targetCache->copyFromCache($sourceCache, $sourceEntry, 'target');
540+
541+
$targetEntry = $targetCache->get('target');
542+
$this->assertFalse($targetEntry['encrypted']);
543+
$this->assertSame(0, $targetEntry['encryptedVersion']);
544+
}
545+
503546
public function testGetIncomplete(): void {
504547
$file1 = 'folder1';
505548
$file2 = 'folder2';

0 commit comments

Comments
 (0)