fix: resolve domain validation issues in AppwriteDomain validator

This commit is contained in:
Harsh Mahajan
2025-07-28 16:57:39 +05:30
parent 691e687a3f
commit 0dc3708a95
2 changed files with 18 additions and 9 deletions
@@ -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)) {
@@ -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);