From 31b718d829ff2ec9de858906d337be679dee66e4 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 13 Jan 2023 15:28:04 +0000 Subject: [PATCH 1/3] 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 From 7910149053ac878412e7c55dbb6074988844c877 Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 13 Jan 2023 15:42:29 +0000 Subject: [PATCH 2/3] Remove tests and update other references to old validators --- src/Appwrite/GraphQL/Types/Mapper.php | 8 +- tests/unit/Network/Validators/DomainTest.php | 43 ---------- tests/unit/Network/Validators/HostTest.php | 44 ---------- tests/unit/Network/Validators/IPTest.php | 87 -------------------- tests/unit/Network/Validators/URLTest.php | 57 ------------- 5 files changed, 4 insertions(+), 235 deletions(-) delete mode 100644 tests/unit/Network/Validators/DomainTest.php delete mode 100755 tests/unit/Network/Validators/HostTest.php delete mode 100755 tests/unit/Network/Validators/IPTest.php delete mode 100755 tests/unit/Network/Validators/URLTest.php diff --git a/src/Appwrite/GraphQL/Types/Mapper.php b/src/Appwrite/GraphQL/Types/Mapper.php index 1675acc308..42bdc4b149 100644 --- a/src/Appwrite/GraphQL/Types/Mapper.php +++ b/src/Appwrite/GraphQL/Types/Mapper.php @@ -232,14 +232,14 @@ class Mapper case 'Appwrite\Network\Validator\Email': case 'Appwrite\Event\Validator\Event': case 'Utopia\Validator\HexColor': - case 'Appwrite\Network\Validator\Host': - case 'Appwrite\Network\Validator\IP': + case 'Utopia\Validator\Host': + case 'Utopia\Validator\IP': case 'Utopia\Database\Validator\Key': - case 'Appwrite\Network\Validator\Origin': + case 'Utopia\Validator\Origin': case 'Appwrite\Auth\Validator\Password': case 'Utopia\Validator\Text': case 'Utopia\Database\Validator\UID': - case 'Appwrite\Network\Validator\URL': + case 'Utopia\Validator\URL': case 'Utopia\Validator\WhiteList': default: $type = Type::string(); diff --git a/tests/unit/Network/Validators/DomainTest.php b/tests/unit/Network/Validators/DomainTest.php deleted file mode 100644 index c158e39416..0000000000 --- a/tests/unit/Network/Validators/DomainTest.php +++ /dev/null @@ -1,43 +0,0 @@ -domain = new Domain(); - } - - public function tearDown(): void - { - $this->domain = null; - } - - public function testIsValid(): void - { - // Assertions - $this->assertEquals(true, $this->domain->isValid('example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example-app.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain.example_app.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain-new.example.com')); - $this->assertEquals(true, $this->domain->isValid('subdomain_new.example.com')); - $this->assertEquals(true, $this->domain->isValid('localhost')); - $this->assertEquals(true, $this->domain->isValid('appwrite.io')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(true, $this->domain->isValid('appwrite.org')); - $this->assertEquals(false, $this->domain->isValid(false)); - $this->assertEquals(false, $this->domain->isValid('.')); - $this->assertEquals(false, $this->domain->isValid('..')); - $this->assertEquals(false, $this->domain->isValid('')); - $this->assertEquals(false, $this->domain->isValid(['string', 'string'])); - $this->assertEquals(false, $this->domain->isValid(1)); - $this->assertEquals(false, $this->domain->isValid(1.2)); - } -} diff --git a/tests/unit/Network/Validators/HostTest.php b/tests/unit/Network/Validators/HostTest.php deleted file mode 100755 index a2c89ba1e2..0000000000 --- a/tests/unit/Network/Validators/HostTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Utopia\Validator\Host; -use PHPUnit\Framework\TestCase; - -class HostTest extends TestCase -{ - protected ?Host $host = null; - - public function setUp(): void - { - $this->host = new Host(['appwrite.io', 'subdomain.appwrite.test', 'localhost']); - } - - public function tearDown(): void - { - $this->host = null; - } - - public function testIsValid(): void - { - // Assertions - $this->assertEquals($this->host->isValid('https://appwrite.io/link'), true); - $this->assertEquals($this->host->isValid('https://localhost'), true); - $this->assertEquals($this->host->isValid('localhost'), false); - $this->assertEquals($this->host->isValid('http://subdomain.appwrite.test/path'), true); - $this->assertEquals($this->host->isValid('http://test.subdomain.appwrite.test/path'), false); - $this->assertEquals($this->host->getType(), 'string'); - } -} diff --git a/tests/unit/Network/Validators/IPTest.php b/tests/unit/Network/Validators/IPTest.php deleted file mode 100755 index 370f3d60ca..0000000000 --- a/tests/unit/Network/Validators/IPTest.php +++ /dev/null @@ -1,87 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Utopia\Validator\IP; -use PHPUnit\Framework\TestCase; - -class IPTest extends TestCase -{ - protected ?IP $validator; - - public function setUp(): void - { - $this->validator = new IP(); - } - - public function tearDown(): void - { - $this->validator = null; - } - - public function testIsValidIP(): void - { - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - $this->assertEquals($this->validator->getType(), 'string'); - } - - public function testIsValidIPALL(): void - { - $this->validator = new IP(IP::ALL); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } - - public function testIsValidIPV4(): void - { - $this->validator = new IP(IP::V4); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), false); - $this->assertEquals($this->validator->isValid('109.67.204.101'), true); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } - - public function testIsValidIPV6(): void - { - $this->validator = new IP(IP::V6); - - // Assertions - $this->assertEquals($this->validator->isValid('2001:0db8:85a3:08d3:1319:8a2e:0370:7334'), true); - $this->assertEquals($this->validator->isValid('109.67.204.101'), false); - $this->assertEquals($this->validator->isValid(23.5), false); - $this->assertEquals($this->validator->isValid('23.5'), false); - $this->assertEquals($this->validator->isValid(null), false); - $this->assertEquals($this->validator->isValid(true), false); - $this->assertEquals($this->validator->isValid(false), false); - } -} diff --git a/tests/unit/Network/Validators/URLTest.php b/tests/unit/Network/Validators/URLTest.php deleted file mode 100755 index b73df8f5a8..0000000000 --- a/tests/unit/Network/Validators/URLTest.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @version 1.0 RC4 - * @license The MIT License (MIT) - */ - -namespace Tests\Unit\Network\Validators; - -use Utopia\Validator\URL; -use PHPUnit\Framework\TestCase; - -class URLTest extends TestCase -{ - protected ?URL $url; - - public function setUp(): void - { - $this->url = new URL(); - } - - public function tearDown(): void - { - $this->url = null; - } - - public function testIsValid(): void - { - $this->assertEquals('Value must be a valid URL', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(true, $this->url->isValid('htts://example.com')); // does not validate protocol - $this->assertEquals(false, $this->url->isValid('example.com')); // though, requires some kind of protocol - $this->assertEquals(false, $this->url->isValid('http:/example.com')); - $this->assertEquals(true, $this->url->isValid('http://exa-mple.com')); - $this->assertEquals(false, $this->url->isValid('htt@s://example.com')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/foo%2\u00c2\u00a9zbar')); - $this->assertEquals(true, $this->url->isValid('http://www.example.com/?q=%3Casdf%3E')); - $this->assertEquals(true, $this->url->isValid('https://example.com/foo%2\u00c2\u00ä9zbär')); - } - - public function testIsValidAllowedSchemes(): void - { - $this->url = new URL(['http', 'https']); - $this->assertEquals('Value must be a valid URL with following schemes (http, https)', $this->url->getDescription()); - $this->assertEquals(true, $this->url->isValid('http://example.com')); - $this->assertEquals(true, $this->url->isValid('https://example.com')); - $this->assertEquals(false, $this->url->isValid('gopher://www.example.com')); - } -} From f5715f4df859a13192bacb0c8dc0dc7ffd3cbcfa Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Fri, 13 Jan 2023 15:43:57 +0000 Subject: [PATCH 3/3] Update Spec generator --- src/Appwrite/Specification/Format/OpenAPI3.php | 4 ++-- src/Appwrite/Specification/Format/Swagger2.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Specification/Format/OpenAPI3.php b/src/Appwrite/Specification/Format/OpenAPI3.php index fb7208c569..0e54977d60 100644 --- a/src/Appwrite/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/Specification/Format/OpenAPI3.php @@ -311,7 +311,7 @@ class OpenAPI3 extends Format $node['schema']['format'] = 'email'; $node['schema']['x-example'] = 'email@example.com'; break; - case 'Appwrite\Network\Validator\URL': + case 'Utopia\Validator\URL': $node['schema']['type'] = $validator->getType(); $node['schema']['format'] = 'url'; $node['schema']['x-example'] = 'https://example.com'; @@ -391,7 +391,7 @@ class OpenAPI3 extends Format case 'Utopia\Validator\Length': $node['schema']['type'] = $validator->getType(); break; - case 'Appwrite\Network\Validator\Host': + case 'Utopia\Validator\Host': $node['schema']['type'] = $validator->getType(); $node['schema']['format'] = 'url'; $node['schema']['x-example'] = 'https://example.com'; diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index e4d673ed14..ea48d671f2 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -312,7 +312,7 @@ class Swagger2 extends Format $node['format'] = 'email'; $node['x-example'] = 'email@example.com'; break; - case 'Appwrite\Network\Validator\URL': + case 'Utopia\Validator\URL': $node['type'] = $validator->getType(); $node['format'] = 'url'; $node['x-example'] = 'https://example.com'; @@ -393,7 +393,7 @@ class Swagger2 extends Format case 'Utopia\Validator\Length': $node['type'] = $validator->getType(); break; - case 'Appwrite\Network\Validator\Host': + case 'Utopia\Validator\Host': $node['type'] = $validator->getType(); $node['format'] = 'url'; $node['x-example'] = 'https://example.com';