diff --git a/docker-compose.yml b/docker-compose.yml index 58b78fcd8e..afc32a518b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1033,7 +1033,7 @@ services: volumes: - appwrite-mariadb:/var/lib/mysql:rw ports: - - "3306:3306" + - "3307:3306" environment: - MYSQL_ROOT_PASSWORD=${_APP_DB_ROOT_PASS} - MYSQL_DATABASE=${_APP_DB_SCHEMA} diff --git a/src/Appwrite/Network/Validator/AppwriteDomain.php b/src/Appwrite/Network/Validator/AppwriteDomain.php new file mode 100644 index 0000000000..ad719c7120 --- /dev/null +++ b/src/Appwrite/Network/Validator/AppwriteDomain.php @@ -0,0 +1,128 @@ +suffix = System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'); + } + + /** + * Get Description + * + * Returns validator description + * + * @return string + */ + public function getDescription(): string + { + return 'Value must be a valid one-level subdomain of ' . $this->suffix . ' (e.g., myapp.' . $this->suffix . ')'; + } + + /** + * Is valid + * + * Validation will pass when $value is a valid one-level subdomain + * of the configured Appwrite sites domain. + * + * @param mixed $value + * @return bool + */ + public function isValid($value): bool + { + // Must be a string + if (!is_string($value)) { + return false; + } + + // Must not be empty + if (empty($value)) { + return false; + } + + // Must not contain spaces or other invalid characters + if (preg_match('/\s/', $value)) { + return false; + } + + // Convert to lowercase for consistent validation + $domain = strtolower(trim($value)); + + // Must end with the configured suffix + if (!str_ends_with($domain, '.' . $this->suffix)) { + return false; + } + + // Remove the suffix to get the subdomain part + $subdomainPart = substr($domain, 0, -strlen('.' . $this->suffix)); + + // Must not be empty after removing suffix + if (empty($subdomainPart)) { + return false; + } + + // Must not start or end with a dot + if (str_starts_with($subdomainPart, '.') || str_ends_with($subdomainPart, '.')) { + return false; + } + + // Must not contain any dots (only one-level subdomains allowed) + if (strpos($subdomainPart, '.') !== false) { + return false; + } + + // Validate the subdomain format using basic domain rules + // Must contain only alphanumeric characters and hyphens + // Must not start or end with hyphen + if (!preg_match('/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/', $subdomainPart)) { + return false; + } + + // Must not be longer than 63 characters (DNS limitation) + if (strlen($subdomainPart) > 63) { + return false; + } + + return true; + } + + /** + * Is array + * + * Function will return false as this validator validates strings. + * + * @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/Platform/Modules/Console/Http/Resources/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php index 114a24ef22..8598884458 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Modules\Console\Http\Resources; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; @@ -67,10 +68,11 @@ class Get extends Action Database $dbForPlatform ) { if ($type === 'rules') { - $validator = new Domain($value); + $domainValidator = new Domain($value); + $appwriteDomainValidator = new AppwriteDomain(); - if (!$validator->isValid($value)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $validator->getDescription()); + if (!$domainValidator->isValid($value) && !$appwriteDomainValidator->isValid($value)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Value must be a valid domain name or a valid Appwrite subdomain.'); } $document = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [ diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php index 4efe8176f6..ad81f3f23a 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Proxy\Http\Rules\API; use Appwrite\Event\Certificate; use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -60,7 +61,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new ValidatorDomain(), 'Domain name.') + ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') ->inject('response') ->inject('project') ->inject('queueForCertificates') @@ -71,6 +72,19 @@ class Create extends Action public function action(string $domain, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform) { + // 1. Domain format validations + try { + $domain = new Domain($domain); + } catch (\Throwable) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); + } + + // 2. Domain prefix validations + if (\str_starts_with($domain->get(), 'commit-') || \str_starts_with($domain->get(), 'branch-')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); + } + + // 3. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL @@ -98,20 +112,10 @@ class Create extends Action $deniedDomains[] = $denyListDomain; } - if (\in_array($domain, $deniedDomains)) { + if (\in_array($domain->get(), $deniedDomains)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - if (\str_starts_with($domain, 'commit-') || \str_starts_with($domain, 'branch-')) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); - } - - try { - $domain = new Domain($domain); - } catch (\Throwable) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); - } - // TODO: @christyjacob remove once we migrate the rules in 1.7.x $ruleId = System::getEnv('_APP_RULES_FORMAT') === 'md5' ? md5($domain->get()) : ID::unique(); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php index 1c8fe7b04d..183c6a1cb6 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Proxy\Http\Rules\Function; use Appwrite\Event\Certificate; use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -62,7 +63,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new ValidatorDomain(), 'Domain name.') + ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') ->param('functionId', '', new UID(), 'ID of function to be executed.') ->param('branch', '', new Text(255, 0), 'Name of VCS branch to deploy changes automatically', true) ->inject('response') @@ -76,6 +77,19 @@ class Create extends Action public function action(string $domain, string $functionId, string $branch, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject) { + // 1. Domain format validations + try { + $domain = new Domain($domain); + } catch (\Throwable) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); + } + + // 2. Domain prefix validations + if (\str_starts_with($domain->get(), 'commit-') || \str_starts_with($domain->get(), 'branch-')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); + } + + // 3. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL @@ -103,20 +117,10 @@ class Create extends Action $deniedDomains[] = $denyListDomain; } - if (\in_array($domain, $deniedDomains)) { + if (\in_array($domain->get(), $deniedDomains)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - if (\str_starts_with($domain, 'commit-') || \str_starts_with($domain, 'branch-')) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); - } - - try { - $domain = new Domain($domain); - } catch (\Throwable) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); - } - $function = $dbForProject->getDocument('functions', $functionId); if ($function->isEmpty()) { throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php index 580d92bc74..c6c5094131 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Proxy\Http\Rules\Redirect; use Appwrite\Event\Certificate; use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -63,7 +64,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new ValidatorDomain(), 'Domain name.') + ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') ->param('url', null, new URL(), 'Target URL of redirection') ->param('statusCode', null, new WhiteList([301, 302, 307, 308]), 'Status code of redirection') ->param('resourceId', '', new UID(), 'ID of parent resource.') @@ -79,6 +80,19 @@ class Create extends Action public function action(string $domain, string $url, int $statusCode, string $resourceId, string $resourceType, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject) { + // 1. Domain format validations + try { + $domain = new Domain($domain); + } catch (\Throwable) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); + } + + // 2. Domain prefix validations + if (\str_starts_with($domain->get(), 'commit-') || \str_starts_with($domain->get(), 'branch-')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); + } + + // 3. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL @@ -106,20 +120,10 @@ class Create extends Action $deniedDomains[] = $denyListDomain; } - if (\in_array($domain, $deniedDomains)) { + if (\in_array($domain->get(), $deniedDomains)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - if (\str_starts_with($domain, 'commit-') || \str_starts_with($domain, 'branch-')) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); - } - - try { - $domain = new Domain($domain); - } catch (\Throwable) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); - } - $collection = match ($resourceType) { 'site' => 'sites', 'function' => 'functions' diff --git a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php index 7a5a1f4952..026680ccf7 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\Proxy\Http\Rules\Site; use Appwrite\Event\Certificate; use Appwrite\Event\Event; use Appwrite\Extend\Exception; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -62,7 +63,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new ValidatorDomain(), 'Domain name.') + ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') ->param('siteId', '', new UID(), 'ID of site to be executed.') ->param('branch', '', new Text(255, 0), 'Name of VCS branch to deploy changes automatically', true) ->inject('response') @@ -76,6 +77,19 @@ class Create extends Action public function action(string $domain, string $siteId, string $branch, Response $response, Document $project, Certificate $queueForCertificates, Event $queueForEvents, Database $dbForPlatform, Database $dbForProject) { + // 1. Domain format validations + try { + $domain = new Domain($domain); + } catch (\Throwable) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); + } + + // 2. Domain prefix validations + if (\str_starts_with($domain->get(), 'commit-') || \str_starts_with($domain->get(), 'branch-')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); + } + + // 3. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL @@ -103,20 +117,10 @@ class Create extends Action $deniedDomains[] = $denyListDomain; } - if (\in_array($domain, $deniedDomains)) { + if (\in_array($domain->get(), $deniedDomains)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - if (\str_starts_with($domain, 'commit-') || \str_starts_with($domain, 'branch-')) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); - } - - try { - $domain = new Domain($domain); - } catch (\Throwable) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); - } - $site = $dbForProject->getDocument('sites', $siteId); if ($site->isEmpty()) { throw new Exception(Exception::RULE_RESOURCE_NOT_FOUND); diff --git a/tests/unit/Network/Validators/AppwriteDomainTest.php b/tests/unit/Network/Validators/AppwriteDomainTest.php new file mode 100644 index 0000000000..b9ddd8052f --- /dev/null +++ b/tests/unit/Network/Validators/AppwriteDomainTest.php @@ -0,0 +1,146 @@ +validator = new AppwriteDomain(); + } + + public function tearDown(): void + { + $this->validator = null; + } + + public function testIsValid(): void + { + // Get the actual configured suffix from environment (in Docker it's sites.localhost) + // But test both the environment value and the default value + $envSuffix = \Utopia\System\System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'); + + // Valid one-level subdomains with environment suffix + $this->assertEquals(true, $this->validator->isValid('api.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('test.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('myapp.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('staging.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('prod.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('app123.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('test-app.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('my-awesome-app.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('a.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('x1.' . $envSuffix)); + + // Case insensitive validation + $this->assertEquals(true, $this->validator->isValid('API.' . strtoupper($envSuffix))); + $this->assertEquals(true, $this->validator->isValid('Test.' . ucfirst($envSuffix))); + $this->assertEquals(true, $this->validator->isValid('MyApp.' . $envSuffix)); + + // Invalid sub-subdomains (multiple levels) + $this->assertEquals(false, $this->validator->isValid('api.dev.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('foo.bar.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('app.staging.test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('sub.domain.example.' . $envSuffix)); + + // Non-appwrite.network domains + $this->assertEquals(false, $this->validator->isValid('example.com')); + $this->assertEquals(false, $this->validator->isValid('api.example.com')); + $this->assertEquals(false, $this->validator->isValid('test.google.com')); + $this->assertEquals(false, $this->validator->isValid('app.github.io')); + $this->assertEquals(false, $this->validator->isValid('myapp.herokuapp.com')); + + // Domains with spaces and invalid characters + $this->assertEquals(false, $this->validator->isValid('my app.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('test .' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid(' api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('api.' . $envSuffix . ' ')); + $this->assertEquals(false, $this->validator->isValid('app@test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('app#test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('app$test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('app%test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('app_test.' . $envSuffix)); + + // Edge cases with dots + $this->assertEquals(false, $this->validator->isValid('.api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('api..' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('api.' . $envSuffix . '.')); + $this->assertEquals(false, $this->validator->isValid('.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('..' . $envSuffix)); + + // Just the suffix without subdomain + $this->assertEquals(false, $this->validator->isValid($envSuffix)); + $this->assertEquals(false, $this->validator->isValid('.' . $envSuffix)); + + // Empty and null values + $this->assertEquals(false, $this->validator->isValid('')); + $this->assertEquals(false, $this->validator->isValid(' ')); + $this->assertEquals(false, $this->validator->isValid(null)); + $this->assertEquals(false, $this->validator->isValid(false)); + + // Non-string types + $this->assertEquals(false, $this->validator->isValid(123)); + $this->assertEquals(false, $this->validator->isValid(12.34)); + $this->assertEquals(false, $this->validator->isValid(['api.appwrite.network'])); + $this->assertEquals(false, $this->validator->isValid((object)['domain' => 'api.appwrite.network'])); + $this->assertEquals(false, $this->validator->isValid(true)); + + // Invalid subdomain formats + $this->assertEquals(false, $this->validator->isValid('-api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('api-.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('-test-.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('-.' . $envSuffix)); + + // Too long subdomain (over 63 characters) + $longSubdomain = str_repeat('a', 64) . '.' . $envSuffix; + $this->assertEquals(false, $this->validator->isValid($longSubdomain)); + + // Exactly 63 characters should be valid + $maxLengthSubdomain = str_repeat('a', 63) . '.' . $envSuffix; + $this->assertEquals(true, $this->validator->isValid($maxLengthSubdomain)); + + // Single character subdomain should be valid + $this->assertEquals(true, $this->validator->isValid('a.' . $envSuffix)); + + // Wrong suffix variations (use hardcoded examples for non-matching domains) + $this->assertEquals(false, $this->validator->isValid('api.appwrite.com')); + $this->assertEquals(false, $this->validator->isValid('api.appwrite.org')); + $this->assertEquals(false, $this->validator->isValid('api.notappwrite.network')); + $this->assertEquals(false, $this->validator->isValid('api.' . $envSuffix . '.com')); + + // Numbers in subdomain + $this->assertEquals(true, $this->validator->isValid('123.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('api123.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('123api.' . $envSuffix)); + + // Mixed case with hyphens + $this->assertEquals(true, $this->validator->isValid('My-Test-App.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('app-v2.' . $envSuffix)); + } + + public function testGetType(): void + { + $this->assertEquals('string', $this->validator->getType()); + } + + public function testIsArray(): void + { + $this->assertEquals(false, $this->validator->isArray()); + } + + public function testGetDescription(): void + { + $description = $this->validator->getDescription(); + $this->assertIsString($description); + + // Get the environment suffix to check it's in the description + $envSuffix = \Utopia\System\System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'); + $this->assertStringContainsString($envSuffix, $description); + $this->assertStringContainsString('one-level subdomain', $description); + } +}