From 8e0ea8d53192981181b291dfde5a941deeb97c33 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 13 Aug 2026 14:07:24 +1200 Subject: [PATCH 1/5] fix(databases): bump utopia-php/mongo to 1.5.3 Reading a row with an integer outside the int32 range returned a 500 on MongoDB. The driver decodes 64-bit BSON integers as MongoDB\BSON\Int64 wrappers, and array-accessing one fatals with "Cannot use object of type MongoDB\BSON\Int64 as array", so any table holding such a value became unlistable from the Console. 1.5.3 unwraps at the decode boundary (utopia-php/mongo#49). Fixes #13175 --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index c3c4cbaf84..ba544531bf 100644 --- a/composer.lock +++ b/composer.lock @@ -4416,16 +4416,16 @@ }, { "name": "utopia-php/mongo", - "version": "1.5.2", + "version": "1.5.3", "source": { "type": "git", "url": "https://github.com/utopia-php/mongo.git", - "reference": "20f9a644a356599fdd6265ba48696fc42a1f1881" + "reference": "be29ee2d84b9f7efdfc6fea5b4b2d4bf95ff5ec4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/mongo/zipball/20f9a644a356599fdd6265ba48696fc42a1f1881", - "reference": "20f9a644a356599fdd6265ba48696fc42a1f1881", + "url": "https://api.github.com/repos/utopia-php/mongo/zipball/be29ee2d84b9f7efdfc6fea5b4b2d4bf95ff5ec4", + "reference": "be29ee2d84b9f7efdfc6fea5b4b2d4bf95ff5ec4", "shasum": "" }, "require": { @@ -4471,9 +4471,9 @@ ], "support": { "issues": "https://github.com/utopia-php/mongo/issues", - "source": "https://github.com/utopia-php/mongo/tree/1.5.2" + "source": "https://github.com/utopia-php/mongo/tree/1.5.3" }, - "time": "2026-08-12T06:57:25+00:00" + "time": "2026-08-13T01:55:11+00:00" }, { "name": "utopia-php/platform", From 446df3f1c23f956931405a4703c210246bc760e2 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 13 Aug 2026 14:30:06 +1200 Subject: [PATCH 2/5] chore(deps): bump utopia-php/database to 7.2.1 Carries the int64 regression tests from utopia-php/database#936, and the unique violation classification work from #733, which now keys off the violated constraint instead of matching on the driver message. --- composer.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.lock b/composer.lock index ba544531bf..dcf4def469 100644 --- a/composer.lock +++ b/composer.lock @@ -3638,16 +3638,16 @@ }, { "name": "utopia-php/database", - "version": "7.1.4", + "version": "7.2.1", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "b2b136a3827ccee2ea4eb83626348db4992286e1" + "reference": "38181c98e23b28c753f35cba6f03086a37033389" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/b2b136a3827ccee2ea4eb83626348db4992286e1", - "reference": "b2b136a3827ccee2ea4eb83626348db4992286e1", + "url": "https://api.github.com/repos/utopia-php/database/zipball/38181c98e23b28c753f35cba6f03086a37033389", + "reference": "38181c98e23b28c753f35cba6f03086a37033389", "shasum": "" }, "require": { @@ -3692,9 +3692,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/7.1.4" + "source": "https://github.com/utopia-php/database/tree/7.2.1" }, - "time": "2026-08-12T09:39:29+00:00" + "time": "2026-08-13T02:26:08+00:00" }, { "name": "utopia-php/detector", From a6d21fcda6b5208032cd73d84ea311d4a4606947 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 13 Aug 2026 10:35:20 +0530 Subject: [PATCH 3/5] Fix unique constraint duplicate errors (#13187) --- app/config/errors.php | 10 ++++++++++ src/Appwrite/Extend/Exception.php | 2 ++ .../Http/Databases/Collections/Documents/Action.php | 10 ++++++++++ .../Databases/Collections/Documents/Bulk/Upsert.php | 12 ++++++++---- .../Http/Databases/Collections/Documents/Create.php | 12 ++++++++---- .../Http/Databases/Collections/Documents/Update.php | 9 ++++++--- .../Http/Databases/Collections/Documents/Upsert.php | 11 +++++++---- tests/e2e/Services/Databases/DatabasesBase.php | 12 ++++++++++++ 8 files changed, 63 insertions(+), 15 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index fcc3c7abdb..079b328219 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -839,6 +839,11 @@ return [ 'description' => 'Document with the requested ID \'%s\' already exists. Try again with a different ID or use ID.unique() to generate a unique ID.', 'code' => 409, ], + Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION => [ + 'name' => Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION, + 'description' => 'Document violates a unique attribute constraint. Try again with different value(s).', + 'code' => 409, + ], Exception::DOCUMENT_UPDATE_CONFLICT => [ 'name' => Exception::DOCUMENT_UPDATE_CONFLICT, 'description' => 'Remote document is newer than local.', @@ -876,6 +881,11 @@ return [ 'description' => 'Row with the requested ID \'%s\' already exists. Try again with a different ID or use ID.unique() to generate a unique ID.', 'code' => 409, ], + Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION => [ + 'name' => Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION, + 'description' => 'Row violates a unique column constraint. Try again with different value(s).', + 'code' => 409, + ], Exception::ROW_UPDATE_CONFLICT => [ 'name' => Exception::ROW_UPDATE_CONFLICT, 'description' => 'Remote row is newer than local.', diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 9f61e5e4ee..17cd908487 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -232,6 +232,7 @@ class Exception extends \Exception public const string DOCUMENT_MISSING_DATA = 'document_missing_data'; public const string DOCUMENT_MISSING_PAYLOAD = 'document_missing_payload'; public const string DOCUMENT_ALREADY_EXISTS = 'document_already_exists'; + public const string DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION = 'document_unique_constraint_violation'; public const string DOCUMENT_UPDATE_CONFLICT = 'document_update_conflict'; public const string DOCUMENT_DELETE_RESTRICTED = 'document_delete_restricted'; @@ -241,6 +242,7 @@ class Exception extends \Exception public const string ROW_MISSING_DATA = 'row_missing_data'; public const string ROW_MISSING_PAYLOAD = 'row_missing_payload'; public const string ROW_ALREADY_EXISTS = 'row_already_exists'; + public const string ROW_UNIQUE_CONSTRAINT_VIOLATION = 'row_unique_constraint_violation'; public const string ROW_UPDATE_CONFLICT = 'row_update_conflict'; public const string ROW_DELETE_RESTRICTED = 'row_delete_restricted'; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 54d5f06db8..7fcb464937 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -164,6 +164,16 @@ abstract class Action extends DatabasesAction : Exception::ROW_ALREADY_EXISTS; } + /** + * Get the appropriate unique constraint exception. + */ + protected function getUniqueConstraintException(): string + { + return $this->isCollectionsAPI() + ? Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION + : Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION; + } + /** * Get the appropriate conflict exception. */ diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php index 9c0eb232b6..5eee3d0040 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php @@ -20,6 +20,7 @@ use Utopia\Database\Exception\Conflict as ConflictException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Database\Exception\Unique as UniqueException; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\UID; use Utopia\Http\Adapter\Swoole\Response as SwooleResponse; @@ -170,12 +171,13 @@ class Upsert extends Action } $dbForDatabases = $getDatabasesDB($database); + $collectionTableId = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(); $upserted = []; try { - $modified = $dbForDatabases->withPreserveDates(function () use ($dbForDatabases, $database, $collection, $documents, $plan, &$upserted) { + $modified = $dbForDatabases->withPreserveDates(function () use ($dbForDatabases, $collectionTableId, $documents, $plan, &$upserted) { return $dbForDatabases->upsertDocuments( - 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $collectionTableId, $documents, onNext: function (Document $document) use ($plan, &$upserted) { if (\count($upserted) < ($plan['databasesBatchSize'] ?? APP_LIMIT_DATABASE_BATCH)) { @@ -186,8 +188,10 @@ class Upsert extends Action }); } catch (ConflictException) { throw new Exception($this->getConflictException()); - } catch (DuplicateException) { - throw new Exception($this->getDuplicateException(), params: ['multiple']); + } catch (UniqueException $e) { + throw new Exception($this->getUniqueConstraintException(), previous: $e); + } catch (DuplicateException $e) { + throw new Exception($this->getDuplicateException(), previous: $e, params: ['multiple']); } catch (RelationshipException $e) { throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); } catch (StructureException $e) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 4ab3bd407a..1143b975e9 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -22,6 +22,7 @@ use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\NotFound as NotFoundException; use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Database\Exception\Unique as UniqueException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -463,12 +464,13 @@ class Create extends Action } $dbForDatabases = $getDatabasesDB($database); + $collectionTableId = 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(); try { $created = []; $dbForDatabases->withPreserveDates( - function () use (&$created, $dbForDatabases, $database, $collection, $documents) { + function () use (&$created, $dbForDatabases, $collectionTableId, $documents) { $dbForDatabases->createDocuments( - 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $collectionTableId, $documents, onNext: function ($doc) use (&$created) { $created[] = $doc; @@ -476,8 +478,10 @@ class Create extends Action ); } ); - } catch (DuplicateException) { - throw new Exception($this->getDuplicateException(), params: [$documentId]); + } catch (UniqueException $e) { + throw new Exception($this->getUniqueConstraintException(), previous: $e); + } catch (DuplicateException $e) { + throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]); } catch (NotFoundException) { throw new Exception($this->getParentNotFoundException(), params: [$collectionId]); } catch (RelationshipException $e) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php index 66ffb504e9..49b8ecd457 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php @@ -19,6 +19,7 @@ use Utopia\Database\Exception\Conflict as ConflictException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Database\Exception\Unique as UniqueException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -326,15 +327,17 @@ class Update extends Action $document = $dbForDatabases->withRequestTimestamp( $requestTimestamp, fn () => $dbForDatabases->withPreserveDates(fn () => $dbForDatabases->updateDocument( - 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $collectionTableId, $document->getId(), $newDocument )) ); } catch (ConflictException) { throw new Exception($this->getConflictException()); - } catch (DuplicateException) { - throw new Exception($this->getDuplicateException(), params: [$documentId]); + } catch (UniqueException $e) { + throw new Exception($this->getUniqueConstraintException(), previous: $e); + } catch (DuplicateException $e) { + throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]); } catch (RelationshipException $e) { throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); } catch (StructureException $e) { diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php index d7cc46449d..c3430fb947 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php @@ -20,6 +20,7 @@ use Utopia\Database\Exception\Conflict as ConflictException; use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Relationship as RelationshipException; use Utopia\Database\Exception\Structure as StructureException; +use Utopia\Database\Exception\Unique as UniqueException; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; @@ -329,9 +330,9 @@ class Upsert extends Action $upserted = []; try { - $dbForDatabases->withPreserveDates(function () use (&$upserted, $dbForDatabases, $database, $collection, $newDocument) { + $dbForDatabases->withPreserveDates(function () use (&$upserted, $dbForDatabases, $collectionTableId, $newDocument) { return $dbForDatabases->upsertDocuments( - 'database_' . $database->getSequence() . '_collection_' . $collection->getSequence(), + $collectionTableId, [$newDocument], onNext: function (Document $document) use (&$upserted) { $upserted[] = $document; @@ -340,8 +341,10 @@ class Upsert extends Action }); } catch (ConflictException) { throw new Exception($this->getConflictException()); - } catch (DuplicateException) { - throw new Exception($this->getDuplicateException(), params: [$documentId]); + } catch (UniqueException $e) { + throw new Exception($this->getUniqueConstraintException(), previous: $e); + } catch (DuplicateException $e) { + throw new Exception($this->getDuplicateException(), previous: $e, params: [$documentId]); } catch (RelationshipException $e) { throw new Exception(Exception::RELATIONSHIP_VALUE_INVALID, $e->getMessage()); } catch (StructureException $e) { diff --git a/tests/e2e/Services/Databases/DatabasesBase.php b/tests/e2e/Services/Databases/DatabasesBase.php index 5e31035fce..78534221c0 100644 --- a/tests/e2e/Services/Databases/DatabasesBase.php +++ b/tests/e2e/Services/Databases/DatabasesBase.php @@ -6262,6 +6262,8 @@ trait DatabasesBase ]); $this->assertEquals(409, $duplicate['headers']['status-code']); + $this->assertEquals($this->getUniqueConstraintException(), $duplicate['body']['type']); + $this->assertStringNotContainsString('requested ID', $duplicate['body']['message']); // Test for exception when inserting new doc and then updating to conflict $document = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $collectionId), array_merge([ @@ -6298,6 +6300,16 @@ trait DatabasesBase ]); $this->assertEquals(409, $duplicate['headers']['status-code']); + $this->assertEquals($this->getUniqueConstraintException(), $duplicate['body']['type']); + $this->assertStringNotContainsString('requested ID', $duplicate['body']['message']); + + } + + private function getUniqueConstraintException(): string + { + return $this->getRecordResource() === 'rows' + ? Exception::ROW_UNIQUE_CONSTRAINT_VIOLATION + : Exception::DOCUMENT_UNIQUE_CONSTRAINT_VIOLATION; } public function testPersistentCreatedAt(): void From 644e17d173f85baa22222a48e539cef04bc52903 Mon Sep 17 00:00:00 2001 From: "cursor[bot]" <206951365+cursor[bot]@users.noreply.github.com> Date: Thu, 13 Aug 2026 12:37:18 +0530 Subject: [PATCH 4/5] fix(compute): require resourceType on deployment get/delete/download/activate (#13127) * fix(compute): require resourceType on deployment get/delete/download/activate Function and site IDs are independent namespaces. resourceId-only checks on Get, Delete, Download, and activate Update can still cross a type boundary when a function and site share a custom ID. Match Status/Duplicate/Create by requiring resourceType as well, and add cross-type e2e coverage. Co-authored-by: Chirag Aggarwal * fix(functions): allow empty deployment resourceType for legacy records Pre-Sites deployments may have a null/empty resourceType. Keep rejecting mismatched types, but do not treat an unset resourceType as a foreign resource on Functions get/delete/download/activate. Sites stay strict because site deployments have always set resourceType. Co-authored-by: Chirag Aggarwal * fix(compute): disambiguate empty deployment resourceType via resourceInternalId Strict resourceType rejects untyped legacy rows; empty-type-as-functions reopened cross-type access when IDs collide. Accept a deployment when resourceType matches, or when resourceType is empty and resourceInternalId matches the parent sequence (unique across functions/sites). Co-authored-by: Chirag Aggarwal * test(compute): pass boolean false for inactive deployment setup SitesBase treats activate with loose equality, so the string 'false' still waits for activation and flakes the resourceType ownership e2e. Co-authored-by: Chirag Aggarwal * fix(compute): reject untyped deployments when function/site IDs collide Sequences are per-collection, so resourceInternalId alone is not enough for empty resourceType. Allow the legacy empty-type path only when the other resource namespace has no document with the same custom ID. Also fix e2e activate handling for multipart (string 'false' + manual ready wait). Co-authored-by: Chirag Aggarwal * fix(compute): require strict deployment resourceType ownership Drop the empty-resourceType compatibility branch. Per-collection sequence fallbacks and opposite-resource lookups were still ambiguous under ID collisions and permission filtering. Match Status/Duplicate/Create: require resourceId and resourceType together. Co-authored-by: Chirag Aggarwal * fix(compute): auth-skip opposite-resource check for untyped deployments Restore empty-resourceType compatibility using resourceInternalId, but only when no opposite-namespace resource shares the custom ID. Look up the opposite resource with authorization skipped so permission filtering cannot hide a colliding function/site and reopen cross-type access. Co-authored-by: Chirag Aggarwal * fix(compute): only reject untyped deployments on sequence collisions Opposite-type resources may share a custom ID with a different sequence. Keep allowing that legacy empty-resourceType path when resourceInternalId matches the parent. Reject only when the opposite resource also shares the same sequence, which is the ambiguous cross-namespace case. Co-authored-by: Chirag Aggarwal * refactor(compute): simplify deployment ownership checks --------- Co-authored-by: Cursor Agent Co-authored-by: Chirag Aggarwal --- .../Functions/Http/Deployments/Delete.php | 7 +- .../Http/Deployments/Download/Get.php | 7 +- .../Functions/Http/Deployments/Get.php | 7 +- .../Http/Functions/Deployment/Update.php | 7 +- .../Modules/Sites/Http/Deployments/Delete.php | 5 +- .../Sites/Http/Deployments/Download/Get.php | 5 +- .../Modules/Sites/Http/Deployments/Get.php | 5 +- .../Sites/Http/Sites/Deployment/Update.php | 5 +- .../Functions/FunctionsCustomServerTest.php | 86 +++++++++++++++++++ .../Services/Sites/SitesCustomServerTest.php | 86 +++++++++++++++++++ 10 files changed, 212 insertions(+), 8 deletions(-) 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([ From b59d9daecbed771e7b0ebc2cca5df162b00e2c7f Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Thu, 13 Aug 2026 12:47:54 +0530 Subject: [PATCH 5/5] fix(tests): retry transient request catcher failures (#13199) --- tests/e2e/Scopes/Scope.php | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/tests/e2e/Scopes/Scope.php b/tests/e2e/Scopes/Scope.php index 34bddeb02f..503a2c0273 100644 --- a/tests/e2e/Scopes/Scope.php +++ b/tests/e2e/Scopes/Scope.php @@ -343,9 +343,12 @@ abstract class Scope extends TestCase } $query = http_build_query($queryParams); + $url = 'http://' . $hostname . ':5000/__find_request__?' . $query; + + for ($attempt = 0; $attempt <= $maxAttempts; $attempt++) { + $response = @file_get_contents($url); + $requests = is_string($response) ? json_decode($response, true) : []; - for ($attempt = 0; $attempt < $maxAttempts; $attempt++) { - $requests = json_decode(file_get_contents('http://' . $hostname . ':5000/__find_request__?' . $query), true); if (is_array($requests)) { for ($i = count($requests) - 1; $i >= 0; $i--) { $request = $this->decodeRequestData($requests[$i]); @@ -369,30 +372,8 @@ abstract class Scope extends TestCase } } - usleep($delayMs * 1000); - } - - $requests = json_decode(file_get_contents('http://' . $hostname . ':5000/__find_request__?' . $query), true); - if (is_array($requests)) { - for ($i = count($requests) - 1; $i >= 0; $i--) { - $request = $this->decodeRequestData($requests[$i]); - if ($probe !== null) { - try { - $probe($request); - return $request; - } catch (\Throwable $error) { - continue; - } - } - - if ($enforceProjectId) { - $requestProjectId = $request['headers']['X-Appwrite-Webhook-Project-Id'] ?? ''; - if ($requestProjectId === $projectId) { - return $request; - } - } else { - return $request; - } + if ($attempt < $maxAttempts) { + usleep($delayMs * 1000); } }