Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] Fix incorrect permissions when copying shared files #49655

Open
wants to merge 3 commits into
base: stable29
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 57 additions & 28 deletions apps/files_sharing/tests/SharedStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
*/
namespace OCA\Files_Sharing\Tests;

use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;
use OC\Files\View;
use OCA\Files_Sharing\SharedStorage;
use OCA\Files_Trashbin\AppInfo\Application;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\Constants;
use OCP\Files\NotFoundException;
use OCP\Share\IShare;

Expand Down Expand Up @@ -68,7 +71,7 @@ protected function tearDown(): void {
}
}

\OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');

parent::tearDown();
}
Expand All @@ -86,7 +89,7 @@ public function testParentOfMountPointIsGone() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -106,7 +109,7 @@ public function testParentOfMountPointIsGone() {

// delete the local folder
/** @var \OC\Files\Storage\Storage $storage */
[$storage, $internalPath] = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
[$storage, $internalPath] = Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/localfolder');
$storage->rmdir($internalPath);

//enforce reload of the mount points
Expand All @@ -131,7 +134,7 @@ public function testRenamePartFile() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);


Expand Down Expand Up @@ -171,24 +174,24 @@ public function testFilesize() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

$share2 = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
);


self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

// compare file size between user1 and user2, should always be the same
$this->assertSame($folderSize, \OC\Files\Filesystem::filesize($this->folder));
$this->assertSame($file1Size, \OC\Files\Filesystem::filesize($this->folder . $this->filename));
$this->assertSame($file2Size, \OC\Files\Filesystem::filesize($this->filename));
$this->assertSame($folderSize, Filesystem::filesize($this->folder));
$this->assertSame($file1Size, Filesystem::filesize($this->folder . $this->filename));
$this->assertSame($file2Size, Filesystem::filesize($this->filename));

//cleanup
$this->shareManager->deleteShare($share1);
Expand All @@ -201,23 +204,23 @@ public function testGetPermissions() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ
Constants::PERMISSION_READ
);


self::loginHelper(self::TEST_FILES_SHARING_API_USER2);

$this->assertTrue(\OC\Files\Filesystem::is_dir($this->folder));
$this->assertTrue(Filesystem::is_dir($this->folder));

// for the share root we expect:
// the read permissions (1)
// the delete permission (8), to enable unshare
$rootInfo = \OC\Files\Filesystem::getFileInfo($this->folder);
$rootInfo = Filesystem::getFileInfo($this->folder);
$this->assertSame(9, $rootInfo->getPermissions());

// for the file within the shared folder we expect:
// the read permissions (1)
$subfileInfo = \OC\Files\Filesystem::getFileInfo($this->folder . $this->filename);
$subfileInfo = Filesystem::getFileInfo($this->folder . $this->filename);
$this->assertSame(1, $subfileInfo->getPermissions());


Expand All @@ -233,7 +236,7 @@ public function testFopenWithReadOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ
Constants::PERMISSION_READ
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -266,7 +269,7 @@ public function testFopenWithCreateOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_CREATE
Constants::PERMISSION_READ | Constants::PERMISSION_CREATE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -312,7 +315,7 @@ public function testFopenWithUpdateOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -358,7 +361,7 @@ public function testFopenWithDeleteOnlyPermission() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE
Constants::PERMISSION_READ | Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand Down Expand Up @@ -392,22 +395,22 @@ public function testMountSharesOtherUser() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$share2 = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER3,
\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE | Constants::PERMISSION_SHARE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/' . $this->folder));

$mountConfigManager = \OC::$server->getMountProviderCollection();
$mounts = $mountConfigManager->getMountsForUser(\OC::$server->getUserManager()->get(self::TEST_FILES_SHARING_API_USER3));
array_walk($mounts, [\OC\Files\Filesystem::getMountManager(), 'addMount']);
array_walk($mounts, [Filesystem::getMountManager(), 'addMount']);

$this->assertTrue($rootView->file_exists('/' . self::TEST_FILES_SHARING_API_USER3 . '/files/' . $this->filename));

Expand All @@ -431,7 +434,7 @@ public function testCopyFromStorage() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -444,7 +447,7 @@ public function testCopyFromStorage() {
[$sharedStorage,] = $view->resolvePath($this->folder);
$this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));

$sourceStorage = new \OC\Files\Storage\Temporary([]);
$sourceStorage = new Temporary([]);
$sourceStorage->file_put_contents('foo.txt', 'asd');

$sharedStorage->copyFromStorage($sourceStorage, 'foo.txt', 'bar.txt');
Expand All @@ -464,7 +467,7 @@ public function testMoveFromStorage() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -477,7 +480,7 @@ public function testMoveFromStorage() {
[$sharedStorage,] = $view->resolvePath($this->folder);
$this->assertTrue($sharedStorage->instanceOfStorage('OCA\Files_Sharing\ISharedStorage'));

$sourceStorage = new \OC\Files\Storage\Temporary([]);
$sourceStorage = new Temporary([]);
$sourceStorage->file_put_contents('foo.txt', 'asd');
$sourceStorage->getScanner()->scan('');

Expand Down Expand Up @@ -507,7 +510,7 @@ public function testNameConflict() {
'foo',
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_GROUP1,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$this->shareManager->acceptShare($share1, self::TEST_FILES_SHARING_API_USER2);

Expand All @@ -520,7 +523,7 @@ public function testNameConflict() {
'foo',
self::TEST_FILES_SHARING_API_USER3,
self::TEST_FILES_SHARING_API_GROUP1,
\OCP\Constants::PERMISSION_ALL
Constants::PERMISSION_ALL
);
$this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);

Expand Down Expand Up @@ -549,7 +552,7 @@ public function testOwnerPermissions() {
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_DELETE
Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
Expand All @@ -561,7 +564,7 @@ public function testOwnerPermissions() {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$this->assertTrue($this->view->file_exists($this->folder . '/newfile.txt'));
$this->assertEquals(\OCP\Constants::PERMISSION_ALL - \OCP\Constants::PERMISSION_CREATE,
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE,
$this->view->getFileInfo($this->folder . '/newfile.txt')->getPermissions());

$this->view->unlink($this->folder);
Expand Down Expand Up @@ -601,4 +604,30 @@ public function testInitWithNotFoundSource() {
$this->assertInstanceOf(\OC\Files\Storage\FailedStorage::class, $storage->getSourceStorage());
$this->assertInstanceOf(\OC\Files\Cache\FailedCache::class, $storage->getCache());
}

public function testCopyPermissions(): void {
self::loginHelper(self::TEST_FILES_SHARING_API_USER1);

$share = $this->share(
IShare::TYPE_USER,
$this->filename,
self::TEST_FILES_SHARING_API_USER1,
self::TEST_FILES_SHARING_API_USER2,
Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE - Constants::PERMISSION_DELETE
);

self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
$view = new View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
$this->assertTrue($view->file_exists($this->filename));

$this->assertTrue($view->copy($this->filename, '/target.txt'));

$this->assertTrue($view->file_exists('/target.txt'));

$info = $view->getFileInfo('/target.txt');
$this->assertEquals(Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, $info->getPermissions());

$this->view->unlink($this->filename);
$this->shareManager->deleteShare($share);
}
}
9 changes: 8 additions & 1 deletion lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader
if ($data['storage_mtime'] == 0) {
$data['storage_mtime'] = $data['mtime'];
}
if (isset($data['f_permissions'])) {
$data['scan_permissions'] = $data['f_permissions'];
}
$data['permissions'] = (int)$data['permissions'];
if (isset($data['creation_time'])) {
$data['creation_time'] = (int)$data['creation_time'];
Expand Down Expand Up @@ -1190,7 +1193,7 @@ public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, str
}

private function cacheEntryToArray(ICacheEntry $entry): array {
return [
$data = [
'size' => $entry->getSize(),
'mtime' => $entry->getMTime(),
'storage_mtime' => $entry->getStorageMTime(),
Expand All @@ -1203,6 +1206,10 @@ private function cacheEntryToArray(ICacheEntry $entry): array {
'upload_time' => $entry->getUploadTime(),
'metadata_etag' => $entry->getMetadataEtag(),
];
if ($entry instanceof CacheEntry && isset($entry['scan_permissions'])) {
$data['permissions'] = $entry['scan_permissions'];
}
return $data;
}

public function getQueryFilterForStorage(): ISearchOperator {
Expand Down
Loading