From 1d40e6597f8330cfc29ad772a44a9fe01115302d Mon Sep 17 00:00:00 2001 From: Harsh Mahajan <127186841+HarshMN2345@users.noreply.github.com> Date: Mon, 28 Jul 2025 17:33:39 +0530 Subject: [PATCH] fix:hit and trial --- .../Modules/Console/Http/Resources/Get.php | 10 +++++++--- .../Modules/Proxy/Http/Rules/API/Create.php | 20 +++++++++++-------- .../Proxy/Http/Rules/Function/Create.php | 18 ++++++++++------- .../Proxy/Http/Rules/Redirect/Create.php | 18 ++++++++++------- .../Modules/Proxy/Http/Rules/Site/Create.php | 18 ++++++++++------- .../Projects/ProjectsCustomServerTest.php | 2 +- 6 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php index 6dded34d16..777ade9093 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Resources/Get.php @@ -14,6 +14,7 @@ use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; +use Utopia\System\System; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; @@ -68,9 +69,12 @@ class Get extends Action ) { if ($type === 'rules') { // Validate domain format and appwrite.network specific rules - $appwriteNetworkValidator = new AppwriteDomain(); - if (!$appwriteNetworkValidator->isValid($value)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + if (!empty($sitesDomain) && \str_ends_with($value, '.' . ltrim($sitesDomain, '.'))) { + $appwriteNetworkValidator = new AppwriteDomain(); + if (!$appwriteNetworkValidator->isValid($value)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + } } $document = Authorization::skip(fn () => $dbForPlatform->findOne('rules', [ 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 e9a9d3c317..20fc7943bb 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/API/Create.php @@ -80,10 +80,11 @@ class Create extends Action $mainDomain = System::getEnv('_APP_DOMAIN', ''); $deniedDomains[] = $mainDomain; - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - if (!empty($sitesDomain)) { - $deniedDomains[] = $sitesDomain; - } + // Don't add sites domain to denied list - it should be allowed for subdomains + // $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + // if (!empty($sitesDomain)) { + // $deniedDomains[] = $sitesDomain; + // } $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (!empty($functionsDomain)) { @@ -108,15 +109,18 @@ class Create extends Action } // Validate domain format and appwrite.network specific rules - $appwriteNetworkValidator = new AppwriteDomain(); - if (!$appwriteNetworkValidator->isValid($domain)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + if (!empty($sitesDomain) && \str_ends_with($domain, '.' . ltrim($sitesDomain, '.'))) { + $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) { + } catch (\Throwable $e) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Domain may not start with http:// or https://.'); } 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 d822eb87aa..e53f6586b6 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Function/Create.php @@ -85,10 +85,11 @@ class Create extends Action $mainDomain = System::getEnv('_APP_DOMAIN', ''); $deniedDomains[] = $mainDomain; - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - if (!empty($sitesDomain)) { - $deniedDomains[] = $sitesDomain; - } + // Don't add sites domain to denied list - it should be allowed for subdomains + // $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + // if (!empty($sitesDomain)) { + // $deniedDomains[] = $sitesDomain; + // } $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (!empty($functionsDomain)) { @@ -113,9 +114,12 @@ class Create extends Action } // Validate domain format and appwrite.network specific rules - $appwriteNetworkValidator = new AppwriteDomain(); - if (!$appwriteNetworkValidator->isValid($domain)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + if (!empty($sitesDomain) && \str_ends_with($domain, '.' . ltrim($sitesDomain, '.'))) { + $appwriteNetworkValidator = new AppwriteDomain(); + if (!$appwriteNetworkValidator->isValid($domain)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + } } // Create domain object for further processing 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 4ccb5df08b..ef936073b0 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Redirect/Create.php @@ -88,10 +88,11 @@ class Create extends Action $mainDomain = System::getEnv('_APP_DOMAIN', ''); $deniedDomains[] = $mainDomain; - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - if (!empty($sitesDomain)) { - $deniedDomains[] = $sitesDomain; - } + // Don't add sites domain to denied list - it should be allowed for subdomains + // $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + // if (!empty($sitesDomain)) { + // $deniedDomains[] = $sitesDomain; + // } $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (!empty($functionsDomain)) { @@ -116,9 +117,12 @@ class Create extends Action } // Validate domain format and appwrite.network specific rules - $appwriteNetworkValidator = new AppwriteDomain(); - if (!$appwriteNetworkValidator->isValid($domain)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + if (!empty($sitesDomain) && \str_ends_with($domain, '.' . ltrim($sitesDomain, '.'))) { + $appwriteNetworkValidator = new AppwriteDomain(); + if (!$appwriteNetworkValidator->isValid($domain)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + } } // Create domain object for further processing 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 3781188490..762b76b84a 100644 --- a/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php +++ b/src/Appwrite/Platform/Modules/Proxy/Http/Rules/Site/Create.php @@ -85,10 +85,11 @@ class Create extends Action $mainDomain = System::getEnv('_APP_DOMAIN', ''); $deniedDomains[] = $mainDomain; - $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); - if (!empty($sitesDomain)) { - $deniedDomains[] = $sitesDomain; - } + // Don't add sites domain to denied list - it should be allowed for subdomains + // $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + // if (!empty($sitesDomain)) { + // $deniedDomains[] = $sitesDomain; + // } $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (!empty($functionsDomain)) { @@ -113,9 +114,12 @@ class Create extends Action } // Validate domain format and appwrite.network specific rules - $appwriteNetworkValidator = new AppwriteDomain(); - if (!$appwriteNetworkValidator->isValid($domain)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + if (!empty($sitesDomain) && \str_ends_with($domain, '.' . ltrim($sitesDomain, '.'))) { + $appwriteNetworkValidator = new AppwriteDomain(); + if (!$appwriteNetworkValidator->isValid($domain)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $appwriteNetworkValidator->getDescription()); + } } // Create domain object for further processing diff --git a/tests/e2e/Services/Projects/ProjectsCustomServerTest.php b/tests/e2e/Services/Projects/ProjectsCustomServerTest.php index 68ff53ae55..1fd0e17d28 100644 --- a/tests/e2e/Services/Projects/ProjectsCustomServerTest.php +++ b/tests/e2e/Services/Projects/ProjectsCustomServerTest.php @@ -27,7 +27,7 @@ class ProjectsCustomServerTest extends Scope ]); $response = $this->client->call(Client::METHOD_POST, '/proxy/rules/api', $headers, [ - 'domain' => $testId . '-api.appwrite.test', + 'domain' => $testId . '-api.' . System::getEnv('_APP_DOMAIN_SITES', 'appwrite.network'), ]); $this->assertEquals(201, $response['headers']['status-code']);