Add Folder validator for storage virtual folder paths

This commit is contained in:
Torsten Dittmann committed 2026-07-06 19:48:28 +04:00
1 parent 17092fd61d
commit db11c8fcc6
2 files changed
+127

No files matched your search

@@ -0,0 +1,72 @@
<?php
namespace Appwrite\Utopia\Database\Validator;
use Utopia\Validator;
class Folder extends Validator
{
public const MAX_LENGTH = 2048;
public function getDescription(): string
{
return 'Folder must be `/`-separated segments without empty, `.`, or `..` segments, must not start with `/`, must not contain control characters, and must be at most ' . self::MAX_LENGTH . ' characters long including the trailing slash.';
}
public function isValid($value): bool
{
if (!\is_string($value)) {
return false;
}
if ($value === '') {
return true;
}
if (\strlen(self::normalize($value)) > self::MAX_LENGTH) {
return false;
}
if (\preg_match('/[\x00-\x1F\x7F]/', $value)) {
return false;
}
if (\str_ends_with($value, '/')) {
$value = \substr($value, 0, -1);
}
if ($value === '') {
return false;
}
foreach (\explode('/', $value) as $segment) {
if ($segment === '' || $segment === '.' || $segment === '..') {
return false;
}
}
return true;
}
/**
* Canonical stored form: trailing slash when non-empty.
*/
public static function normalize(string $value): string
{
if ($value === '') {
return '';
}
return \str_ends_with($value, '/') ? $value : $value . '/';
}
public function isArray(): bool
{
return false;
}
public function getType(): string
{
return self::TYPE_STRING;
}
}
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Utopia\Database\Validator;
use Appwrite\Utopia\Database\Validator\Folder;
use PHPUnit\Framework\TestCase;
final class FolderTest extends TestCase
{
protected ?Folder $object = null;
public function setUp(): void
{
$this->object = new Folder();
}
public function testValues(): void
{
// valid
$this->assertTrue($this->object->isValid(''));
$this->assertTrue($this->object->isValid('photos'));
$this->assertTrue($this->object->isValid('photos/'));
$this->assertTrue($this->object->isValid('photos/2026'));
$this->assertTrue($this->object->isValid('photos/2026/'));
$this->assertTrue($this->object->isValid('photos/2026/july'));
$this->assertTrue($this->object->isValid('with space/and-dash_underscore.dot'));
$this->assertTrue($this->object->isValid(\str_repeat('a', 2047) . '/'));
// invalid
$this->assertFalse($this->object->isValid(null));
$this->assertFalse($this->object->isValid(false));
$this->assertFalse($this->object->isValid(123));
$this->assertFalse($this->object->isValid('/'));
$this->assertFalse($this->object->isValid('/photos'));
$this->assertFalse($this->object->isValid('photos//2026'));
$this->assertFalse($this->object->isValid('photos/./2026'));
$this->assertFalse($this->object->isValid('photos/../2026'));
$this->assertFalse($this->object->isValid('..'));
$this->assertFalse($this->object->isValid("photos/\x01"));
$this->assertFalse($this->object->isValid("photos\n"));
// 2048 chars without trailing slash normalizes to 2049 -- too long
$this->assertFalse($this->object->isValid(\str_repeat('a', 2048)));
$this->assertFalse($this->object->isValid(\str_repeat('a', 3000)));
}
public function testNormalize(): void
{
$this->assertEquals('', Folder::normalize(''));
$this->assertEquals('photos/', Folder::normalize('photos'));
$this->assertEquals('photos/', Folder::normalize('photos/'));
$this->assertEquals('photos/2026/', Folder::normalize('photos/2026'));
}
}