Skip to content

Commit c2d5129

Browse files
committed
fix: add some recrusive detection/prevention
Signed-off-by: Robin Appelman <[email protected]>
1 parent 6761c0a commit c2d5129

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

apps/files_sharing/lib/SharedStorage.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OC\Files\Storage\Home;
4242
use OC\Files\Storage\Wrapper\PermissionsMask;
4343
use OC\User\DisplayNameCache;
44+
use OC\Files\Storage\Wrapper\Wrapper;
4445
use OC\User\NoUserException;
4546
use OCA\Files_External\Config\ExternalMountPoint;
4647
use OCP\Constants;
@@ -96,6 +97,8 @@ class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedSto
9697

9798
private string $sourcePath = '';
9899

100+
private static int $initDepth = 0;
101+
99102
public function __construct($arguments) {
100103
$this->ownerView = $arguments['ownerView'];
101104
$this->logger = \OC::$server->get(LoggerInterface::class);
@@ -135,8 +138,15 @@ private function init() {
135138
if ($this->initialized) {
136139
return;
137140
}
141+
138142
$this->initialized = true;
143+
self::$initDepth++;
144+
139145
try {
146+
if (self::$initDepth > 10) {
147+
throw new \Exception("Maximum share depth reached");
148+
}
149+
140150
/** @var IRootFolder $rootFolder */
141151
$rootFolder = \OC::$server->get(IRootFolder::class);
142152
$this->ownerUserFolder = $rootFolder->getUserFolder($this->superShare->getShareOwner());
@@ -149,6 +159,9 @@ private function init() {
149159
$this->cache = new FailedCache();
150160
$this->rootPath = '';
151161
} else {
162+
if ($this->nonMaskedStorage instanceof Wrapper && $this->nonMaskedStorage->isWrapperOf($this)) {
163+
throw new \Exception('recursive share detected');
164+
}
152165
$this->nonMaskedStorage = $ownerNode->getStorage();
153166
$this->sourcePath = $ownerNode->getPath();
154167
$this->rootPath = $ownerNode->getInternalPath();
@@ -177,6 +190,7 @@ private function init() {
177190
if (!$this->nonMaskedStorage) {
178191
$this->nonMaskedStorage = $this->storage;
179192
}
193+
self::$initDepth--;
180194
}
181195

182196
/**

lib/private/Files/Storage/Wrapper/Wrapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,15 @@ public function writeStream(string $path, $stream, int $size = null): int {
654654
public function getDirectoryContent($directory): \Traversable {
655655
return $this->getWrapperStorage()->getDirectoryContent($directory);
656656
}
657+
658+
public function isWrapperOf(IStorage $storage) {
659+
$wrapped = $this->getWrapperStorage();
660+
if ($wrapped === $storage) {
661+
return true;
662+
}
663+
if ($wrapped instanceof Wrapper) {
664+
return $wrapped->isWrapperOf($storage);
665+
}
666+
return false;
667+
}
657668
}

0 commit comments

Comments
 (0)