From d3bbcce302beb239e5dbccc572628fb11ea1e629 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 8 Apr 2024 09:31:15 +0545 Subject: [PATCH 1/4] Prevent functions domain to be used as custom domain --- app/controllers/api/proxy.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 1dfbc0ba0f..329e392b42 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -49,6 +49,12 @@ App::post('/v1/proxy/rules') if ($domain === $mainDomain) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your main domain to specific resource. Please use subdomain or a different domain.'); } + + $functionsDomain = App::getEnv('_APP_DOMAIN_FUNCTIONS', ''); + if (str_ends_with($domain, $functionsDomain)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your functions domain or it\'s subdomain to specific resource. Please use different domain.'); + } + if ($domain === 'localhost' || $domain === APP_HOSTNAME_INTERNAL) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please pick another one.'); } From 99cb38c674da93965fa978d59c79b5b30bdf5b80 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Mon, 8 Apr 2024 03:57:08 +0000 Subject: [PATCH 2/4] fix linter issues --- app/controllers/api/proxy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 329e392b42..0cd4c50662 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -49,12 +49,12 @@ App::post('/v1/proxy/rules') if ($domain === $mainDomain) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your main domain to specific resource. Please use subdomain or a different domain.'); } - + $functionsDomain = App::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (str_ends_with($domain, $functionsDomain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your functions domain or it\'s subdomain to specific resource. Please use different domain.'); } - + if ($domain === 'localhost' || $domain === APP_HOSTNAME_INTERNAL) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This domain name is not allowed. Please pick another one.'); } From 7ca9e6923f921cacb372e62b7a57df6a9dae6ff3 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 May 2024 10:47:54 +0545 Subject: [PATCH 3/4] add test --- .../Projects/ProjectsCustomServerTest.php | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/Projects/ProjectsCustomServerTest.php b/tests/e2e/Services/Projects/ProjectsCustomServerTest.php index 436d1df611..bae0d8bda9 100644 --- a/tests/e2e/Services/Projects/ProjectsCustomServerTest.php +++ b/tests/e2e/Services/Projects/ProjectsCustomServerTest.php @@ -2,17 +2,43 @@ namespace Tests\E2E\Services\Projects; +use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideServer; +use Utopia\System\System; class ProjectsCustomServerTest extends Scope { use ProjectCustom; use SideServer; - public function testMock() + // Domains + + public function testCreateProjectRule() { - $this->assertEquals(true, true); + $headers = array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-mode' => 'admin', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]); + + $response = $this->client->call(Client::METHOD_POST, '/proxy/rules', $headers, [ + 'resourceType' => 'api', + 'domain' => 'api.appwrite.test', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + // prevent functions domain + $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); + + $response = $this->client->call(Client::METHOD_POST, '/proxy/rules', $headers, [ + 'resourceType' => 'api', + 'domain' => $functionsDomain, + ]); + + $this->assertEquals(400, $response['headers']['status-code']); } } From 74264582cc31bc2cf32d30dfed10370b5aaf73ce Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Sun, 5 May 2024 11:07:59 +0545 Subject: [PATCH 4/4] Update proxy.php --- app/controllers/api/proxy.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/proxy.php b/app/controllers/api/proxy.php index 0cd4c50662..29f39c55af 100644 --- a/app/controllers/api/proxy.php +++ b/app/controllers/api/proxy.php @@ -50,7 +50,7 @@ App::post('/v1/proxy/rules') throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your main domain to specific resource. Please use subdomain or a different domain.'); } - $functionsDomain = App::getEnv('_APP_DOMAIN_FUNCTIONS', ''); + $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); if (str_ends_with($domain, $functionsDomain)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'You cannot assign your functions domain or it\'s subdomain to specific resource. Please use different domain.'); }