diff --git a/src/Appwrite/Utopia/Database/Validator/Folder.php b/src/Appwrite/Utopia/Database/Validator/Folder.php new file mode 100644 index 0000000000..592f1a9ff7 --- /dev/null +++ b/src/Appwrite/Utopia/Database/Validator/Folder.php @@ -0,0 +1,72 @@ + 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; + } +} diff --git a/tests/unit/Utopia/Database/Validator/FolderTest.php b/tests/unit/Utopia/Database/Validator/FolderTest.php new file mode 100644 index 0000000000..d313526db9 --- /dev/null +++ b/tests/unit/Utopia/Database/Validator/FolderTest.php @@ -0,0 +1,55 @@ +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')); + } +}