diff --git a/src/Appwrite/Network/Validator/AppwriteDomain.php b/src/Appwrite/Network/Validator/AppwriteDomain.php index f14d1ab987..1fd4cbc70d 100644 --- a/src/Appwrite/Network/Validator/AppwriteDomain.php +++ b/src/Appwrite/Network/Validator/AppwriteDomain.php @@ -32,7 +32,7 @@ class AppwriteDomain extends Validator */ public function getDescription(): string { - return 'Value must be a valid one-level subdomain of ' . $this->suffix . ' (e.g., myapp.' . $this->suffix . ')'; + return 'Value must be a valid Appwrite subdomain (one-level subdomain ending in .' . $this->suffix . ').'; } /** @@ -46,59 +46,46 @@ class AppwriteDomain extends Validator */ public function isValid($value): bool { - // Must be a string - if (!is_string($value)) { + // Basic validation + if (!is_string($value) || empty($value) || preg_match('/\s/', $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 + // Check if domain ends with the correct suffix if (!str_ends_with($domain, '.' . $this->suffix)) { return false; } - // Remove the suffix to get the subdomain part + // Extract the subdomain part $subdomainPart = substr($domain, 0, -strlen('.' . $this->suffix)); - // Must not be empty after removing suffix - if (empty($subdomainPart)) { + // Check for empty subdomain or leading/trailing dots + if (empty($subdomainPart) || str_starts_with($subdomainPart, '.') || str_ends_with($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) + // Check for multiple levels (sub-subdomains) 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 + // Check for invalid characters (only lowercase letters, numbers, and hyphens) if (!preg_match('/^[a-z0-9]([a-z0-9-]*[a-z0-9])?$/', $subdomainPart)) { return false; } - // Must not be longer than 63 characters (DNS limitation) + // Check length limit (63 characters max for subdomain) if (strlen($subdomainPart) > 63) { return false; } + // Check for forbidden prefixes + if (str_starts_with($subdomainPart, 'commit-') || str_starts_with($subdomainPart, 'branch-')) { + return false; + } + return true; } 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 ad81f3f23a..7f55db6d72 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -19,6 +19,7 @@ use Utopia\Domains\Domain; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; +use Utopia\Validator\AllOf; use Utopia\Validator\AnyOf; use Utopia\Validator\Domain as ValidatorDomain; use Utopia\Validator\IP; @@ -61,7 +62,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') + ->param('domain', null, new AllOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') ->inject('response') ->inject('project') ->inject('queueForCertificates') @@ -75,16 +76,11 @@ class Create extends Action // 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://.'); + } catch (\Throwable $e) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $e->getMessage()); } - // 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 + // 2. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL 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 183c6a1cb6..aa85deaa2f 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -63,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 AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') + ->param('domain', null, new AllOf([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') @@ -80,16 +80,11 @@ class Create extends Action // 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://.'); + } catch (\Throwable $e) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $e->getMessage()); } - // 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 + // 2. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL 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 c6c5094131..ed2b4bb619 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -20,6 +20,7 @@ use Utopia\Domains\Domain; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; +use Utopia\Validator\AllOf; use Utopia\Validator\AnyOf; use Utopia\Validator\Domain as ValidatorDomain; use Utopia\Validator\IP; @@ -64,7 +65,7 @@ class Create extends Action ->label('abuse-limit', 10) ->label('abuse-key', 'userId:{userId}, url:{url}') ->label('abuse-time', 60) - ->param('domain', null, new AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') + ->param('domain', null, new AllOf([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.') @@ -83,16 +84,11 @@ class Create extends Action // 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://.'); + } catch (\Throwable $e) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $e->getMessage()); } - // 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 + // 2. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL 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 026680ccf7..7b86ac6b5d 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -63,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 AnyOf([new ValidatorDomain(), new AppwriteDomain()]), 'Domain name.') + ->param('domain', null, new AllOf([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') @@ -80,16 +80,11 @@ class Create extends Action // 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://.'); + } catch (\Throwable $e) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $e->getMessage()); } - // 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 + // 2. Denied domains check $deniedDomains = [ 'localhost', APP_HOSTNAME_INTERNAL diff --git a/tests/unit/Network/Validators/AppwriteDomainTest.php b/tests/unit/Network/Validators/AppwriteDomainTest.php index efdd56f561..2b4fcd8a46 100644 --- a/tests/unit/Network/Validators/AppwriteDomainTest.php +++ b/tests/unit/Network/Validators/AppwriteDomainTest.php @@ -66,6 +66,33 @@ class AppwriteDomainTest extends TestCase $this->assertEquals(false, $this->validator->isValid('app%test.' . $envSuffix)); $this->assertEquals(false, $this->validator->isValid('app_test.' . $envSuffix)); + // Invalid formatting + $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)); + $this->assertEquals(false, $this->validator->isValid('.' . $envSuffix . '.')); + $this->assertEquals(false, $this->validator->isValid('..' . $envSuffix)); + + // Forbidden prefixes + $this->assertEquals(false, $this->validator->isValid('commit-api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('commit-test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('commit-123.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('branch-api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('branch-test.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('branch-123.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('COMMIT-api.' . $envSuffix)); + $this->assertEquals(false, $this->validator->isValid('BRANCH-test.' . $envSuffix)); + + // Valid domains that should not be confused with forbidden prefixes + $this->assertEquals(true, $this->validator->isValid('commitment.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('branching.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('my-commit.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('my-branch.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('pre-commit.' . $envSuffix)); + $this->assertEquals(true, $this->validator->isValid('post-branch.' . $envSuffix)); + // Edge cases with dots $this->assertEquals(false, $this->validator->isValid('.api.' . $envSuffix)); $this->assertEquals(false, $this->validator->isValid('api..' . $envSuffix));