From 31b718d829ff2ec9de858906d337be679dee66e4 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 13 Jan 2023 15:28:04 +0000 Subject: [PATCH] Replace Appwrite Validators with backported Utopia ones + Updated Utopia Framework to 0.26.0 + Replaced Appwrite Validators with Utopia ones --- app/controllers/api/account.php | 4 +- app/controllers/api/avatars.php | 2 +- app/controllers/api/databases.php | 4 +- app/controllers/api/projects.php | 4 +- app/controllers/api/teams.php | 2 +- app/controllers/mock.php | 2 +- app/init.php | 4 +- composer.json | 2 +- composer.lock | 44 +++---- src/Appwrite/GraphQL/Types/Mapper.php | 2 +- src/Appwrite/Network/Validator/Domain.php | 78 ------------- src/Appwrite/Network/Validator/Host.php | 84 -------------- src/Appwrite/Network/Validator/IP.php | 114 ------------------- src/Appwrite/Network/Validator/URL.php | 92 --------------- tests/unit/Network/Validators/DomainTest.php | 2 +- tests/unit/Network/Validators/HostTest.php | 2 +- tests/unit/Network/Validators/IPTest.php | 2 +- tests/unit/Network/Validators/URLTest.php | 2 +- 18 files changed, 39 insertions(+), 407 deletions(-) delete mode 100644 src/Appwrite/Network/Validator/Domain.php delete mode 100644 src/Appwrite/Network/Validator/Host.php delete mode 100644 src/Appwrite/Network/Validator/IP.php delete mode 100644 src/Appwrite/Network/Validator/URL.php diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index f9425f690b..e7df74b686 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -10,8 +10,8 @@ use Appwrite\Event\Mail; use Appwrite\Event\Phone as EventPhone; use Appwrite\Extend\Exception; use Appwrite\Network\Validator\Email; -use Appwrite\Network\Validator\Host; -use Appwrite\Network\Validator\URL; +use Utopia\Validator\Host; +use Utopia\Validator\URL; use Appwrite\OpenSSL\OpenSSL; use Appwrite\Template\Template; use Appwrite\URL\URL as URLParser; diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index fbc77d69f6..b6688b8ba0 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -1,7 +1,7 @@ whitelist = $whitelist; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'URL host must be one of: ' . \implode(', ', $this->whitelist); - } - - /** - * Is valid - * - * Validation will pass when $value starts with one of the given hosts - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - // Check if value is valid URL - $urlValidator = new URL(); - - if (!$urlValidator->isValid($value)) { - return false; - } - - $hostname = \parse_url($value, PHP_URL_HOST); - $hostnameValidator = new Hostname($this->whitelist); - return $hostnameValidator->isValid($hostname); - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Appwrite/Network/Validator/IP.php b/src/Appwrite/Network/Validator/IP.php deleted file mode 100644 index 0245d59d3e..0000000000 --- a/src/Appwrite/Network/Validator/IP.php +++ /dev/null @@ -1,114 +0,0 @@ -type = $type; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - return 'Value must be a valid IP address'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid IP address. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - switch ($this->type) { - case self::ALL: - if (\filter_var($value, FILTER_VALIDATE_IP)) { - return true; - } - break; - - case self::V4: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { - return true; - } - break; - - case self::V6: - if (\filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { - return true; - } - break; - - default: - return false; - break; - } - - return false; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/src/Appwrite/Network/Validator/URL.php b/src/Appwrite/Network/Validator/URL.php deleted file mode 100644 index 40a12420f5..0000000000 --- a/src/Appwrite/Network/Validator/URL.php +++ /dev/null @@ -1,92 +0,0 @@ -allowedSchemes = $allowedSchemes; - } - - /** - * Get Description - * - * Returns validator description - * - * @return string - */ - public function getDescription(): string - { - if (!empty($this->allowedSchemes)) { - return 'Value must be a valid URL with following schemes (' . \implode(', ', $this->allowedSchemes) . ')'; - } - - return 'Value must be a valid URL'; - } - - /** - * Is valid - * - * Validation will pass when $value is valid URL. - * - * @param mixed $value - * @return bool - */ - public function isValid($value): bool - { - $sanitizedURL = ''; - - foreach (str_split($value) as $character) { - $sanitizedURL .= (ord($character) > 127) ? rawurlencode($character) : $character; - } - - if (\filter_var($sanitizedURL, FILTER_VALIDATE_URL) === false) { - return false; - } - - if (!empty($this->allowedSchemes) && !\in_array(\parse_url($sanitizedURL, PHP_URL_SCHEME), $this->allowedSchemes)) { - return false; - } - - return true; - } - - /** - * Is array - * - * Function will return true if object is array. - * - * @return bool - */ - public function isArray(): bool - { - return false; - } - - /** - * Get Type - * - * Returns validator type. - * - * @return string - */ - public function getType(): string - { - return self::TYPE_STRING; - } -} diff --git a/tests/unit/Network/Validators/DomainTest.php b/tests/unit/Network/Validators/DomainTest.php index 631ea10753..c158e39416 100644 --- a/tests/unit/Network/Validators/DomainTest.php +++ b/tests/unit/Network/Validators/DomainTest.php @@ -2,7 +2,7 @@ namespace Tests\Unit\Network\Validators; -use Appwrite\Network\Validator\Domain; +use Utopia\Validator\Domain; use PHPUnit\Framework\TestCase; class DomainTest extends TestCase diff --git a/tests/unit/Network/Validators/HostTest.php b/tests/unit/Network/Validators/HostTest.php index 7974bf86a1..a2c89ba1e2 100755 --- a/tests/unit/Network/Validators/HostTest.php +++ b/tests/unit/Network/Validators/HostTest.php @@ -14,7 +14,7 @@ namespace Tests\Unit\Network\Validators; -use Appwrite\Network\Validator\Host; +use Utopia\Validator\Host; use PHPUnit\Framework\TestCase; class HostTest extends TestCase diff --git a/tests/unit/Network/Validators/IPTest.php b/tests/unit/Network/Validators/IPTest.php index 57e395111c..370f3d60ca 100755 --- a/tests/unit/Network/Validators/IPTest.php +++ b/tests/unit/Network/Validators/IPTest.php @@ -14,7 +14,7 @@ namespace Tests\Unit\Network\Validators; -use Appwrite\Network\Validator\IP; +use Utopia\Validator\IP; use PHPUnit\Framework\TestCase; class IPTest extends TestCase diff --git a/tests/unit/Network/Validators/URLTest.php b/tests/unit/Network/Validators/URLTest.php index bc43f25623..b73df8f5a8 100755 --- a/tests/unit/Network/Validators/URLTest.php +++ b/tests/unit/Network/Validators/URLTest.php @@ -14,7 +14,7 @@ namespace Tests\Unit\Network\Validators; -use Appwrite\Network\Validator\URL; +use Utopia\Validator\URL; use PHPUnit\Framework\TestCase; class URLTest extends TestCase