From 7d98e83e3f13d64eab893e27c943dcba9b54b808 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 21 Aug 2023 19:45:03 +0200 Subject: [PATCH] fix: add some recrusive detection/prevention Signed-off-by: Robin Appelman --- apps/files_sharing/lib/SharedStorage.php | 14 ++++++++++++++ lib/private/Files/Storage/Wrapper/Wrapper.php | 11 +++++++++++ 2 files changed, 25 insertions(+) diff --git a/apps/files_sharing/lib/SharedStorage.php b/apps/files_sharing/lib/SharedStorage.php index de49e3c429474..65f34b7b9538c 100644 --- a/apps/files_sharing/lib/SharedStorage.php +++ b/apps/files_sharing/lib/SharedStorage.php @@ -40,6 +40,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\DisplayNameCache; use OC\User\NoUserException; use OCA\Files_External\Config\ExternalMountPoint; @@ -96,6 +97,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); @@ -135,8 +138,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()); @@ -149,6 +159,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(); @@ -177,6 +190,7 @@ private function init() { if (!$this->nonMaskedStorage) { $this->nonMaskedStorage = $this->storage; } + self::$initDepth--; } /** diff --git a/lib/private/Files/Storage/Wrapper/Wrapper.php b/lib/private/Files/Storage/Wrapper/Wrapper.php index 9f5564b449038..665914df2a722 100644 --- a/lib/private/Files/Storage/Wrapper/Wrapper.php +++ b/lib/private/Files/Storage/Wrapper/Wrapper.php @@ -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; + } }