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
This commit is contained in:
Harsh Mahajan committed 2025-07-28 12:29:38 +05:30
1 parent 738e794e8b
commit 5932b859ff
10 files changed
+133 -96

No files matched your search

+1
View File
@@ -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';
+1
View File
@@ -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
@@ -0,0 +1,82 @@
<?php
namespace Appwrite\Network\Validator;
use Utopia\System\System;
use Utopia\Validator;
use Utopia\Validator\Domain;
class AppwriteDomain extends Validator
{
public function getDescription(): string
{
$suffix = System::getEnv('_APP_DOMAIN_SITES') ?: APP_DOMAIN_SITES;
return "Must be a valid domain and sub-subdomains are not allowed for {$suffix}. Only one level of subdomain is permitted.";
}
public function isValid($value): bool
{
$suffix = System::getEnv('_APP_DOMAIN_SITES') ?: APP_DOMAIN_SITES;
// For non-string values, reject them
if (!is_string($value)) {
return false;
}
// For empty strings, reject them
if (empty($value)) {
return false;
}
// Check for spaces in the domain
if (\preg_match('/\s/', $value)) {
return false;
}
// Check for leading dots
if (\str_starts_with($value, '.')) {
return false;
}
// Check for trailing dots
if (\str_ends_with($value, '.')) {
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)) {
return false;
}
// For appwrite.network domains, apply our specific rules
if (\str_ends_with($value, $suffix . '.')) {
return false;
}
// Extract subdomain and check for sub-subdomains
$subdomain = \str_replace($suffix, '', \strtolower($value));
// If there's no subdomain (just the root domain), it's valid
if (empty($subdomain)) {
return true;
}
// Check if the subdomain contains dots (sub-subdomains)
if (\str_contains($subdomain, '.')) {
return false;
}
return true;
}
public function isArray(): bool
{
return false;
}
public function getType(): string
{
return self::TYPE_STRING;
}
}
@@ -1,50 +0,0 @@
<?php
namespace Appwrite\Network\Validator;
use Utopia\Validator;
class AppwriteNetworkDomain extends Validator
{
public function getDescription(): string
{
$suffix = getenv('_APP_DOMAIN_SITES') ?: '.appwrite.network';
return "Sub-subdomains are not allowed for {$suffix}. Only one level of subdomain is permitted.";
}
public function isValid($value): bool
{
$suffix = getenv('_APP_DOMAIN_SITES') ?: '.appwrite.network';
if (!is_string($value) || empty($value)) {
return true;
}
if (\preg_match('/\s/', $value)) {
return false;
}
if (\str_starts_with($value, '.')) {
return false;
}
if (\str_ends_with($value, $suffix . '.')) {
return false;
}
if (!\str_ends_with(\strtolower($value), $suffix)) {
return true;
}
$subdomain = \str_replace($suffix, '', \strtolower($value));
if (\str_contains($subdomain, '.')) {
return false;
}
return true;
}
public function isArray(): bool
{
return false;
}
public function getType(): string
{
return self::TYPE_STRING;
}
}
@@ -3,7 +3,7 @@
namespace Appwrite\Platform\Modules\Console\Http\Resources;
use Appwrite\Extend\Exception;
use Appwrite\Network\Validator\AppwriteNetworkDomain;
use Appwrite\Network\Validator\AppwriteDomain;
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
@@ -14,7 +14,6 @@ use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Platform\Action;
use Utopia\Platform\Scope\HTTP;
use Utopia\Validator\Domain;
use Utopia\Validator\Text;
use Utopia\Validator\WhiteList;
@@ -68,17 +67,12 @@ class Get extends Action
Database $dbForPlatform
) {
if ($type === 'rules') {
$validator = new Domain($value);
$appwriteNetworkValidator = new AppwriteNetworkDomain();
// Validate domain format and appwrite.network specific rules
$appwriteNetworkValidator = new AppwriteDomain();
if (!$appwriteNetworkValidator->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]),
]));
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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) {
@@ -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());