Skip to content

Commit

Permalink
fix: update "move into share" check to share manager
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Feb 27, 2024
1 parent d0ebe36 commit 96942e4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 460 deletions.
53 changes: 29 additions & 24 deletions lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\ReservedWordException;
use OCP\Files\Storage\IStorage;
use OCP\IUser;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use OCP\Server;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Psr\Log\LoggerInterface;

/**
Expand Down Expand Up @@ -731,6 +733,8 @@ public function deleteAll($directory) {
public function rename($source, $target) {
$absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($source));
$absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($target));
$targetParts = explode('/', $absolutePath2);
$targetUser = $targetParts[1] ?? null;
$result = false;
if (
Filesystem::isValidPath($target)
Expand Down Expand Up @@ -785,7 +789,7 @@ public function rename($source, $target) {
if ($internalPath1 === '') {
if ($mount1 instanceof MoveableMount) {
$sourceParentMount = $this->getMount(dirname($source));
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($storage2, $internalPath2)) {
if ($sourceParentMount === $mount2 && $this->targetIsNotShared($targetUser, $absolutePath2)) {
/**
* @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
*/
Expand Down Expand Up @@ -1781,28 +1785,29 @@ private function assertPathLength($path): void {
* It is not allowed to move a mount point into a different mount point or
* into an already shared folder
*/
private function targetIsNotShared(IStorage $targetStorage, string $targetInternalPath): bool {
// note: cannot use the view because the target is already locked
$fileId = $targetStorage->getCache()->getId($targetInternalPath);
if ($fileId === -1) {
// target might not exist, need to check parent instead
$fileId = $targetStorage->getCache()->getId(dirname($targetInternalPath));
}

// check if any of the parents were shared by the current owner (include collections)
$shares = Share::getItemShared(
'folder',
(string)$fileId,
\OC\Share\Constants::FORMAT_NONE,
null,
true
);

if (count($shares) > 0) {
$this->logger->debug(
'It is not allowed to move one mount point into a shared folder',
['app' => 'files']);
return false;
private function targetIsNotShared(string $user, string $targetPath): bool {
$providers = [
IShare::TYPE_USER,
IShare::TYPE_GROUP,
IShare::TYPE_EMAIL,
IShare::TYPE_CIRCLE,
IShare::TYPE_ROOM,
IShare::TYPE_DECK,
IShare::TYPE_SCIENCEMESH
];
$shareManager = Server::get(IManager::class);
/** @var IShare[] $shares */
$shares = array_merge(...array_map(function (int $type) use ($shareManager, $user) {
return $shareManager->getSharesBy($user, $type);
}, $providers));

foreach ($shares as $share) {
if (str_starts_with($targetPath, $share->getNode()->getPath())) {
$this->logger->debug(
'It is not allowed to move one mount point into a shared folder',
['app' => 'files']);
return false;
}
}

return true;
Expand Down
Loading

0 comments on commit 96942e4

Please sign in to comment.