diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Folders/XList.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Folders/XList.php index 0d67ffef1d..ee69a39a0b 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Folders/XList.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Folders/XList.php @@ -91,8 +91,11 @@ class XList extends Action $parent = Folder::normalize($parent); $cursor = Folder::normalize($cursor); - if ($cursor !== '' && (!\str_starts_with($cursor, $parent) || $cursor === $parent)) { - throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Folder '{$cursor}' for the 'cursor' value is not inside '{$parent}'."); + if ($cursor !== '') { + $remainder = \substr($cursor, \strlen($parent), -1); + if (!\str_starts_with($cursor, $parent) || $cursor === $parent || \str_contains($remainder, '/')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, "Folder '{$cursor}' for the 'cursor' value is not an immediate child of '{$parent}'."); + } } $collection = 'bucket_' . $bucket->getSequence(); @@ -145,6 +148,8 @@ class XList extends Action * Smallest string that sorts after every `parent` value inside the given * folder: the trailing '/' replaced with the next code point ('0'), so a * seek jumps past all of the folder's contents in one indexed query. + * Assumes the database orders `parent` values bytewise (binary collation), + * so nothing sorts between '/' (0x2F) and '0' (0x30). */ private static function seekPast(string $folder): string { diff --git a/tests/e2e/Services/Storage/StorageBase.php b/tests/e2e/Services/Storage/StorageBase.php index 295303abb3..e3c0a7c8b2 100644 --- a/tests/e2e/Services/Storage/StorageBase.php +++ b/tests/e2e/Services/Storage/StorageBase.php @@ -2151,6 +2151,18 @@ trait StorageBase // nonexistent prefix -- empty list, not 404 $this->assertCount(0, $listFolders(['parent' => 'missing'])); + // crafted cursors -- must be an immediate child of parent + foreach ([ + ['parent' => 'photos', 'cursor' => 'photos/2026/july'], // grandchild + ['parent' => 'photos', 'cursor' => 'invoices'], // outside parent + ] as $params) { + $response = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/folders', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), $params); + $this->assertEquals(400, $response['headers']['status-code']); + } + // implicit lifecycle -- deleting the last file removes the folder chain $delete = $this->client->call(Client::METHOD_DELETE, '/storage/buckets/' . $bucketId . '/files/' . $julyFileId, array_merge([ 'content-type' => 'application/json',