diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php index 0f26d518d3..0795d53a33 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Delete.php @@ -87,7 +87,12 @@ class Delete extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $function->getId()) { + $resourceType = $deployment->getAttribute('resourceType'); + // Untyped deployments predate Sites and belong to Functions. + if ( + $deployment->getAttribute('resourceId') !== $function->getId() + || ($resourceType !== 'functions' && !empty($resourceType)) + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php index b92a4a156a..02c57b7c11 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php @@ -111,7 +111,12 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $function->getId()) { + $resourceType = $deployment->getAttribute('resourceType'); + // Untyped deployments predate Sites and belong to Functions. + if ( + $deployment->getAttribute('resourceId') !== $function->getId() + || ($resourceType !== 'functions' && !empty($resourceType)) + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php index 31a395341b..dfdcf7c434 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Get.php @@ -71,7 +71,12 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $function->getId()) { + $resourceType = $deployment->getAttribute('resourceType'); + // Untyped deployments predate Sites and belong to Functions. + if ( + $deployment->getAttribute('resourceId') !== $function->getId() + || ($resourceType !== 'functions' && !empty($resourceType)) + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php index b5819e9665..4f35982c28 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Functions/Deployment/Update.php @@ -88,7 +88,12 @@ class Update extends Base throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $function->getId()) { + $resourceType = $deployment->getAttribute('resourceType'); + // Untyped deployments predate Sites and belong to Functions. + if ( + $deployment->getAttribute('resourceId') !== $function->getId() + || ($resourceType !== 'functions' && !empty($resourceType)) + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php index f847a2a344..0d496804e7 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Delete.php @@ -87,7 +87,10 @@ class Delete extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $site->getId()) { + if ( + $deployment->getAttribute('resourceId') !== $site->getId() + || $deployment->getAttribute('resourceType') !== 'sites' + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php index 170a3c7f2a..90f0db2f35 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php @@ -110,7 +110,10 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $site->getId()) { + if ( + $deployment->getAttribute('resourceId') !== $site->getId() + || $deployment->getAttribute('resourceType') !== 'sites' + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php index 6c6384e2a0..4b2f98579d 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Get.php @@ -67,7 +67,10 @@ class Get extends Action throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $site->getId()) { + if ( + $deployment->getAttribute('resourceId') !== $site->getId() + || $deployment->getAttribute('resourceType') !== 'sites' + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php index af95a9431c..e0eb3d361c 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Sites/Deployment/Update.php @@ -86,7 +86,10 @@ class Update extends Base throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } - if ($deployment->getAttribute('resourceId') !== $site->getId()) { + if ( + $deployment->getAttribute('resourceId') !== $site->getId() + || $deployment->getAttribute('resourceType') !== 'sites' + ) { throw new Exception(Exception::DEPLOYMENT_NOT_FOUND); } diff --git a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php index 2ced54103d..83085f924c 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomServerTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomServerTest.php @@ -1391,6 +1391,92 @@ final class FunctionsCustomServerTest extends Scope $this->cleanupFunction($otherFunctionId); } + public function testDeploymentEndpointsRequireMatchingResourceType(): void + { + $sharedId = ID::unique(); + + $functionId = $this->setupFunction([ + 'functionId' => $sharedId, + 'name' => 'Resource type function', + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'execute' => [Role::any()->toString()], + ]); + + $site = $this->client->call(Client::METHOD_POST, '/sites', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'siteId' => $sharedId, + 'name' => 'Resource type site', + 'framework' => 'other', + 'buildRuntime' => 'node-22', + 'outputDirectory' => './', + 'providerBranch' => 'main', + 'providerRootDirectory' => './', + 'fallbackFile' => '', + ]); + $this->assertEquals(201, $site['headers']['status-code']); + + $deployment = $this->createDeployment($functionId, [ + 'code' => $this->packageFunction('basic'), + 'activate' => 'false', + 'entrypoint' => 'index.js', + ]); + $this->assertEquals(202, $deployment['headers']['status-code']); + $deploymentId = $deployment['body']['$id'] ?? ''; + + $this->assertEventually(function () use ($functionId, $deploymentId) { + $deployment = $this->getDeployment($functionId, $deploymentId); + $this->assertEquals('ready', $deployment['body']['status']); + }, 120000, 500); + + /** + * Test for FAILURE — a site that shares the function custom ID must not + * read or mutate the function deployment (resourceType mismatch). + */ + $response = $this->client->call(Client::METHOD_GET, '/sites/' . $sharedId . '/deployments/' . $deploymentId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + $response = $this->client->call(Client::METHOD_PATCH, '/sites/' . $sharedId . '/deployment', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'deploymentId' => $deploymentId, + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + $response = $this->client->call(Client::METHOD_DELETE, '/sites/' . $sharedId . '/deployments/' . $deploymentId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + /** + * Test for SUCCESS — the owning function path still works. + */ + $response = $this->getDeployment($functionId, $deploymentId); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($deploymentId, $response['body']['$id']); + + $this->client->call(Client::METHOD_DELETE, '/sites/' . $sharedId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->cleanupFunction($functionId); + } + public function testCreateDuplicateDeploymentRequiresOwnership(): void { $data = $this->setupTestDeployment(); diff --git a/tests/e2e/Services/Sites/SitesCustomServerTest.php b/tests/e2e/Services/Sites/SitesCustomServerTest.php index 59d252af87..f57a0b44ea 100644 --- a/tests/e2e/Services/Sites/SitesCustomServerTest.php +++ b/tests/e2e/Services/Sites/SitesCustomServerTest.php @@ -1619,6 +1619,92 @@ final class SitesCustomServerTest extends Scope $this->cleanupSite($siteId); } + public function testDeploymentEndpointsRequireMatchingResourceType(): void + { + $sharedId = ID::unique(); + + $siteId = $this->setupSite([ + 'buildRuntime' => 'node-22', + 'fallbackFile' => '', + 'framework' => 'other', + 'name' => 'Resource type site', + 'outputDirectory' => './', + 'providerBranch' => 'main', + 'providerRootDirectory' => './', + 'siteId' => $sharedId, + ]); + + $function = $this->client->call(Client::METHOD_POST, '/functions', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'functionId' => $sharedId, + 'name' => 'Resource type function', + 'runtime' => 'node-22', + 'entrypoint' => 'index.js', + 'execute' => ['any'], + ]); + $this->assertEquals(201, $function['headers']['status-code']); + + $deployment = $this->createDeployment($siteId, [ + 'code' => $this->packageSite('static-single-file'), + 'activate' => 'false', + ]); + $this->assertEquals(202, $deployment['headers']['status-code']); + $deploymentId = $deployment['body']['$id'] ?? ''; + + $this->assertEventually(function () use ($siteId, $deploymentId) { + $deployment = $this->getDeployment($siteId, $deploymentId); + $this->assertEquals('ready', $deployment['body']['status']); + }, 120000, 500); + + /** + * Test for FAILURE — a function that shares the site custom ID must not + * read or mutate the site deployment (resourceType mismatch). + */ + $response = $this->client->call(Client::METHOD_GET, '/functions/' . $sharedId . '/deployments/' . $deploymentId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + $response = $this->client->call(Client::METHOD_PATCH, '/functions/' . $sharedId . '/deployment', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'deploymentId' => $deploymentId, + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $sharedId . '/deployments/' . $deploymentId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('deployment_not_found', $response['body']['type']); + + /** + * Test for SUCCESS — the owning site path still works. + */ + $response = $this->getDeployment($siteId, $deploymentId); + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals($deploymentId, $response['body']['$id']); + + $this->client->call(Client::METHOD_DELETE, '/functions/' . $sharedId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + $this->cleanupDeployment($siteId, $deploymentId); + $this->cleanupSite($siteId); + } + public function testCreateDuplicateDeploymentRequiresOwnership(): void { $siteId = $this->setupSite([