From c0be12cf885655fe7dc83ddfd64fd80221684267 Mon Sep 17 00:00:00 2001 From: Sujith H Date: Fri, 15 Feb 2019 17:27:24 +0530 Subject: [PATCH] [stable10] Backport of Skip shared files when adding recovery key Skip shared files when recovery key is enabled in the encryption. Signed-off-by: Sujith H --- apps/encryption/lib/Recovery.php | 25 +++++++++++++++++++++++- apps/encryption/tests/RecoveryTest.php | 27 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/apps/encryption/lib/Recovery.php b/apps/encryption/lib/Recovery.php index 656dd4458518..aaa2693b5e4f 100644 --- a/apps/encryption/lib/Recovery.php +++ b/apps/encryption/lib/Recovery.php @@ -25,6 +25,7 @@ namespace OCA\Encryption; +use OC\Files\FileInfo; use OCA\Encryption\Crypto\Crypt; use OCP\Encryption\Keys\IStorage; use OCP\IConfig; @@ -220,6 +221,9 @@ public function setRecoveryForUser($value) { private function addRecoveryKeys($path) { $dirContent = $this->view->getDirectoryContent($path); foreach ($dirContent as $item) { + if ($this->isSharedStorage($item)) { + continue; + } $filePath = $item->getPath(); if ($item['type'] === 'dir') { $this->addRecoveryKeys($filePath . '/'); @@ -248,6 +252,9 @@ private function addRecoveryKeys($path) { private function removeRecoveryKeys($path) { $dirContent = $this->view->getDirectoryContent($path); foreach ($dirContent as $item) { + if ($this->isSharedStorage($item)) { + continue; + } $filePath = $item->getPath(); if ($item['type'] === 'dir') { $this->removeRecoveryKeys($filePath . '/'); @@ -326,5 +333,21 @@ private function recoverFile($path, $privateKey, $uid) { } - + /** + * check if the item is on a shared storage + * + * @param FileInfo $item + * @return bool + */ + protected function isSharedStorage(FileInfo $item) { + /** + * hardcoded class to prevent dependency on files_sharing app and federated share + * TODO: add filter callback to view::getDirectoryContent() or its successor + * so we can filter by more than just mimetype + */ + if ($item->getStorage()->instanceOfStorage('OCA\Files_Sharing\ISharedStorage')) { + return true; + } + return false; + } } diff --git a/apps/encryption/tests/RecoveryTest.php b/apps/encryption/tests/RecoveryTest.php index 1cb044fe4412..0047f5b961ea 100644 --- a/apps/encryption/tests/RecoveryTest.php +++ b/apps/encryption/tests/RecoveryTest.php @@ -27,7 +27,13 @@ namespace OCA\Encryption\Tests; +use OC\Files\FileInfo; +use OC\Files\Mount\MountPoint; use OCA\Encryption\Recovery; +use OCA\Files_Sharing\ISharedStorage; +use OCP\Files\Cache\ICacheEntry; +use OCP\Files\Storage\IStorage; +use OCP\IUser; use Test\TestCase; class RecoveryTest extends TestCase { @@ -244,6 +250,27 @@ public function testRecoverFile() { ['/', 'testkey', 'admin'])); } + public function testNormalShareOrFedShareRecoveryKeySkipped() { + $storage = $this->createMock(IStorage::class); + $storage->method('instanceOfStorage') + ->with(ISharedStorage::class) + ->willReturn(true); + + $cacheEntry = $this->createMock(ICacheEntry::class); + $mountPoint = $this->createMock(MountPoint::class); + $mountPoint->method('getStorage') + ->willReturn($storage); + + $user = $this->createMock(IUser::class); + $remoteUser = $this->createMock(IUser::class); + $normalShare = new FileInfo('test/files/foo', $storage, '', $cacheEntry, $mountPoint, $user); + $fedShare = new FileInfo('test/files/fedshare', $storage, '', $cacheEntry, $mountPoint, $remoteUser); + + $this->viewMock->method('getDirectoryContent') + ->willReturn([$normalShare, $fedShare]); + $this->assertEquals(true, $this->instance->setRecoveryForUser('1')); + } + protected function setUp() { parent::setUp();