From 5932b859ff4400da50193e0ce2c133768daf845f Mon Sep 17 00:00:00 2001 From: Harsh Mahajan <127186841+HarshMN2345@users.noreply.github.com> Date: Mon, 28 Jul 2025 12:29:38 +0530 Subject: [PATCH] fix: feat: add AppwriteDomain validator to prevent sub-subdomains - Replace AppwriteNetworkDomain with AppwriteDomain validator - Prevent sub-subdomains for appwrite.network domains - Allow only single-level subdomains - Reject domains with spaces and invalid characters - Update all proxy rule endpoints to use new validator - Add comprehensive unit tests --- app/init/constants.php | 1 + docker-compose.yml | 1 + .../Network/Validator/AppwriteDomain.php | 82 +++++++++++++++++++ .../Validator/AppwriteNetworkDomain.php | 50 ----------- .../Modules/Console/Http/Resources/Get.php | 12 +-- .../Modules/Proxy/Http/Rules/API/Create.php | 6 +- .../Proxy/Http/Rules/Function/Create.php | 6 +- .../Proxy/Http/Rules/Redirect/Create.php | 6 +- .../Modules/Proxy/Http/Rules/Site/Create.php | 6 +- ...kDomainTest.php => AppwriteDomainTest.php} | 59 ++++++------- 10 files changed, 133 insertions(+), 96 deletions(-) create mode 100644 src/Appwrite/Network/Validator/AppwriteDomain.php delete mode 100644 src/Appwrite/Network/Validator/AppwriteNetworkDomain.php rename tests/unit/Network/Validators/{AppwriteNetworkDomainTest.php => AppwriteDomainTest.php} (61%) diff --git a/app/init/constants.php b/app/init/constants.php index 62d9f16853..1b71d57b0c 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -4,6 +4,7 @@ use Appwrite\Platform\Modules\Compute\Specification; const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; +const APP_DOMAIN_SITES = '.appwrite.network'; // Default sites domain suffix const APP_EMAIL_TEAM = 'team@localhost.test'; // Default email address const APP_EMAIL_SECURITY = ''; // Default security email address const APP_EMAIL_LOGO_URL = 'https://cloud.appwrite.io/images/mails/logo.png'; diff --git a/docker-compose.yml b/docker-compose.yml index 58b78fcd8e..912fe3b045 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -536,6 +536,7 @@ services: - _APP_DOMAIN_TARGET_AAAA - _APP_DOMAIN_TARGET_A - _APP_DOMAIN_FUNCTIONS + - _APP_DOMAIN_SITES - _APP_EMAIL_CERTIFICATES - _APP_REDIS_HOST - _APP_REDIS_PORT diff --git a/src/Appwrite/Network/Validator/AppwriteDomain.php b/src/Appwrite/Network/Validator/AppwriteDomain.php new file mode 100644 index 0000000000..2a6780ff67 --- /dev/null +++ b/src/Appwrite/Network/Validator/AppwriteDomain.php @@ -0,0 +1,82 @@ +isValid($value)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); } - if (!$validator->isValid($value)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $validator->getDescription()); - } - $document = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [ Query::equal('domain', [$value]), ])); 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 9995783573..e9a9d3c317 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -5,7 +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\AppwriteNetworkDomain; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -107,11 +107,13 @@ class Create extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - $appwriteNetworkValidator = new AppwriteNetworkDomain(); + // Validate domain format and appwrite.network specific rules + $appwriteNetworkValidator = new AppwriteDomain(); if (!$appwriteNetworkValidator->isValid($domain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); } + // Create domain object for further processing try { $domain = new Domain($domain); } catch (\Throwable) { 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 648a2994cc..d822eb87aa 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -5,7 +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\AppwriteNetworkDomain; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -112,11 +112,13 @@ class Create extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - $appwriteNetworkValidator = new AppwriteNetworkDomain(); + // Validate domain format and appwrite.network specific rules + $appwriteNetworkValidator = new AppwriteDomain(); if (!$appwriteNetworkValidator->isValid($domain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); } + // Create domain object for further processing try { $domain = new Domain($domain); } catch (\Throwable) { 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 850007a547..4ccb5df08b 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -5,7 +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\AppwriteNetworkDomain; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -115,11 +115,13 @@ class Create extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - $appwriteNetworkValidator = new AppwriteNetworkDomain(); + // Validate domain format and appwrite.network specific rules + $appwriteNetworkValidator = new AppwriteDomain(); if (!$appwriteNetworkValidator->isValid($domain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); } + // Create domain object for further processing try { $domain = new Domain($domain); } catch (\Throwable) { 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 4c52c6f23f..3781188490 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -5,7 +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\AppwriteNetworkDomain; +use Appwrite\Network\Validator\AppwriteDomain; use Appwrite\Network\Validator\DNS; use Appwrite\SDK\AuthType; use Appwrite\SDK\Method; @@ -112,11 +112,13 @@ class Create extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please use a different domain.'); } - $appwriteNetworkValidator = new AppwriteNetworkDomain(); + // Validate domain format and appwrite.network specific rules + $appwriteNetworkValidator = new AppwriteDomain(); if (!$appwriteNetworkValidator->isValid($domain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); } + // Create domain object for further processing try { $domain = new Domain($domain); } catch (\Throwable) { diff --git a/tests/unit/Network/Validators/AppwriteNetworkDomainTest.php b/tests/unit/Network/Validators/AppwriteDomainTest.php similarity index 61% rename from tests/unit/Network/Validators/AppwriteNetworkDomainTest.php rename to tests/unit/Network/Validators/AppwriteDomainTest.php index 89062487b2..c1179946f4 100644 --- a/tests/unit/Network/Validators/AppwriteNetworkDomainTest.php +++ b/tests/unit/Network/Validators/AppwriteDomainTest.php @@ -2,17 +2,18 @@ namespace Tests\Unit\Network\Validators; -use Appwrite\Network\Validator\AppwriteNetworkDomain; +use Appwrite\Network\Validator\AppwriteDomain; use PHPUnit\Framework\TestCase; +use Utopia\System\System; -class AppwriteNetworkDomainTest extends TestCase +class AppwriteDomainTest extends TestCase { - protected ?AppwriteNetworkDomain $validator = null; + protected ?AppwriteDomain $validator = null; public function setUp(): void { - putenv('_APP_DOMAIN_SITES=.appwrite.network'); - $this->validator = new AppwriteNetworkDomain(); + putenv('_APP_DOMAIN_SITES=' . APP_DOMAIN_SITES); + $this->validator = new AppwriteDomain(); } public function tearDown(): void @@ -54,40 +55,40 @@ class AppwriteNetworkDomainTest extends TestCase public function testNonAppwriteNetworkDomains(): void { - // Non-appwrite.network domains should pass validation (not our concern) - $this->assertEquals(true, $this->validator->isValid('example.com')); - $this->assertEquals(true, $this->validator->isValid('api.example.com')); - $this->assertEquals(true, $this->validator->isValid('deep.nested.example.com')); - $this->assertEquals(true, $this->validator->isValid('google.com')); - $this->assertEquals(true, $this->validator->isValid('sub.domain.test.io')); - $this->assertEquals(true, $this->validator->isValid('localhost')); - $this->assertEquals(true, $this->validator->isValid('127.0.0.1')); + // Non-appwrite.network domains should be rejected by this validator + $this->assertEquals(false, $this->validator->isValid('example.com')); + $this->assertEquals(false, $this->validator->isValid('api.example.com')); + $this->assertEquals(false, $this->validator->isValid('deep.nested.example.com')); + $this->assertEquals(false, $this->validator->isValid('google.com')); + $this->assertEquals(false, $this->validator->isValid('sub.domain.test.io')); + $this->assertEquals(false, $this->validator->isValid('localhost')); + $this->assertEquals(false, $this->validator->isValid('127.0.0.1')); // Similar but different domains - $this->assertEquals(true, $this->validator->isValid('appwrite.com')); - $this->assertEquals(true, $this->validator->isValid('api.appwrite.com')); - $this->assertEquals(true, $this->validator->isValid('test.appwrite.org')); - $this->assertEquals(true, $this->validator->isValid('notappwrite.network')); + $this->assertEquals(false, $this->validator->isValid('appwrite.com')); + $this->assertEquals(false, $this->validator->isValid('api.appwrite.com')); + $this->assertEquals(false, $this->validator->isValid('test.appwrite.org')); + $this->assertEquals(false, $this->validator->isValid('notappwrite.network')); } public function testDoubleSubdomainCustomDomain(): void { - // Double subdomain for a custom domain should be allowed - $this->assertEquals(true, $this->validator->isValid('stage.dashboard.example.com')); - $this->assertEquals(true, $this->validator->isValid('foo.bar.baz.example.com')); + // Double subdomain for a custom domain should be rejected by this validator + $this->assertEquals(false, $this->validator->isValid('stage.dashboard.example.com')); + $this->assertEquals(false, $this->validator->isValid('foo.bar.baz.example.com')); } public function testEdgeCases(): void { - // Empty and invalid values should pass (let other validators handle them) - $this->assertEquals(true, $this->validator->isValid('')); - $this->assertEquals(true, $this->validator->isValid(null)); - $this->assertEquals(true, $this->validator->isValid(false)); - $this->assertEquals(true, $this->validator->isValid(123)); - $this->assertEquals(true, $this->validator->isValid([])); + // Empty and invalid values should be rejected + $this->assertEquals(false, $this->validator->isValid('')); + $this->assertEquals(false, $this->validator->isValid(null)); + $this->assertEquals(false, $this->validator->isValid(false)); + $this->assertEquals(false, $this->validator->isValid(123)); + $this->assertEquals(false, $this->validator->isValid([])); // Just the root domain (unlikely but should be valid) - $this->assertEquals(true, $this->validator->isValid('appwrite.network')); + $this->assertEquals(false, $this->validator->isValid('appwrite.network')); // Domain with trailing/leading dots $this->assertEquals(false, $this->validator->isValid('api.test.appwrite.network.')); @@ -104,8 +105,8 @@ class AppwriteNetworkDomainTest extends TestCase public function testValidatorProperties(): void { - $suffix = getenv('_APP_DOMAIN_SITES') ?: '.appwrite.network'; - $expectedDescription = "Sub-subdomains are not allowed for {$suffix}. Only one level of subdomain is permitted."; + $suffix = System::getEnv('_APP_DOMAIN_SITES') ?: APP_DOMAIN_SITES; + $expectedDescription = "Must be a valid domain and sub-subdomains are not allowed for {$suffix}. Only one level of subdomain is permitted."; $this->assertEquals($expectedDescription, $this->validator->getDescription()); $this->assertEquals(false, $this->validator->isArray()); $this->assertEquals('string', $this->validator->getType());