Skip to content

Commit d411ddf

Browse files
Merge pull request #50159 from nextcloud/backport/49552/stable29
[stable29] fix: improve checks for moving shares/storages into other mounts
2 parents 1d7ade4 + bd7bd8f commit d411ddf

File tree

10 files changed

+147
-36
lines changed

10 files changed

+147
-36
lines changed

apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function isDeletable($path) {
6262
return $this->deletables[$path];
6363
}
6464

65-
public function rename($path1, $path2) {
65+
public function rename($path1, $path2, array $options = []) {
6666
return $this->canRename;
6767
}
6868

apps/files/lib/Service/OwnershipTransferService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ protected function transferFiles(string $sourceUid,
416416
$view->mkdir($finalTarget);
417417
$finalTarget = $finalTarget . '/' . basename($sourcePath);
418418
}
419-
if ($view->rename($sourcePath, $finalTarget) === false) {
419+
if ($view->rename($sourcePath, $finalTarget, ['checkSubMounts' => false]) === false) {
420420
throw new TransferOwnershipException("Could not transfer files.", 1);
421421
}
422422
if (!is_dir("$sourceUid/files")) {

apps/files/src/actions/moveOrCopyAction.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import '@nextcloud/dialogs/style.css'
2323
import type { Folder, Node, View } from '@nextcloud/files'
2424
import type { IFilePickerButton } from '@nextcloud/dialogs'
25-
import type { FileStat, ResponseDataDetailed } from 'webdav'
25+
import type { FileStat, ResponseDataDetailed, WebDAVClientError } from 'webdav'
2626
import type { MoveCopyResult } from './moveOrCopyActionUtils'
2727

2828
// eslint-disable-next-line n/no-extraneous-import
@@ -184,7 +184,18 @@ export const handleCopyMoveNodeTo = async (node: Node, destination: Folder, meth
184184
}
185185
// getting here means either no conflict, file was renamed to keep both files
186186
// in a conflict, or the selected file was chosen to be kept during the conflict
187-
await client.moveFile(currentPath, join(destinationPath, node.basename))
187+
try {
188+
await client.moveFile(currentPath, join(destinationPath, node.basename))
189+
} catch (error) {
190+
const parser = new DOMParser()
191+
const text = await (error as WebDAVClientError).response?.text()
192+
const message = parser.parseFromString(text ?? '', 'text/xml')
193+
.querySelector('message')?.textContent
194+
if (message) {
195+
showError(message)
196+
}
197+
throw error
198+
}
188199
// Delete the node as it will be fetched again
189200
// when navigating to the destination folder
190201
emit('files:node:deleted', node)

build/integration/files_features/transfer-ownership.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ Feature: transfer-ownership
511511
And user "user2" accepts last share
512512
When transferring ownership of path "test" from "user0" to "user1"
513513
Then the command failed with exit code 1
514-
And the command output contains the text "Could not transfer files."
514+
And the command error output contains the text "Moving a storage (user0/files/test) into another storage (user1) is not allowed"
515515

516516
Scenario: transferring ownership does not transfer received shares
517517
Given user "user0" exists

dist/files-init.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-init.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-main.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files-main.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/private/Files/View.php

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,13 @@
6363
use OCP\Files\InvalidCharacterInPathException;
6464
use OCP\Files\InvalidDirectoryException;
6565
use OCP\Files\InvalidPathException;
66+
use OCP\Files\Mount\IMountManager;
6667
use OCP\Files\Mount\IMountPoint;
6768
use OCP\Files\NotFoundException;
6869
use OCP\Files\ReservedWordException;
70+
use OCP\IL10N;
6971
use OCP\IUser;
72+
use OCP\L10N\IFactory;
7073
use OCP\Lock\ILockingProvider;
7174
use OCP\Lock\LockedException;
7275
use OCP\Server;
@@ -97,6 +100,7 @@ class View {
97100
private bool $updaterEnabled = true;
98101
private UserManager $userManager;
99102
private LoggerInterface $logger;
103+
private IL10N $l10n;
100104

101105
/**
102106
* @throws \Exception If $root contains an invalid path
@@ -111,6 +115,7 @@ public function __construct(string $root = '') {
111115
$this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
112116
$this->userManager = \OC::$server->getUserManager();
113117
$this->logger = \OC::$server->get(LoggerInterface::class);
118+
$this->l10n = \OC::$server->get(IFactory::class)->get('files');
114119
}
115120

116121
/**
@@ -727,18 +732,24 @@ public function deleteAll($directory) {
727732
*
728733
* @param string $source source path
729734
* @param string $target target path
735+
* @param array $options
730736
*
731737
* @return bool|mixed
732738
* @throws LockedException
733739
*/
734-
public function rename($source, $target) {
740+
public function rename($source, $target, array $options = []) {
741+
$checkSubMounts = $options['checkSubMounts'] ?? true;
742+
735743
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
736744
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
737745

738746
if (str_starts_with($absolutePath2, $absolutePath1 . '/')) {
739747
throw new ForbiddenException("Moving a folder into a child folder is forbidden", false);
740748
}
741749

750+
/** @var IMountManager $mountManager */
751+
$mountManager = \OC::$server->get(IMountManager::class);
752+
742753
$targetParts = explode('/', $absolutePath2);
743754
$targetUser = $targetParts[1] ?? null;
744755
$result = false;
@@ -792,31 +803,38 @@ public function rename($source, $target) {
792803
try {
793804
$this->changeLock($target, ILockingProvider::LOCK_EXCLUSIVE, true);
794805

806+
if ($checkSubMounts) {
807+
$movedMounts = $mountManager->findIn($this->getAbsolutePath($source));
808+
} else {
809+
$movedMounts = [];
810+
}
811+
795812
if ($internalPath1 === '') {
796-
if ($mount1 instanceof MoveableMount) {
797-
$sourceParentMount = $this->getMount(dirname($source));
798-
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($targetUser, $absolutePath2)) {
799-
/**
800-
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
801-
*/
802-
$sourceMountPoint = $mount1->getMountPoint();
803-
$result = $mount1->moveMount($absolutePath2);
804-
$manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
805-
} else {
806-
$result = false;
807-
}
808-
} else {
809-
$result = false;
810-
}
813+
$sourceParentMount = $this->getMount(dirname($source));
814+
$movedMounts[] = $mount1;
815+
$this->validateMountMove($movedMounts, $sourceParentMount, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
816+
/**
817+
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
818+
*/
819+
$sourceMountPoint = $mount1->getMountPoint();
820+
$result = $mount1->moveMount($absolutePath2);
821+
$manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
822+
811823
// moving a file/folder within the same mount point
812824
} elseif ($storage1 === $storage2) {
825+
if (count($movedMounts) > 0) {
826+
$this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
827+
}
813828
if ($storage1) {
814829
$result = $storage1->rename($internalPath1, $internalPath2);
815830
} else {
816831
$result = false;
817832
}
818833
// moving a file/folder between storages (from $storage1 to $storage2)
819834
} else {
835+
if (count($movedMounts) > 0) {
836+
$this->validateMountMove($movedMounts, $mount1, $mount2, !$this->targetIsNotShared($targetUser, $absolutePath2));
837+
}
820838
$result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
821839
}
822840

@@ -866,6 +884,55 @@ public function rename($source, $target) {
866884
return $result;
867885
}
868886

887+
/**
888+
* @throws ForbiddenException
889+
*/
890+
private function validateMountMove(array $mounts, IMountPoint $sourceMount, IMountPoint $targetMount, bool $targetIsShared): void {
891+
$targetPath = $this->getRelativePath($targetMount->getMountPoint());
892+
if ($targetPath) {
893+
$targetPath = trim($targetPath, '/');
894+
} else {
895+
$targetPath = $targetMount->getMountPoint();
896+
}
897+
898+
foreach ($mounts as $mount) {
899+
$sourcePath = $this->getRelativePath($mount->getMountPoint());
900+
if ($sourcePath) {
901+
$sourcePath = trim($sourcePath, '/');
902+
} else {
903+
$sourcePath = $mount->getMountPoint();
904+
}
905+
906+
if (!$mount instanceof MoveableMount) {
907+
throw new ForbiddenException($this->l10n->t('Storage %s cannot be moved', [$sourcePath]), false);
908+
}
909+
910+
if ($targetIsShared) {
911+
if ($sourceMount instanceof SharedMount) {
912+
throw new ForbiddenException($this->l10n->t('Moving a share (%s) into a shared folder is not allowed', [$sourcePath]), false);
913+
} else {
914+
throw new ForbiddenException($this->l10n->t('Moving a storage (%s) into a shared folder is not allowed', [$sourcePath]), false);
915+
}
916+
}
917+
918+
if ($sourceMount !== $targetMount) {
919+
if ($sourceMount instanceof SharedMount) {
920+
if ($targetMount instanceof SharedMount) {
921+
throw new ForbiddenException($this->l10n->t('Moving a share (%s) into another share (%s) is not allowed', [$sourcePath, $targetPath]), false);
922+
} else {
923+
throw new ForbiddenException($this->l10n->t('Moving a share (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
924+
}
925+
} else {
926+
if ($targetMount instanceof SharedMount) {
927+
throw new ForbiddenException($this->l10n->t('Moving a storage (%s) into a share (%s) is not allowed', [$sourcePath, $targetPath]), false);
928+
} else {
929+
throw new ForbiddenException($this->l10n->t('Moving a storage (%s) into another storage (%s) is not allowed', [$sourcePath, $targetPath]), false);
930+
}
931+
}
932+
}
933+
}
934+
}
935+
869936
/**
870937
* Copy a file/folder from the source path to target path
871938
*

tests/lib/Files/ViewTest.php

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use OCP\Constants;
2121
use OCP\Files\Config\IMountProvider;
2222
use OCP\Files\FileInfo;
23+
use OCP\Files\ForbiddenException;
2324
use OCP\Files\GenericFileException;
2425
use OCP\Files\Mount\IMountManager;
2526
use OCP\Files\Storage\IStorage;
@@ -1639,10 +1640,27 @@ public function testMountPointMove() {
16391640
$this->assertTrue($view->rename('mount2', 'sub/moved_mount'), 'Can move a mount point into a subdirectory');
16401641
}
16411642

1642-
/**
1643-
* Test that moving a mount point into another is forbidden
1644-
*/
1645-
public function testMoveMountPointIntoAnother() {
1643+
public function testMoveMountPointOverwrite(): void {
1644+
self::loginAsUser($this->user);
1645+
1646+
[$mount1, $mount2] = $this->createTestMovableMountPoints([
1647+
$this->user . '/files/mount1',
1648+
$this->user . '/files/mount2',
1649+
]);
1650+
1651+
$mount1->expects($this->never())
1652+
->method('moveMount');
1653+
1654+
$mount2->expects($this->never())
1655+
->method('moveMount');
1656+
1657+
$view = new View('/' . $this->user . '/files/');
1658+
1659+
$this->expectException(ForbiddenException::class);
1660+
$view->rename('mount1', 'mount2');
1661+
}
1662+
1663+
public function testMoveMountPointIntoMount(): void {
16461664
self::loginAsUser($this->user);
16471665

16481666
[$mount1, $mount2] = $this->createTestMovableMountPoints([
@@ -1658,8 +1676,8 @@ public function testMoveMountPointIntoAnother() {
16581676

16591677
$view = new View('/' . $this->user . '/files/');
16601678

1661-
$this->assertFalse($view->rename('mount1', 'mount2'), 'Cannot overwrite another mount point');
1662-
$this->assertFalse($view->rename('mount1', 'mount2/sub'), 'Cannot move a mount point into another');
1679+
$this->expectException(ForbiddenException::class);
1680+
$view->rename('mount1', 'mount2/sub');
16631681
}
16641682

16651683
/**
@@ -1701,9 +1719,24 @@ public function testMoveMountPointIntoSharedFolder() {
17011719
->setNode($shareDir);
17021720
$shareManager->createShare($share);
17031721

1704-
$this->assertFalse($view->rename('mount1', 'shareddir'), 'Cannot overwrite shared folder');
1705-
$this->assertFalse($view->rename('mount1', 'shareddir/sub'), 'Cannot move mount point into shared folder');
1706-
$this->assertFalse($view->rename('mount1', 'shareddir/sub/sub2'), 'Cannot move mount point into shared subfolder');
1722+
try {
1723+
$view->rename('mount1', 'shareddir');
1724+
$this->fail('Cannot overwrite shared folder');
1725+
} catch (ForbiddenException $e) {
1726+
1727+
}
1728+
try {
1729+
$view->rename('mount1', 'shareddir/sub');
1730+
$this->fail('Cannot move mount point into shared folder');
1731+
} catch (ForbiddenException $e) {
1732+
1733+
}
1734+
try {
1735+
$view->rename('mount1', 'shareddir/sub/sub2');
1736+
$this->fail('Cannot move mount point into shared subfolder');
1737+
} catch (ForbiddenException $e) {
1738+
1739+
}
17071740
$this->assertTrue($view->rename('mount2', 'shareddir notshared/sub'), 'Can move mount point into a similarly named but non-shared folder');
17081741

17091742
$shareManager->deleteShare($share);

0 commit comments

Comments
 (0)