diff --git a/src/Appwrite/Network/Validator/AppwriteDomain.php b/src/Appwrite/Network/Validator/AppwriteDomain.php index b09f34523c..50e33578ee 100644 --- a/src/Appwrite/Network/Validator/AppwriteDomain.php +++ b/src/Appwrite/Network/Validator/AppwriteDomain.php @@ -43,19 +43,28 @@ class AppwriteDomain extends Validator return false; } - // If the domain doesn't end with our suffix, reject it - // (this validator is specifically for appwrite.network domains) - if (!\str_ends_with(\strtolower($value), $suffix)) { + // Remove leading dot from suffix for comparison + $suffixForComparison = ltrim($suffix, '.'); + + // Check if domain ends with the suffix + $domainLower = \strtolower($value); + $suffixLower = \strtolower($suffixForComparison); + + if (!\str_ends_with($domainLower, $suffixLower)) { return false; } - // For appwrite.network domains, apply our specific rules - if (\str_ends_with($value, $suffix . '.')) { + // Check that the domain has the correct structure (subdomain.suffix) + // This prevents domains like 'notappwrite.network' from being accepted + $expectedSuffix = '.' . $suffixLower; + if (!\str_ends_with($domainLower, $expectedSuffix)) { return false; } - // Extract subdomain and check for sub-subdomains - $subdomain = \str_replace($suffix, '', \strtolower($value)); + // Extract subdomain by removing the suffix from the end + $subdomain = \substr($domainLower, 0, -\strlen($suffixLower)); + // Remove trailing dot if present + $subdomain = rtrim($subdomain, '.'); // If there's no subdomain (just the root domain), it's invalid for this validator if (empty($subdomain)) { diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 8680ca191c..113b1da9e6 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -133,7 +133,7 @@ class SitesCustomServerTest extends Scope $this->assertEquals(409, $response['headers']['status-code']); // domain unavailable - $nonExistingDomain = "non-existent-subdomain.sites.localhost"; + $nonExistingDomain = "non-existent-subdomain." . System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'); $response = $this->client->call(Client::METHOD_GET, '/console/resources', [ 'origin' => 'http://localhost', @@ -2453,7 +2453,7 @@ class SitesCustomServerTest extends Scope public function testErrorPages(): void { // non-existent domain page - $domain = 'non-existent-page.sites.localhost'; + $domain = 'non-existent-page.' . System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'); $proxyClient = new Client(); $proxyClient->setEndpoint('http://' . $domain);