Skip to content

Commit

Permalink
fix: add some recrusive detection/prevention
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Mar 5, 2024
1 parent 799c6de commit 2ccd21c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 15 additions & 1 deletion apps/files_sharing/lib/SharedStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OC\Files\Storage\FailedStorage;
use OC\Files\Storage\Home;
use OC\Files\Storage\Wrapper\PermissionsMask;
use OC\Files\Storage\Wrapper\Wrapper;
use OC\User\NoUserException;
use OCA\Files_External\Config\ConfigAdapter;
use OCP\Constants;
Expand Down Expand Up @@ -97,6 +98,8 @@ class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedSto

private string $sourcePath = '';

private static int $initDepth = 0;

public function __construct($arguments) {
$this->ownerView = $arguments['ownerView'];
$this->logger = \OC::$server->get(LoggerInterface::class);
Expand Down Expand Up @@ -136,8 +139,15 @@ private function init() {
if ($this->initialized) {
return;
}

$this->initialized = true;
self::$initDepth++;

try {
if (self::$initDepth > 10) {
throw new \Exception("Maximum share depth reached");
}

/** @var IRootFolder $rootFolder */
$rootFolder = \OC::$server->get(IRootFolder::class);
$this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
Expand All @@ -150,6 +160,9 @@ private function init() {
$this->cache = new FailedCache();
$this->rootPath = '';
} else {
if ($this->nonMaskedStorage instanceof Wrapper && $this->nonMaskedStorage->isWrapperOf($this)) {
throw new \Exception('recursive share detected');
}
$this->nonMaskedStorage = $ownerNode->getStorage();
$this->sourcePath = $ownerNode->getPath();
$this->rootPath = $ownerNode->getInternalPath();
Expand Down Expand Up @@ -178,6 +191,7 @@ private function init() {
if (!$this->nonMaskedStorage) {
$this->nonMaskedStorage = $this->storage;
}
self::$initDepth--;
}

/**
Expand Down Expand Up @@ -411,7 +425,7 @@ public function getCache($path = '', $storage = null) {
return new FailedCache();
}

$this->cache = new Cache(
$this->cache = new \OCA\Files_Sharing\Cache(
$storage,
$sourceRoot,
\OC::$server->get(CacheDependencies::class),
Expand Down
11 changes: 11 additions & 0 deletions lib/private/Files/Storage/Wrapper/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,4 +654,15 @@ public function writeStream(string $path, $stream, int $size = null): int {
public function getDirectoryContent($directory): \Traversable {
return $this->getWrapperStorage()->getDirectoryContent($directory);
}

public function isWrapperOf(IStorage $storage) {
$wrapped = $this->getWrapperStorage();
if ($wrapped === $storage) {
return true;
}
if ($wrapped instanceof Wrapper) {
return $wrapped->isWrapperOf($storage);
}
return false;
}
}

0 comments on commit 2ccd21c

Please sign in to comment.