From e6239550077089669696886145c469aeb23c4489 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 28 Aug 2026 11:58:23 +0000 Subject: [PATCH 01/46] Fix TypeError committing bulkDelete with array queries Transaction log queries can be SDK JSON strings or already-decoded {method, attribute, values} arrays. Query::parseQueries() always calls parse(string), which TypeErrors on arrays during commit. Parse each stored query with parseQuery() or parse() so valid array queries commit successfully. Apply the same handling when replaying logs in TransactionState. Fixes CLOUD-3QMK Co-authored-by: Chirag Aggarwal --- src/Appwrite/Databases/TransactionState.php | 34 ++++- .../Http/Databases/Transactions/Update.php | 34 ++++- .../Transactions/TransactionsBase.php | 134 ++++++++++++++++++ 3 files changed, 198 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Databases/TransactionState.php b/src/Appwrite/Databases/TransactionState.php index 8799ef60e5..6ed83865d4 100644 --- a/src/Appwrite/Databases/TransactionState.php +++ b/src/Appwrite/Databases/TransactionState.php @@ -463,7 +463,7 @@ class TransactionState case 'bulkUpdate': if (isset($data['queries']) && isset($data['data'])) { - $queries = Query::parseQueries($data['queries'] ?? []); + $queries = $this->parseStoredQueries($data['queries'] ?? []); $updateData = $data['data']; foreach ($state[$collectionId] ?? [] as $docId => $entry) { @@ -520,7 +520,7 @@ class TransactionState case 'bulkDelete': if (isset($data['queries'])) { - $queries = Query::parseQueries($data['queries'] ?? []); + $queries = $this->parseStoredQueries($data['queries'] ?? []); $filters = $this->extractFilters($queries); foreach ($state[$collectionId] ?? [] as $docId => $entry) { @@ -738,4 +738,34 @@ class TransactionState return true; } + + /** + * Parse queries stored on a transaction log. + * + * Each query may be an SDK JSON string or an already-decoded + * `{method, attribute, values}` array. + * + * @param array $queries + * @return array + * @throws Exception\Query + */ + private function parseStoredQueries(array $queries): array + { + $parsed = []; + + foreach ($queries as $query) { + if (\is_array($query)) { + $parsed[] = Query::parseQuery($query); + continue; + } + + if (!\is_string($query)) { + throw new Exception\Query('Invalid query. Must be a string or array, got ' . \gettype($query)); + } + + $parsed[] = Query::parse($query); + } + + return $parsed; + } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index f70c707c5d..136ae17428 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -860,7 +860,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = Query::parseQueries($data['queries'] ?? []); + $queries = $this->parseStoredQueries($data['queries'] ?? []); $updateData = new Document($data['data']); $dependentDocs = []; @@ -982,7 +982,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = Query::parseQueries($data['queries'] ?? []); + $queries = $this->parseStoredQueries($data['queries'] ?? []); $count = $dbForProject->deleteDocuments( $collectionId, @@ -1007,4 +1007,34 @@ class Update extends Action return $count; } + + /** + * Parse queries stored on a transaction log. + * + * Each query may be an SDK JSON string or an already-decoded + * `{method, attribute, values}` array. + * + * @param array $queries + * @return array + * @throws QueryException + */ + private function parseStoredQueries(array $queries): array + { + $parsed = []; + + foreach ($queries as $query) { + if (\is_array($query)) { + $parsed[] = Query::parseQuery($query); + continue; + } + + if (!\is_string($query)) { + throw new QueryException('Invalid query. Must be a string or array, got ' . \gettype($query)); + } + + $parsed[] = Query::parse($query); + } + + return $parsed; + } } diff --git a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php index a49b9cf25b..b15636f8c2 100644 --- a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php +++ b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php @@ -2668,6 +2668,140 @@ trait TransactionsBase $this->assertEquals(0, $response['body']['total']); } + /** + * Commit bulkUpdate/bulkDelete with SDK query strings and decoded query arrays. + */ + public function testBulkDeleteAndUpdateWithArrayQueries(): void + { + $database = $this->client->call(Client::METHOD_POST, $this->getDatabaseUrl(), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'databaseId' => ID::unique(), + 'name' => 'BulkArrayQueriesTestDB' + ]); + + $databaseId = $database['body']['$id']; + + $collection = $this->client->call(Client::METHOD_POST, $this->getContainerUrl($databaseId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getContainerIdParam() => ID::unique(), + 'name' => 'TestCollection', + 'permissions' => [ + Permission::create(Role::any()), + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ]); + + $collectionId = $collection['body']['$id']; + + if ($this->getSupportForAttributes()) { + $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $collectionId, "string", null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'key' => 'name', + 'size' => 256, + 'required' => true, + ]); + + $this->client->call(Client::METHOD_POST, $this->getSchemaUrl($databaseId, $collectionId, "string", null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'key' => 'category', + 'size' => 256, + 'required' => false, + ]); + $this->waitForAllAttributes($databaseId, $collectionId); + } + + foreach (['keep_1', 'keep_2', 'drop_1', 'drop_2'] as $id) { + $category = \str_starts_with($id, 'keep') ? 'keep' : 'drop'; + $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRecordIdParam() => $id, + 'data' => [ + 'name' => $id, + 'category' => $category, + ] + ]); + } + + $transaction = $this->client->call(Client::METHOD_POST, $this->getTransactionUrl(), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(201, $transaction['headers']['status-code']); + $transactionId = $transaction['body']['$id']; + + $response = $this->client->call(Client::METHOD_POST, $this->getTransactionUrl($transactionId) . "/operations", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'operations' => [ + [ + 'databaseId' => $databaseId, + $this->getContainerIdParam() => $collectionId, + 'action' => 'bulkUpdate', + 'data' => [ + 'queries' => [Query::equal('category', ['keep'])->toArray()], + 'data' => ['name' => 'updated'], + ], + ], + [ + 'databaseId' => $databaseId, + $this->getContainerIdParam() => $collectionId, + 'action' => 'bulkDelete', + 'data' => [ + 'queries' => [ + Query::equal('category', ['drop'])->toString(), + Query::equal('name', ['drop_1', 'drop_2'])->toArray(), + ], + ], + ], + ] + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_PATCH, $this->getTransactionUrl($transactionId), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'commit' => true + ]); + + $this->assertEquals(200, $response['headers']['status-code'], \json_encode($response['body'])); + + $response = $this->client->call(Client::METHOD_GET, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders())); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(2, $response['body']['total']); + + foreach ($response['body'][$this->getRecordResource()] as $doc) { + $this->assertEquals('keep', $doc['category']); + $this->assertEquals('updated', $doc['name']); + } + } + /** * Test multiple single route operations in one transaction */ From b10821738070346da92b412e4bbc40fda341f944 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 30 Aug 2026 23:47:53 +0530 Subject: [PATCH 02/46] fix(vcs): stop the GitHub App installation callback dead-ending GitHub only echoes the state parameter back when the flow finishes through the redirect URI. An organisation member requesting owner approval, and an owner approving that request from GitHub's own settings, both finish on the app's setup URL instead, which carries no state. The callback then threw a bare 400 whose message blamed the user for being an organisation member -- the check is only empty($state) and never looks at the caller's GitHub role, so an owner hit the same page with no way forward. Authorize now mirrors state into a short-lived, one-shot cookie the callback falls back to. The error redirect also used a ':' separator instead of '?', so the console received /settings/git-installations:error=... and could never read the error back, and empty success/failure values in state overrode the computed console defaults, turning the remaining failure branch into a bare 400 as well. The unused setup_action param now distinguishes a submitted request from a plain permission failure. --- app/init/constants.php | 5 + .../Modules/VCS/Http/GitHub/Authorize/Get.php | 17 ++- .../Modules/VCS/Http/GitHub/Callback/Get.php | 80 +++++++++----- .../VCSGitHubCallbackConsoleClientTest.php | 101 ++++++++++++++++++ 4 files changed, 175 insertions(+), 28 deletions(-) create mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php diff --git a/app/init/constants.php b/app/init/constants.php index 0bd3193500..b8d82b3c8e 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -564,6 +564,11 @@ const SCHEDULE_RESOURCE_TYPE_MESSAGE = 'message'; /** Preview cookie */ const COOKIE_NAME_PREVIEW = 'a_jwt_console'; +/** VCS installation state cookie, a fallback for providers that drop the state parameter */ +const COOKIE_NAME_VCS_STATE = 'a_vcs_state'; +const COOKIE_PATH_VCS_STATE = '/v1/vcs'; +const COOKIE_EXPIRY_VCS_STATE = 60 * 10; // 10 minutes + // Cache Reconnect const CACHE_RECONNECT_MAX_RETRIES = 2; const CACHE_RECONNECT_RETRY_DELAY = 1000; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index efa43f4837..2362deb280 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,7 +64,7 @@ class Get extends Action Document $project, array $platform ) { - $state = \json_encode([ + $state = (string) \json_encode([ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, @@ -83,7 +83,22 @@ class Get extends Action 'redirect_uri' => $protocol . '://' . $hostname . "/v1/vcs/github/callback" ]); + // GitHub only echoes the state parameter back when it finishes through the + // redirect URI. Flows that end on the app's setup URL instead -- an + // organisation member requesting owner approval, or an owner approving that + // request -- arrive at the callback with no state at all, so mirror it into a + // short-lived cookie the callback can fall back to. $response + ->addCookie( + COOKIE_NAME_VCS_STATE, + $state, + \time() + COOKIE_EXPIRY_VCS_STATE, + COOKIE_PATH_VCS_STATE, + null, + $protocol === 'https', + true, + Response::COOKIE_SAMESITE_LAX + ) ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') ->redirect($url); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 76bcd4d8ea..de07c33747 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\GitHub\Callback; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; +use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; @@ -38,10 +39,11 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'GitHub installation ID', true) ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) - ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(2048, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) ->inject('vcsFactory') ->inject('project') + ->inject('request') ->inject('response') ->inject('dbForPlatform') ->inject('platform') @@ -55,16 +57,41 @@ class Get extends Action string $code, VcsFactory $vcsFactory, Document $project, + Request $request, Response $response, Database $dbForPlatform, array $platform ) { - if (empty($state)) { - $error = 'Installation requests from organisation members for the Appwrite GitHub App are currently unsupported. To proceed with the installation, login to the Appwrite Console and install the GitHub App.'; - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + + // GitHub only echoes state back when it finishes through the redirect URI. + // Flows that end on the app's setup URL instead -- an organisation member + // requesting owner approval, or an owner approving that request -- arrive + // here with no state, so fall back to the cookie Authorize left behind. + $cookie = $request->getCookie(COOKIE_NAME_VCS_STATE, ''); + + if (!empty($cookie)) { + $state = empty($state) ? $cookie : $state; + + // One shot: a leftover cookie must never attach a later installation + // to the project this browser happened to start from. + $response->addCookie( + COOKIE_NAME_VCS_STATE, + '', + \time() - 3600, + COOKIE_PATH_VCS_STATE, + null, + $protocol === 'https', + true, + Response::COOKIE_SAMESITE_LAX + ); } - $state = \json_decode($state, true); + if (empty($state)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.'); + } + + $state = \json_decode($state, true) ?? []; $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; @@ -73,20 +100,19 @@ class Get extends Action if ($project->isEmpty()) { $error = 'Project with the ID from state could not be found.'; - if (!empty($redirectFailure)) { - $separator = \str_contains($redirectFailure, '?') ? '&' : ':'; - $response - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Pragma', 'no-cache') - ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); - return; + if (empty($redirectFailure)) { + throw new Exception(Exception::PROJECT_NOT_FOUND, $error); } - throw new Exception(Exception::PROJECT_NOT_FOUND, $error); + $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); + return; } $region = $project->getAttribute('region', 'default'); - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = $platform['consoleHostname'] ?? ''; $defaultState = [ @@ -94,10 +120,8 @@ class Get extends Action 'failure' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations", ]; - $state = \array_merge($defaultState, $state ?? []); - - $redirectSuccess = $state['success'] ?? ''; - $redirectFailure = $state['failure'] ?? ''; + $redirectSuccess = empty($state['success']) ? $defaultState['success'] : $state['success']; + $redirectFailure = empty($state['failure']) ? $defaultState['failure'] : $state['failure']; // Create / Update installation if (!empty($providerInstallationId)) { @@ -165,18 +189,20 @@ class Get extends Action ])); } } else { - $error = 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite console.'; + $error = $setupAction === 'request' + ? 'Your installation request was sent to the organization owners for approval. Installing the Appwrite GitHub App on an organization requires an owner, so ask one of them to create the installation from the Appwrite Console.' + : 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.'; - if (!empty($redirectFailure)) { - $separator = \str_contains($redirectFailure, '?') ? '&' : ':'; - $response - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Pragma', 'no-cache') - ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); - return; + if (empty($redirectFailure)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); } - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); + $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); + return; } $response diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php new file mode 100644 index 0000000000..4127397e5c --- /dev/null +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -0,0 +1,101 @@ +getProject()['$id'] . '/settings/git-installations'; + } + + private function getState(): string + { + $redirect = $this->getRedirect(); + + return (string) \json_encode([ + 'projectId' => $this->getProject()['$id'], + 'success' => $redirect, + 'failure' => $redirect, + ]); + } + + /** + * @param array $cookies + * @return array + */ + private function getCallbackHeaders(array $cookies = []): array + { + $headers = \array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); + + foreach ($cookies as $name => $value) { + $headers['cookie'] .= '; ' . $name . '=' . \urlencode($value); + } + + return $headers; + } + + /** + * GitHub sends the user back without an installation id when an organisation + * member can only request the installation. The console has to receive that + * as a query string, not glued onto the path. + */ + public function testCallbackRequest(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'request', + 'state' => $this->getState(), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); + } + + /** + * GitHub drops the state parameter whenever it finishes the flow through the + * app's setup URL, which is what an organisation member's request does. The + * cookie the authorize endpoint left behind has to take over, so the user + * lands back in the console instead of on a bare 400 page. + */ + public function testCallbackMissingState(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders([ + 'a_vcs_state' => $this->getState(), + ]), [ + 'setup_action' => 'request', + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); + } + + /** + * Without state and without the cookie there is no project to redirect to, so + * the error has to at least say what went wrong. + */ + public function testCallbackMissingStateAndCookie(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Missing state parameter', (string) $response['body']); + } +} From 52b96460809e6e00c8910cc5ad2e35ac8601b709 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 30 Aug 2026 23:48:29 +0530 Subject: [PATCH 03/46] fix(vcs): sign the GitHub App installation state /v1/vcs/github/callback is public and took the projectId and both redirect URLs straight out of the state parameter, so anyone could name the project an installation gets attached to, or point the redirect anywhere. The OAuth2 providers already guard this: Authorize/Base signs [projectId, success, failure] with _APP_OPENSSL_KEY_V1 and Callback/Base fails closed on hash_equals. The GitHub App flow predates that and was never brought across. Mirror the same signature into the state Authorize builds and verify it before the projectId is read. This also covers the fallback cookie, since the cookie carries the signed state verbatim. --- .../Modules/VCS/Http/GitHub/Authorize/Get.php | 3 +++ .../Modules/VCS/Http/GitHub/Callback/Get.php | 8 ++++++ .../VCSGitHubCallbackConsoleClientTest.php | 27 ++++++++++++++++++- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 2362deb280..02f04baa2c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,10 +64,13 @@ class Get extends Action Document $project, array $platform ) { + // The callback endpoint is public, so it verifies this signature + // before trusting the projectId and redirect URLs in state. $state = (string) \json_encode([ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); $appName = System::getEnv('_APP_VCS_GITHUB_APP_NAME'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index de07c33747..b8726dff19 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -95,6 +95,14 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; + // This endpoint is public -- without verifying the signature the + // Authorize action put in state, anyone could pass an arbitrary + // projectId here and attach an installation to another project. + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); + if (!\hash_equals($signature, $state['signature'] ?? '')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); + } + $project = $dbForPlatform->getDocument('projects', $projectId); if ($project->isEmpty()) { diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 4127397e5c..dc2ffa7c07 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -8,6 +8,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; +use Utopia\System\System; /** * Failure paths of the GitHub App installation callback. Deliberately does not @@ -26,12 +27,14 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope private function getState(): string { + $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); return (string) \json_encode([ - 'projectId' => $this->getProject()['$id'], + 'projectId' => $projectId, 'success' => $redirect, 'failure' => $redirect, + 'signature' => \hash_hmac('sha256', \json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); } @@ -98,4 +101,26 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertStringContainsString('Missing state parameter', (string) $response['body']); } + + /** + * The endpoint is public, so an unsigned state must not be able to name the + * project an installation gets attached to. + */ + public function testCallbackUnsignedState(): void + { + $redirect = $this->getRedirect(); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => (string) \json_encode([ + 'projectId' => $this->getProject()['$id'], + 'success' => $redirect, + 'failure' => $redirect, + ]), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); + } } From 8d8d9dc33c3b4afb0062a20fc937207ab7934ed2 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 30 Aug 2026 23:49:35 +0530 Subject: [PATCH 04/46] fix(vcs): scope installation repository endpoints to the project The repository endpoints loaded an installation by id and never checked it belongs to the requesting project. Authorization does not backstop this: on admin-mode routes outside /v1/projects*, shared/api.php sets the default authorization status to false, so getDocument('installations', $installationId) returns another tenant's document. Apply the same guard Installations/Get.php, Delete.php and Namespaces/XList.php already use, so a mismatched project gets INSTALLATION_NOT_FOUND rather than another tenant's repositories, file contents, branches or detections. --- .../Http/Installations/Repositories/Branches/XList.php | 8 +++++++- .../VCS/Http/Installations/Repositories/Contents/Get.php | 8 +++++++- .../VCS/Http/Installations/Repositories/Create.php | 8 +++++++- .../Http/Installations/Repositories/Detections/Create.php | 8 +++++++- .../Modules/VCS/Http/Installations/Repositories/Get.php | 8 +++++++- .../Modules/VCS/Http/Installations/Repositories/XList.php | 8 +++++++- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index 58261a78e5..c599f0f6d7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -59,6 +59,7 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -70,7 +71,8 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { try { $queries = Query::parseQueries($queries); @@ -84,6 +86,10 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 89e50559c3..786585635b 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -55,6 +55,7 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -66,7 +67,8 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -74,6 +76,10 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 7f29f0b005..292db0c00b 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -57,6 +57,7 @@ class Create extends Action ->inject('user') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -69,7 +70,8 @@ class Create extends Action InstallationTokens $installationTokens, Document $user, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -77,6 +79,10 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $provider = $installation->getAttribute('provider', 'github'); if ($installation->getAttribute('personal', false) === true) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index 6006e96aae..a124636d5a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -98,6 +98,7 @@ class Create extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -109,7 +110,8 @@ class Create extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -117,6 +119,10 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index e7f340bb19..fae8d4330f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -53,6 +53,7 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -62,7 +63,8 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -70,6 +72,10 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index fc7059ccb0..5cbc037334 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -100,6 +100,7 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -111,7 +112,8 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { if (empty($search)) { $search = ""; @@ -123,6 +125,10 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); From 4e2fb5c9f1a004f34d1a48fe3ac9dc93c0768027 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 31 Aug 2026 00:00:27 +0530 Subject: [PATCH 05/46] fix(vcs): use the state cookie only when there is no installation to attach The cookie is set for ten minutes with SameSite=Lax, so a top-level navigation from another site still sends it. Substituting it for missing state on a request that carries an installation id let that cookie supply the project for an installation it was never bound to; the signature covers the project and redirect URLs, not the installation. Fall back to the cookie only when there is no installation id, which is the organisation-member request it exists for. Callbacks that would persist an installation now require state on the request itself. --- .../Modules/VCS/Http/GitHub/Callback/Get.php | 11 ++++++++--- .../VCSGitHubCallbackConsoleClientTest.php | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index b8726dff19..34f1df70c3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -66,12 +66,17 @@ class Get extends Action // GitHub only echoes state back when it finishes through the redirect URI. // Flows that end on the app's setup URL instead -- an organisation member - // requesting owner approval, or an owner approving that request -- arrive - // here with no state, so fall back to the cookie Authorize left behind. + // requesting owner approval -- arrive here with no state, so fall back to + // the cookie Authorize left behind. The signature covers the project and + // redirect URLs but not the installation, so the cookie is only good for + // reporting the outcome, never for deciding which project an installation + // attaches to. $cookie = $request->getCookie(COOKIE_NAME_VCS_STATE, ''); if (!empty($cookie)) { - $state = empty($state) ? $cookie : $state; + if (empty($state) && empty($providerInstallationId)) { + $state = $cookie; + } // One shot: a leftover cookie must never attach a later installation // to the project this browser happened to start from. diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index dc2ffa7c07..9f73a59c1f 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -123,4 +123,21 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } + + /** + * The cookie is not bound to the installation the callback was handed, so it + * must never stand in for state on a request that would persist one. + */ + public function testCallbackCookieDoesNotBindInstallation(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders([ + 'a_vcs_state' => $this->getState(), + ]), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Missing state parameter', (string) $response['body']); + } } From 66e772eaf4e2e387fbebd8a7b3a3eae455fa7d3a Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 31 Aug 2026 00:22:54 +0530 Subject: [PATCH 06/46] fix(vcs): drop the callback state cookie The cookie guessed that the browser hitting the callback was the one that started the flow, which SameSite=Lax does not guarantee: any top-level navigation from another site sent it. Once the callback refused to let it attach an installation, the only branch it could still reach was the error redirect, so all it bought was a nicer message on a flow that cannot succeed anyway -- an organisation member cannot install the app, an owner has to. It also never helped the case that matters. An owner approving a member's request from GitHub's settings is a different browser with no cookie, so that flow needs reconciliation from the installation webhook rather than anything carried in the request. Missing state now reports itself plainly in every case. --- app/init/constants.php | 5 -- .../Modules/VCS/Http/GitHub/Authorize/Get.php | 15 ------ .../Modules/VCS/Http/GitHub/Callback/Get.php | 34 +------------ .../VCSGitHubCallbackConsoleClientTest.php | 50 ++----------------- 4 files changed, 6 insertions(+), 98 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index b8d82b3c8e..0bd3193500 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -564,11 +564,6 @@ const SCHEDULE_RESOURCE_TYPE_MESSAGE = 'message'; /** Preview cookie */ const COOKIE_NAME_PREVIEW = 'a_jwt_console'; -/** VCS installation state cookie, a fallback for providers that drop the state parameter */ -const COOKIE_NAME_VCS_STATE = 'a_vcs_state'; -const COOKIE_PATH_VCS_STATE = '/v1/vcs'; -const COOKIE_EXPIRY_VCS_STATE = 60 * 10; // 10 minutes - // Cache Reconnect const CACHE_RECONNECT_MAX_RETRIES = 2; const CACHE_RECONNECT_RETRY_DELAY = 1000; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 02f04baa2c..8a9ec57a00 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -86,22 +86,7 @@ class Get extends Action 'redirect_uri' => $protocol . '://' . $hostname . "/v1/vcs/github/callback" ]); - // GitHub only echoes the state parameter back when it finishes through the - // redirect URI. Flows that end on the app's setup URL instead -- an - // organisation member requesting owner approval, or an owner approving that - // request -- arrive at the callback with no state at all, so mirror it into a - // short-lived cookie the callback can fall back to. $response - ->addCookie( - COOKIE_NAME_VCS_STATE, - $state, - \time() + COOKIE_EXPIRY_VCS_STATE, - COOKIE_PATH_VCS_STATE, - null, - $protocol === 'https', - true, - Response::COOKIE_SAMESITE_LAX - ) ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') ->redirect($url); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 34f1df70c3..1ac1c24799 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -5,7 +5,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\GitHub\Callback; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; -use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; @@ -43,7 +42,6 @@ class Get extends Action ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) ->inject('vcsFactory') ->inject('project') - ->inject('request') ->inject('response') ->inject('dbForPlatform') ->inject('platform') @@ -57,41 +55,10 @@ class Get extends Action string $code, VcsFactory $vcsFactory, Document $project, - Request $request, Response $response, Database $dbForPlatform, array $platform ) { - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; - - // GitHub only echoes state back when it finishes through the redirect URI. - // Flows that end on the app's setup URL instead -- an organisation member - // requesting owner approval -- arrive here with no state, so fall back to - // the cookie Authorize left behind. The signature covers the project and - // redirect URLs but not the installation, so the cookie is only good for - // reporting the outcome, never for deciding which project an installation - // attaches to. - $cookie = $request->getCookie(COOKIE_NAME_VCS_STATE, ''); - - if (!empty($cookie)) { - if (empty($state) && empty($providerInstallationId)) { - $state = $cookie; - } - - // One shot: a leftover cookie must never attach a later installation - // to the project this browser happened to start from. - $response->addCookie( - COOKIE_NAME_VCS_STATE, - '', - \time() - 3600, - COOKIE_PATH_VCS_STATE, - null, - $protocol === 'https', - true, - Response::COOKIE_SAMESITE_LAX - ); - } - if (empty($state)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.'); } @@ -126,6 +93,7 @@ class Get extends Action } $region = $project->getAttribute('region', 'default'); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = $platform['consoleHostname'] ?? ''; $defaultState = [ diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 9f73a59c1f..568078d6e2 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -39,18 +39,11 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope } /** - * @param array $cookies * @return array */ - private function getCallbackHeaders(array $cookies = []): array + private function getCallbackHeaders(): array { - $headers = \array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); - - foreach ($cookies as $name => $value) { - $headers['cookie'] .= '; ' . $name . '=' . \urlencode($value); - } - - return $headers; + return \array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); } /** @@ -69,29 +62,12 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); } + /** - * GitHub drops the state parameter whenever it finishes the flow through the - * app's setup URL, which is what an organisation member's request does. The - * cookie the authorize endpoint left behind has to take over, so the user - * lands back in the console instead of on a bare 400 page. + * Without state there is no project to redirect to, so the error has to at + * least say what went wrong. */ public function testCallbackMissingState(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders([ - 'a_vcs_state' => $this->getState(), - ]), [ - 'setup_action' => 'request', - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); - } - - /** - * Without state and without the cookie there is no project to redirect to, so - * the error has to at least say what went wrong. - */ - public function testCallbackMissingStateAndCookie(): void { $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ 'setup_action' => 'install', @@ -124,20 +100,4 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } - /** - * The cookie is not bound to the installation the callback was handed, so it - * must never stand in for state on a request that would persist one. - */ - public function testCallbackCookieDoesNotBindInstallation(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders([ - 'a_vcs_state' => $this->getState(), - ]), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Missing state parameter', (string) $response['body']); - } } From 590f8c7e9a85c0645aa10dce62695262bbcd8abe Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Mon, 31 Aug 2026 00:36:51 +0530 Subject: [PATCH 07/46] test(vcs): follow e2e naming and call conventions in the callback suite Test methods are verb-first everywhere else in tests/e2e, matching the {restVerb}{Qualifier} rule the sibling testGetInstallation follows, and e2e suites call builtins unprefixed rather than with a leading backslash. --- .../VCSGitHubCallbackConsoleClientTest.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 568078d6e2..9d2a326ca4 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -30,11 +30,11 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); - return (string) \json_encode([ + return (string) json_encode([ 'projectId' => $projectId, 'success' => $redirect, 'failure' => $redirect, - 'signature' => \hash_hmac('sha256', \json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); } @@ -43,7 +43,7 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope */ private function getCallbackHeaders(): array { - return \array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); + return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); } /** @@ -51,7 +51,7 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope * member can only request the installation. The console has to receive that * as a query string, not glued onto the path. */ - public function testCallbackRequest(): void + public function testGetCallbackInstallationRequest(): void { $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ 'setup_action' => 'request', @@ -62,12 +62,11 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); } - /** * Without state there is no project to redirect to, so the error has to at * least say what went wrong. */ - public function testCallbackMissingState(): void + public function testGetCallbackMissingState(): void { $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ 'setup_action' => 'install', @@ -82,14 +81,14 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope * The endpoint is public, so an unsigned state must not be able to name the * project an installation gets attached to. */ - public function testCallbackUnsignedState(): void + public function testGetCallbackUnsignedState(): void { $redirect = $this->getRedirect(); $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ 'setup_action' => 'install', 'installation_id' => '1234567', - 'state' => (string) \json_encode([ + 'state' => (string) json_encode([ 'projectId' => $this->getProject()['$id'], 'success' => $redirect, 'failure' => $redirect, @@ -99,5 +98,4 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } - } From c7ad4a7fc0f81d117ec7af369bf39c6317ff1323 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 14:41:37 +0100 Subject: [PATCH 08/46] fix(spec): emit FCM service account JSON as an object, not a string The OpenAPI3 parameter switch matches on the concrete validator class, so Utopia\Validator\JSON\FCM fell through to the string default even though it reports TYPE_OBJECT and the controller still accepts an array, stdClass or JSON string. Swapping the FCM param's JSON validator for the stricter FCM one was meant to tighten validation, but it silently narrowed the published type and turned serviceAccountJSON into a string in every generated SDK. --- .../SDK/Specification/Format/OpenAPI3.php | 1 + tests/unit/SDK/Specification/FormatTest.php | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 1ee1babc96..53151e610c 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -733,6 +733,7 @@ class OpenAPI3 extends Format break; case \Utopia\Validator\JSON::class: case \Utopia\Validator\JSON\ObjectValidator::class: + case \Utopia\Validator\JSON\FCM::class: case \Utopia\Validator\Assoc::class: $node['schema']['type'] = 'object'; $node['schema']['default'] = (empty($param['default'])) ? new \stdClass() : $param['default']; diff --git a/tests/unit/SDK/Specification/FormatTest.php b/tests/unit/SDK/Specification/FormatTest.php index 96d05875cf..5601a70c1d 100644 --- a/tests/unit/SDK/Specification/FormatTest.php +++ b/tests/unit/SDK/Specification/FormatTest.php @@ -63,6 +63,7 @@ use Utopia\Validator\FloatValidator; use Utopia\Validator\HexColor; use Utopia\Validator\Integer as IntegerValidator; use Utopia\Validator\JSON; +use Utopia\Validator\JSON\FCM; use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; @@ -1040,6 +1041,31 @@ final class FormatTest extends TestCase $this->assertSame('object', $openApiQuery['type']); } + public function testObjectValidatorsOutsideTheSwitchAreNotEmittedAsStrings(): void + { + Method::$processed = []; + Method::$errors = []; + + $route = (new Route('POST', '/v1/tests/fcm')) + ->desc('Create FCM provider') + ->label('sdk', new Method( + namespace: 'test', + group: null, + name: 'createFcmProvider', + description: 'Create FCM provider.', + auth: [], + responses: [], + )) + ->param('serviceAccountJSON', null, new Nullable(new FCM()), 'FCM service account JSON.', true) + ->param('bareServiceAccountJSON', null, new FCM(), 'FCM service account JSON.', true); + + $spec = (new OpenAPI3(new Container(), [], [$route], [], [], 0, 'console'))->parse(); + $properties = $spec['paths']['/tests/fcm']['post']['requestBody']['content']['application/json']['schema']['properties']; + + $this->assertSame('object', $properties['serviceAccountJSON']['type']); + $this->assertSame('object', $properties['bareServiceAccountJSON']['type']); + } + public function testJsonAndNullableModelRulesEmitExpectedSchemas(): void { Method::$processed = []; From fc7e0e633db9e6f9776a97ab740ad70d34a6d0ac Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 14:45:01 +0100 Subject: [PATCH 09/46] refactor(spec): honour the declared type in the OpenAPI3 parameter default Rather than adding one more class to the object case, the default branch now emits an object when the validator reports TYPE_OBJECT, so the next object validator without a case of its own does not regress the same way. The listed cases keep their current behaviour: Assoc reports TYPE_ARRAY but is deliberately published as an object, and a type-driven rule applied to them would change that. Across the full cloud route table this changes exactly the two FCM parameters and nothing else. --- .../SDK/Specification/Format/OpenAPI3.php | 12 +++- tests/unit/SDK/Specification/FormatTest.php | 60 ++++++++++++++++++- 2 files changed, 69 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 53151e610c..4410385545 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -733,7 +733,6 @@ class OpenAPI3 extends Format break; case \Utopia\Validator\JSON::class: case \Utopia\Validator\JSON\ObjectValidator::class: - case \Utopia\Validator\JSON\FCM::class: case \Utopia\Validator\Assoc::class: $node['schema']['type'] = 'object'; $node['schema']['default'] = (empty($param['default'])) ? new \stdClass() : $param['default']; @@ -944,6 +943,17 @@ class OpenAPI3 extends Format } break; default: + // The cases above match on the concrete validator class, so a + // validator that declares itself an object but is not listed + // reaches here and would be published as a string. That is how + // JSON\FCM narrowed serviceAccountJSON in every generated SDK. + if ($validator->getType() === Validator::TYPE_OBJECT) { + $node['schema']['type'] = 'object'; + $node['schema']['default'] = empty($param['default']) ? new \stdClass() : $param['default']; + $node['schema']['example'] = ($param['example'] ?? '') !== '' ? $param['example'] : '{}'; + break; + } + $node['schema']['type'] = 'string'; if (($param['example'] ?? '') !== '') { $node['schema']['example'] = $param['example']; diff --git a/tests/unit/SDK/Specification/FormatTest.php b/tests/unit/SDK/Specification/FormatTest.php index 5601a70c1d..c4424d7c0d 100644 --- a/tests/unit/SDK/Specification/FormatTest.php +++ b/tests/unit/SDK/Specification/FormatTest.php @@ -54,6 +54,7 @@ use Utopia\Database\Validator\Spatial; use Utopia\DI\Container; use Utopia\Http\Route; use Utopia\Platform\Enum; +use Utopia\Validator; use Utopia\Validator\AnyOf; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; @@ -69,6 +70,56 @@ use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; +/** + * Stands in for a validator that reports an object but has no case of its own in + * the OpenAPI3 parameter switch, which is the shape that regressed. + */ +class UnlistedObjectValidator extends Validator +{ + public function getDescription(): string + { + return 'Value must be an object'; + } + + public function isArray(): bool + { + return false; + } + + public function getType(): string + { + return self::TYPE_OBJECT; + } + + public function isValid(mixed $value): bool + { + return \is_array($value) || $value instanceof \stdClass; + } +} + +class UnlistedStringValidator extends Validator +{ + public function getDescription(): string + { + return 'Value must be a string'; + } + + public function isArray(): bool + { + return false; + } + + public function getType(): string + { + return self::TYPE_STRING; + } + + public function isValid(mixed $value): bool + { + return \is_string($value); + } +} + class TestFormat extends Format { public function getName(): string @@ -1041,7 +1092,7 @@ final class FormatTest extends TestCase $this->assertSame('object', $openApiQuery['type']); } - public function testObjectValidatorsOutsideTheSwitchAreNotEmittedAsStrings(): void + public function testObjectValidatorsWithoutTheirOwnCaseAreNotEmittedAsStrings(): void { Method::$processed = []; Method::$errors = []; @@ -1057,13 +1108,18 @@ final class FormatTest extends TestCase responses: [], )) ->param('serviceAccountJSON', null, new Nullable(new FCM()), 'FCM service account JSON.', true) - ->param('bareServiceAccountJSON', null, new FCM(), 'FCM service account JSON.', true); + ->param('bareServiceAccountJSON', null, new FCM(), 'FCM service account JSON.', true) + ->param('unlistedObject', null, new UnlistedObjectValidator(), 'An object validator with no case of its own.', true) + ->param('unlistedString', null, new UnlistedStringValidator(), 'A string validator with no case of its own.', true); $spec = (new OpenAPI3(new Container(), [], [$route], [], [], 0, 'console'))->parse(); $properties = $spec['paths']['/tests/fcm']['post']['requestBody']['content']['application/json']['schema']['properties']; $this->assertSame('object', $properties['serviceAccountJSON']['type']); $this->assertSame('object', $properties['bareServiceAccountJSON']['type']); + $this->assertSame('object', $properties['unlistedObject']['type']); + $this->assertEquals(new \stdClass(), $properties['unlistedObject']['default']); + $this->assertSame('string', $properties['unlistedString']['type']); } public function testJsonAndNullableModelRulesEmitExpectedSchemas(): void From d68394c166afe781a0e81e3af470ac5fe23fc786 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 14:45:56 +0100 Subject: [PATCH 10/46] test(spec): drop the OpenAPI3 object-type regression test --- tests/unit/SDK/Specification/FormatTest.php | 82 --------------------- 1 file changed, 82 deletions(-) diff --git a/tests/unit/SDK/Specification/FormatTest.php b/tests/unit/SDK/Specification/FormatTest.php index c4424d7c0d..96d05875cf 100644 --- a/tests/unit/SDK/Specification/FormatTest.php +++ b/tests/unit/SDK/Specification/FormatTest.php @@ -54,7 +54,6 @@ use Utopia\Database\Validator\Spatial; use Utopia\DI\Container; use Utopia\Http\Route; use Utopia\Platform\Enum; -use Utopia\Validator; use Utopia\Validator\AnyOf; use Utopia\Validator\ArrayList; use Utopia\Validator\Assoc; @@ -64,62 +63,11 @@ use Utopia\Validator\FloatValidator; use Utopia\Validator\HexColor; use Utopia\Validator\Integer as IntegerValidator; use Utopia\Validator\JSON; -use Utopia\Validator\JSON\FCM; use Utopia\Validator\Nullable; use Utopia\Validator\Range; use Utopia\Validator\Text; use Utopia\Validator\WhiteList; -/** - * Stands in for a validator that reports an object but has no case of its own in - * the OpenAPI3 parameter switch, which is the shape that regressed. - */ -class UnlistedObjectValidator extends Validator -{ - public function getDescription(): string - { - return 'Value must be an object'; - } - - public function isArray(): bool - { - return false; - } - - public function getType(): string - { - return self::TYPE_OBJECT; - } - - public function isValid(mixed $value): bool - { - return \is_array($value) || $value instanceof \stdClass; - } -} - -class UnlistedStringValidator extends Validator -{ - public function getDescription(): string - { - return 'Value must be a string'; - } - - public function isArray(): bool - { - return false; - } - - public function getType(): string - { - return self::TYPE_STRING; - } - - public function isValid(mixed $value): bool - { - return \is_string($value); - } -} - class TestFormat extends Format { public function getName(): string @@ -1092,36 +1040,6 @@ final class FormatTest extends TestCase $this->assertSame('object', $openApiQuery['type']); } - public function testObjectValidatorsWithoutTheirOwnCaseAreNotEmittedAsStrings(): void - { - Method::$processed = []; - Method::$errors = []; - - $route = (new Route('POST', '/v1/tests/fcm')) - ->desc('Create FCM provider') - ->label('sdk', new Method( - namespace: 'test', - group: null, - name: 'createFcmProvider', - description: 'Create FCM provider.', - auth: [], - responses: [], - )) - ->param('serviceAccountJSON', null, new Nullable(new FCM()), 'FCM service account JSON.', true) - ->param('bareServiceAccountJSON', null, new FCM(), 'FCM service account JSON.', true) - ->param('unlistedObject', null, new UnlistedObjectValidator(), 'An object validator with no case of its own.', true) - ->param('unlistedString', null, new UnlistedStringValidator(), 'A string validator with no case of its own.', true); - - $spec = (new OpenAPI3(new Container(), [], [$route], [], [], 0, 'console'))->parse(); - $properties = $spec['paths']['/tests/fcm']['post']['requestBody']['content']['application/json']['schema']['properties']; - - $this->assertSame('object', $properties['serviceAccountJSON']['type']); - $this->assertSame('object', $properties['bareServiceAccountJSON']['type']); - $this->assertSame('object', $properties['unlistedObject']['type']); - $this->assertEquals(new \stdClass(), $properties['unlistedObject']['default']); - $this->assertSame('string', $properties['unlistedString']['type']); - } - public function testJsonAndNullableModelRulesEmitExpectedSchemas(): void { Method::$processed = []; From 0891542ae18040de1d8242751aff6ce25c0b95cb Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 14:47:21 +0100 Subject: [PATCH 11/46] refactor(spec): drop the object cases the parameter default now covers JSON and JSON\ObjectValidator both report TYPE_OBJECT, so the default emits exactly what their cases did. Assoc keeps its case because it reports TYPE_ARRAY and is published as an object on purpose. --- src/Appwrite/SDK/Specification/Format/OpenAPI3.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index 4410385545..294e2d72ca 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -731,9 +731,9 @@ class OpenAPI3 extends Format $node['schema']['format'] = 'url'; $node['schema']['example'] = ($param['example'] ?? '') !== '' ? $param['example'] : 'https://example.com'; break; - case \Utopia\Validator\JSON::class: - case \Utopia\Validator\JSON\ObjectValidator::class: case \Utopia\Validator\Assoc::class: + // Assoc reports TYPE_ARRAY, so only an explicit case publishes + // it as an object. TYPE_OBJECT is handled by the default. $node['schema']['type'] = 'object'; $node['schema']['default'] = (empty($param['default'])) ? new \stdClass() : $param['default']; $node['schema']['example'] = ($param['example'] ?? '') !== '' ? $param['example'] : '{}'; @@ -943,10 +943,6 @@ class OpenAPI3 extends Format } break; default: - // The cases above match on the concrete validator class, so a - // validator that declares itself an object but is not listed - // reaches here and would be published as a string. That is how - // JSON\FCM narrowed serviceAccountJSON in every generated SDK. if ($validator->getType() === Validator::TYPE_OBJECT) { $node['schema']['type'] = 'object'; $node['schema']['default'] = empty($param['default']) ? new \stdClass() : $param['default']; From d67dd4ea42689eced9f6f5d54b5fe2090309a272 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 19:18:45 +0100 Subject: [PATCH 12/46] Test transaction replay with array queries --- src/Appwrite/Databases/TransactionState.php | 2 +- .../Http/Databases/Transactions/Update.php | 33 ++----------------- .../Transactions/TransactionsBase.php | 18 +++++++++- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/Appwrite/Databases/TransactionState.php b/src/Appwrite/Databases/TransactionState.php index 6ed83865d4..c3082f8e0f 100644 --- a/src/Appwrite/Databases/TransactionState.php +++ b/src/Appwrite/Databases/TransactionState.php @@ -749,7 +749,7 @@ class TransactionState * @return array * @throws Exception\Query */ - private function parseStoredQueries(array $queries): array + public function parseStoredQueries(array $queries): array { $parsed = []; diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index 136ae17428..c8efd4680e 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -860,7 +860,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = $this->parseStoredQueries($data['queries'] ?? []); + $queries = $transactionState->parseStoredQueries($data['queries'] ?? []); $updateData = new Document($data['data']); $dependentDocs = []; @@ -982,7 +982,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = $this->parseStoredQueries($data['queries'] ?? []); + $queries = $transactionState->parseStoredQueries($data['queries'] ?? []); $count = $dbForProject->deleteDocuments( $collectionId, @@ -1008,33 +1008,4 @@ class Update extends Action return $count; } - /** - * Parse queries stored on a transaction log. - * - * Each query may be an SDK JSON string or an already-decoded - * `{method, attribute, values}` array. - * - * @param array $queries - * @return array - * @throws QueryException - */ - private function parseStoredQueries(array $queries): array - { - $parsed = []; - - foreach ($queries as $query) { - if (\is_array($query)) { - $parsed[] = Query::parseQuery($query); - continue; - } - - if (!\is_string($query)) { - throw new QueryException('Invalid query. Must be a string or array, got ' . \gettype($query)); - } - - $parsed[] = Query::parse($query); - } - - return $parsed; - } } diff --git a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php index b15636f8c2..0361cc7031 100644 --- a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php +++ b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php @@ -2671,8 +2671,9 @@ trait TransactionsBase /** * Commit bulkUpdate/bulkDelete with SDK query strings and decoded query arrays. */ - public function testBulkDeleteAndUpdateWithArrayQueries(): void + public function testUpdateBulkOperationsWithArrayQueries(): void { + // Test for SUCCESS $database = $this->client->call(Client::METHOD_POST, $this->getDatabaseUrl(), array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2778,6 +2779,21 @@ trait TransactionsBase $this->assertEquals(201, $response['headers']['status-code']); + $response = $this->client->call(Client::METHOD_GET, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), [ + 'transactionId' => $transactionId + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals(2, $response['body']['total']); + + foreach ($response['body'][$this->getRecordResource()] as $doc) { + $this->assertEquals('keep', $doc['category']); + $this->assertEquals('updated', $doc['name']); + } + $response = $this->client->call(Client::METHOD_PATCH, $this->getTransactionUrl($transactionId), array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], From 208c47c573c3357078f93ec2d560ff1434c853cf Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 19:34:52 +0100 Subject: [PATCH 13/46] Reject array queries in transaction operations --- .../Utopia/Database/Validator/Operation.php | 6 ++ .../Transactions/TransactionsBase.php | 64 ++++++------------- 2 files changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Appwrite/Utopia/Database/Validator/Operation.php b/src/Appwrite/Utopia/Database/Validator/Operation.php index 6d50611708..6d85fd703c 100644 --- a/src/Appwrite/Utopia/Database/Validator/Operation.php +++ b/src/Appwrite/Utopia/Database/Validator/Operation.php @@ -177,6 +177,12 @@ class Operation extends Validator $this->description = "Key 'queries' must be an array for {$action}"; return false; } + foreach ($value['data']['queries'] as $query) { + if (!\is_string($query)) { + $this->description = "Key 'data.queries' must contain only strings for {$action}"; + return false; + } + } } // BulkUpdate requires both queries and data diff --git a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php index 0361cc7031..1e68f16ebb 100644 --- a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php +++ b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php @@ -2669,11 +2669,11 @@ trait TransactionsBase } /** - * Commit bulkUpdate/bulkDelete with SDK query strings and decoded query arrays. + * Reject bulkUpdate/bulkDelete with decoded query arrays. */ - public function testUpdateBulkOperationsWithArrayQueries(): void + public function testCreateBulkOperationsWithArrayQueries(): void { - // Test for SUCCESS + // Test for FAILURE $database = $this->client->call(Client::METHOD_POST, $this->getDatabaseUrl(), array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], @@ -2763,59 +2763,31 @@ trait TransactionsBase 'data' => ['name' => 'updated'], ], ], + ] + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertEquals('general_argument_invalid', $response['body']['type']); + + $response = $this->client->call(Client::METHOD_POST, $this->getTransactionUrl($transactionId) . "/operations", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'operations' => [ [ 'databaseId' => $databaseId, $this->getContainerIdParam() => $collectionId, 'action' => 'bulkDelete', 'data' => [ - 'queries' => [ - Query::equal('category', ['drop'])->toString(), - Query::equal('name', ['drop_1', 'drop_2'])->toArray(), - ], + 'queries' => [Query::equal('category', ['drop'])->toArray()], ], ], ] ]); - $this->assertEquals(201, $response['headers']['status-code']); - - $response = $this->client->call(Client::METHOD_GET, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders()), [ - 'transactionId' => $transactionId - ]); - - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(2, $response['body']['total']); - - foreach ($response['body'][$this->getRecordResource()] as $doc) { - $this->assertEquals('keep', $doc['category']); - $this->assertEquals('updated', $doc['name']); - } - - $response = $this->client->call(Client::METHOD_PATCH, $this->getTransactionUrl($transactionId), array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'] - ]), [ - 'commit' => true - ]); - - $this->assertEquals(200, $response['headers']['status-code'], \json_encode($response['body'])); - - $response = $this->client->call(Client::METHOD_GET, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - ], $this->getHeaders())); - - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertEquals(2, $response['body']['total']); - - foreach ($response['body'][$this->getRecordResource()] as $doc) { - $this->assertEquals('keep', $doc['category']); - $this->assertEquals('updated', $doc['name']); - } + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertEquals('general_argument_invalid', $response['body']['type']); } /** From 8182b12c9250a7a30662f1d9fd632140295f4ab5 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 31 Aug 2026 20:05:24 +0100 Subject: [PATCH 14/46] Enforce transaction query input at validation --- src/Appwrite/Databases/TransactionState.php | 34 ++----------------- .../Http/Databases/Transactions/Update.php | 5 ++- 2 files changed, 4 insertions(+), 35 deletions(-) diff --git a/src/Appwrite/Databases/TransactionState.php b/src/Appwrite/Databases/TransactionState.php index c3082f8e0f..8799ef60e5 100644 --- a/src/Appwrite/Databases/TransactionState.php +++ b/src/Appwrite/Databases/TransactionState.php @@ -463,7 +463,7 @@ class TransactionState case 'bulkUpdate': if (isset($data['queries']) && isset($data['data'])) { - $queries = $this->parseStoredQueries($data['queries'] ?? []); + $queries = Query::parseQueries($data['queries'] ?? []); $updateData = $data['data']; foreach ($state[$collectionId] ?? [] as $docId => $entry) { @@ -520,7 +520,7 @@ class TransactionState case 'bulkDelete': if (isset($data['queries'])) { - $queries = $this->parseStoredQueries($data['queries'] ?? []); + $queries = Query::parseQueries($data['queries'] ?? []); $filters = $this->extractFilters($queries); foreach ($state[$collectionId] ?? [] as $docId => $entry) { @@ -738,34 +738,4 @@ class TransactionState return true; } - - /** - * Parse queries stored on a transaction log. - * - * Each query may be an SDK JSON string or an already-decoded - * `{method, attribute, values}` array. - * - * @param array $queries - * @return array - * @throws Exception\Query - */ - public function parseStoredQueries(array $queries): array - { - $parsed = []; - - foreach ($queries as $query) { - if (\is_array($query)) { - $parsed[] = Query::parseQuery($query); - continue; - } - - if (!\is_string($query)) { - throw new Exception\Query('Invalid query. Must be a string or array, got ' . \gettype($query)); - } - - $parsed[] = Query::parse($query); - } - - return $parsed; - } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php index c8efd4680e..f70c707c5d 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php @@ -860,7 +860,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = $transactionState->parseStoredQueries($data['queries'] ?? []); + $queries = Query::parseQueries($data['queries'] ?? []); $updateData = new Document($data['data']); $dependentDocs = []; @@ -982,7 +982,7 @@ class Update extends Action \DateTime $createdAt, array &$state ): int { - $queries = $transactionState->parseStoredQueries($data['queries'] ?? []); + $queries = Query::parseQueries($data['queries'] ?? []); $count = $dbForProject->deleteDocuments( $collectionId, @@ -1007,5 +1007,4 @@ class Update extends Action return $count; } - } From 8e06b0efee81de167652c16107e841aead00d161 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 1 Sep 2026 13:58:30 +0530 Subject: [PATCH 15/46] Revert the security changes into their own PRs The project scoping on the repository endpoints and the state signature are separate concerns from the misleading installation error, and each wants its own E2E coverage. Split out so this PR carries only the callback behaviour. --- .../Modules/VCS/Http/GitHub/Authorize/Get.php | 3 --- .../Modules/VCS/Http/GitHub/Callback/Get.php | 8 ------ .../Repositories/Branches/XList.php | 8 +----- .../Repositories/Contents/Get.php | 8 +----- .../Installations/Repositories/Create.php | 8 +----- .../Repositories/Detections/Create.php | 8 +----- .../Http/Installations/Repositories/Get.php | 8 +----- .../Http/Installations/Repositories/XList.php | 8 +----- .../VCSGitHubCallbackConsoleClientTest.php | 27 +------------------ 9 files changed, 7 insertions(+), 79 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 8a9ec57a00..654cc7f8e8 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,13 +64,10 @@ class Get extends Action Document $project, array $platform ) { - // The callback endpoint is public, so it verifies this signature - // before trusting the projectId and redirect URLs in state. $state = (string) \json_encode([ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, - 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); $appName = System::getEnv('_APP_VCS_GITHUB_APP_NAME'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 1ac1c24799..958f9ba53a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -67,14 +67,6 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; - // This endpoint is public -- without verifying the signature the - // Authorize action put in state, anyone could pass an arbitrary - // projectId here and attach an installation to another project. - $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); - if (!\hash_equals($signature, $state['signature'] ?? '')) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); - } - $project = $dbForPlatform->getDocument('projects', $projectId); if ($project->isEmpty()) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index c599f0f6d7..58261a78e5 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -59,7 +59,6 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -71,8 +70,7 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { try { $queries = Query::parseQueries($queries); @@ -86,10 +84,6 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 786585635b..89e50559c3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -55,7 +55,6 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -67,8 +66,7 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -76,10 +74,6 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 292db0c00b..7f29f0b005 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -57,7 +57,6 @@ class Create extends Action ->inject('user') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -70,8 +69,7 @@ class Create extends Action InstallationTokens $installationTokens, Document $user, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -79,10 +77,6 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $provider = $installation->getAttribute('provider', 'github'); if ($installation->getAttribute('personal', false) === true) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index a124636d5a..6006e96aae 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -98,7 +98,6 @@ class Create extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -110,8 +109,7 @@ class Create extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -119,10 +117,6 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index fae8d4330f..e7f340bb19 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -53,7 +53,6 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -63,8 +62,7 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -72,10 +70,6 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index 5cbc037334..fc7059ccb0 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -100,7 +100,6 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') - ->inject('project') ->callback($this->action(...)); } @@ -112,8 +111,7 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform, - Document $project + Database $dbForPlatform ) { if (empty($search)) { $search = ""; @@ -125,10 +123,6 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } - if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { - throw new Exception(Exception::INSTALLATION_NOT_FOUND); - } - $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 9d2a326ca4..be7e3a4293 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -8,7 +8,6 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; -use Utopia\System\System; /** * Failure paths of the GitHub App installation callback. Deliberately does not @@ -27,14 +26,12 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope private function getState(): string { - $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); return (string) json_encode([ - 'projectId' => $projectId, + 'projectId' => $this->getProject()['$id'], 'success' => $redirect, 'failure' => $redirect, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); } @@ -76,26 +73,4 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertStringContainsString('Missing state parameter', (string) $response['body']); } - - /** - * The endpoint is public, so an unsigned state must not be able to name the - * project an installation gets attached to. - */ - public function testGetCallbackUnsignedState(): void - { - $redirect = $this->getRedirect(); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => (string) json_encode([ - 'projectId' => $this->getProject()['$id'], - 'success' => $redirect, - 'failure' => $redirect, - ]), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); - } } From 7e52012a72c877b49b7e2bce76c93d06abe85cfb Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 1 Sep 2026 13:59:25 +0530 Subject: [PATCH 16/46] Restore the unchanged authorize state encoding The cast came in with the fallback cookie and has no reader now that state is only appended to the redirect query. --- src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 654cc7f8e8..efa43f4837 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,7 +64,7 @@ class Get extends Action Document $project, array $platform ) { - $state = (string) \json_encode([ + $state = \json_encode([ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, From ec1d091ad85fa67c51bd1b7febebcb281bb0a051 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Sun, 30 Aug 2026 23:49:35 +0530 Subject: [PATCH 17/46] fix(vcs): scope installation repository endpoints to the project The repository endpoints loaded an installation by id and never checked it belongs to the requesting project. Authorization does not backstop this: on admin-mode routes outside /v1/projects*, shared/api.php sets the default authorization status to false, so getDocument('installations', $installationId) returns another tenant's document. Apply the same guard Installations/Get.php, Delete.php and Namespaces/XList.php already use, so a mismatched project gets INSTALLATION_NOT_FOUND rather than another tenant's repositories, file contents, branches or detections. --- .../Http/Installations/Repositories/Branches/XList.php | 8 +++++++- .../VCS/Http/Installations/Repositories/Contents/Get.php | 8 +++++++- .../VCS/Http/Installations/Repositories/Create.php | 8 +++++++- .../Http/Installations/Repositories/Detections/Create.php | 8 +++++++- .../Modules/VCS/Http/Installations/Repositories/Get.php | 8 +++++++- .../Modules/VCS/Http/Installations/Repositories/XList.php | 8 +++++++- 6 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php index 58261a78e5..c599f0f6d7 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Branches/XList.php @@ -59,6 +59,7 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -70,7 +71,8 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { try { $queries = Query::parseQueries($queries); @@ -84,6 +86,10 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php index 89e50559c3..786585635b 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Contents/Get.php @@ -55,6 +55,7 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -66,7 +67,8 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -74,6 +76,10 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php index 7f29f0b005..292db0c00b 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -57,6 +57,7 @@ class Create extends Action ->inject('user') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -69,7 +70,8 @@ class Create extends Action InstallationTokens $installationTokens, Document $user, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -77,6 +79,10 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $provider = $installation->getAttribute('provider', 'github'); if ($installation->getAttribute('personal', false) === true) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php index 6006e96aae..a124636d5a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Detections/Create.php @@ -98,6 +98,7 @@ class Create extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -109,7 +110,8 @@ class Create extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -117,6 +119,10 @@ class Create extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php index e7f340bb19..fae8d4330f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Get.php @@ -53,6 +53,7 @@ class Get extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -62,7 +63,8 @@ class Get extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { $installation = $dbForPlatform->getDocument('installations', $installationId); @@ -70,6 +72,10 @@ class Get extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php index fc7059ccb0..5cbc037334 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/XList.php @@ -100,6 +100,7 @@ class XList extends Action ->inject('installationTokens') ->inject('response') ->inject('dbForPlatform') + ->inject('project') ->callback($this->action(...)); } @@ -111,7 +112,8 @@ class XList extends Action VcsFactory $vcsFactory, InstallationTokens $installationTokens, Response $response, - Database $dbForPlatform + Database $dbForPlatform, + Document $project ) { if (empty($search)) { $search = ""; @@ -123,6 +125,10 @@ class XList extends Action throw new Exception(Exception::INSTALLATION_NOT_FOUND); } + if ($installation->getAttribute('projectInternalId') !== $project->getSequence()) { + throw new Exception(Exception::INSTALLATION_NOT_FOUND); + } + $installation = $installationTokens->refreshForInstallation($installation, $dbForPlatform, $vcsFactory); $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); From 33fbc8fd4975435be33326152914905ca370bab4 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 10:22:53 +0530 Subject: [PATCH 18/46] test(vcs): cover cross-tenant installation isolation end to end Two console users with their own teams, projects and Gitea accounts: each connects an installation, neither can use the other's through any of the six repository endpoints even knowing its id, and the probe by the other tenant's project id dies on team membership before reaching VCS code. Inviting the second user into the first team and accepting via the emailed secret makes the same project-scoped request succeed, while the installation still resolves only under its own project. The Gitea OAuth2 helpers move from VCSGiteaConsoleClientTest into VCSGiteaBase and take the project, headers and Gitea account as parameters, so suites can run the dance as any tenant. A second Gitea user is created through the admin API with must_change_password disabled, since the default would turn the authorize redirect into the change-password page. --- tests/e2e/Services/VCSGitea/VCSGiteaBase.php | 274 ++++++++++++++++++ .../VCSGitea/VCSGiteaConsoleClientTest.php | 139 --------- ...VCSGiteaInstallationsConsoleClientTest.php | 174 +++++++++++ 3 files changed, 448 insertions(+), 139 deletions(-) create mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php index 34a63e1010..859ae7a280 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php @@ -2,10 +2,18 @@ namespace Tests\E2E\Services\VCSGitea; +use Tests\E2E\Client; +use Utopia\Database\Helpers\ID; use Utopia\System\System; trait VCSGiteaBase { + // Admin user created by dev/gitea/setup.sh (run by the CI Gitea job) + protected const GITEA_USERNAME = 'appwrite'; + protected const GITEA_PASSWORD = 'password'; + + protected array $giteaCookies = []; + protected function setUp(): void { parent::setUp(); @@ -14,4 +22,270 @@ trait VCSGiteaBase $this->markTestSkipped('Gitea VCS is not configured.'); } } + + /** + * Create a console user with their own team and project, so tests can act + * as two unrelated tenants side by side. + * + * @return array{userId: string, email: string, session: string, teamId: string, projectId: string, headers: array} + */ + protected function createTenantHelper(): array + { + $email = \uniqid('tenant-', true) . \getmypid() . \bin2hex(\random_bytes(4)) . '@localhost.test'; + $password = 'password'; + + $user = $this->client->call(Client::METHOD_POST, '/account', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'userId' => ID::unique(), + 'email' => $email, + 'password' => $password, + 'name' => 'VCS Tenant', + ]); + $this->assertEquals(201, $user['headers']['status-code']); + + $session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'email' => $email, + 'password' => $password, + ]); + $this->assertEquals(201, $session['headers']['status-code']); + $this->assertNotEmpty($session['cookies']['a_session_console'] ?? ''); + $sessionCookie = $session['cookies']['a_session_console']; + + // Sessions propagate slowly under parallel load, so retry 401s like ProjectCustom does + $team = null; + for ($i = 0; $i < 5; $i++) { + $team = $this->client->call(Client::METHOD_POST, '/teams', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-project' => 'console', + ], [ + 'teamId' => ID::unique(), + 'name' => 'VCS Tenant Team', + ]); + + if ($team['headers']['status-code'] !== 401) { + break; + } + + \usleep(500000); + } + $this->assertEquals(201, $team['headers']['status-code']); + + $project = null; + for ($i = 0; $i < 5; $i++) { + $project = $this->client->call(Client::METHOD_POST, '/projects', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-project' => 'console', + ], [ + 'projectId' => ID::unique(), + 'region' => System::getEnv('_APP_REGION', 'default'), + 'name' => 'VCS Tenant Project', + 'teamId' => $team['body']['$id'], + ]); + + if ($project['headers']['status-code'] !== 401) { + break; + } + + \usleep(500000); + } + $this->assertEquals(201, $project['headers']['status-code']); + + return [ + 'userId' => $user['body']['$id'], + 'email' => $email, + 'session' => $sessionCookie, + 'teamId' => $team['body']['$id'], + 'projectId' => $project['body']['$id'], + 'headers' => [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-mode' => 'admin', + ], + ]; + } + + protected function createGiteaUserHelper(string $username, string $password): void + { + $response = $this->giteaApiHelper(Client::METHOD_POST, '/api/v1/admin/users', [ + 'username' => $username, + 'email' => $username . '@localhost.test', + 'password' => $password, + // Defaults to true, which turns the OAuth2 authorize into a + // redirect to the change-password page instead of the callback. + 'must_change_password' => false, + ]); + + // 422 means the user survived a previous run; the Gitea volume persists. + $this->assertContains($response['headers']['status-code'], [201, 422], \json_encode($response['body'])); + } + + /** + * Walk the OAuth2 dance against the local Gitea up to the grant and return + * what the callback expects, asserting every hop on the way. + * + * @return array{code: string, state: string, consoleUrl: string} + */ + protected function authorizeGiteaHelper(?string $projectId = null, ?array $headers = null, string $username = self::GITEA_USERNAME, string $password = self::GITEA_PASSWORD): array + { + $projectId ??= $this->getProject()['$id']; + $headers ??= $this->getHeaders(); + + // Start each dance from a fresh jar; Gitea reuses session cookies, so a + // stale jar would silently keep the previous user logged in. + $this->giteaCookies = []; + + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers), [ + 'success' => $consoleUrl, + 'failure' => $consoleUrl, + ], true, false); + + $this->assertEquals(301, $authorize['headers']['status-code']); + + $loginUrl = $authorize['headers']['location'] ?? ''; + $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); + + $query = []; + \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); + $this->assertNotEmpty($query['client_id'] ?? ''); + $this->assertNotEmpty($query['redirect_uri'] ?? ''); + $this->assertNotEmpty($query['state'] ?? ''); + $this->assertSame('code', $query['response_type'] ?? ''); + + // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); + $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); + + $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'user_name' => $username, + 'password' => $password, + ]); + $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); + $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); + + // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them + $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); + $consent = $this->giteaCallHelper($gitea, Client::METHOD_GET, $authorizePath); + + if (\in_array($consent['headers']['status-code'], [302, 303])) { + // Already granted: Gitea redirects straight back with a code + $redirect = $consent['headers']['location'] ?? ''; + } else { + $this->assertEquals(200, $consent['headers']['status-code']); + + $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'client_id' => $query['client_id'], + 'state' => $query['state'], + 'scope' => $query['scope'] ?? '', + 'nonce' => '', + 'redirect_uri' => $query['redirect_uri'], + ]); + $this->assertEquals(303, $grant['headers']['status-code']); + + $redirect = $grant['headers']['location'] ?? ''; + } + + $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); + + $callbackQuery = []; + \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); + $this->assertNotEmpty($callbackQuery['code'] ?? ''); + $this->assertNotEmpty($callbackQuery['state'] ?? ''); + + return [ + 'code' => $callbackQuery['code'], + 'state' => $callbackQuery['state'], + 'consoleUrl' => $consoleUrl, + ]; + } + + /** + * Complete the OAuth2 dance and return the resulting installation. + */ + protected function createInstallationHelper(?string $projectId = null, ?array $headers = null, string $username = self::GITEA_USERNAME, string $password = self::GITEA_PASSWORD): array + { + $projectId ??= $this->getProject()['$id']; + $headers ??= $this->getHeaders(); + + ['code' => $code, 'state' => $state, 'consoleUrl' => $consoleUrl] = $this->authorizeGiteaHelper($projectId, $headers, $username, $password); + + $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers), [ + 'code' => $code, + 'state' => $state, + ], true, false); + + $this->assertEquals(301, $callback['headers']['status-code']); + $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); + + $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers)); + + $this->assertEquals(200, $installations['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $installations['body']['total']); + + foreach ($installations['body']['installations'] as $installation) { + if ($installation['provider'] === 'gitea') { + return $installation; + } + } + + $this->fail('Gitea installation not found in listInstallations.'); + } + + // Client does not persist cookies between calls, so carry them manually and never follow redirects + protected function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array + { + $headers = []; + + if (!empty($this->giteaCookies)) { + $headers['cookie'] = \implode('; ', \array_map( + fn (string $name) => $name . '=' . $this->giteaCookies[$name], + \array_keys($this->giteaCookies) + )); + } + + if ($method !== Client::METHOD_GET) { + $headers['content-type'] = 'application/x-www-form-urlencoded'; + } + + $response = $gitea->call($method, $path, $headers, $params, false, false); + + $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); + + return $response; + } + + protected function giteaApiHelper(string $method, string $path, array $params = []): array + { + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + return $gitea->call($method, $path, [ + 'content-type' => 'application/json', + 'authorization' => 'Basic ' . \base64_encode(self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD), + ], $params); + } } diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 3b65774a78..b9113161d0 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -19,12 +19,6 @@ final class VCSGiteaConsoleClientTest extends Scope use ProjectCustom; use SideConsole; - // Admin user created by gitea-bootstrap (docker-compose.override.yml, `gitea` profile) - private const GITEA_USERNAME = 'appwrite'; - private const GITEA_PASSWORD = 'password'; - - private array $giteaCookies = []; - public function testCreateInstallation(): void { $installation = $this->createInstallationHelper(); @@ -109,139 +103,6 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-v2'), 30000, 1000); } - /** - * Walk the full OAuth2 dance against the local Gitea and return the - * resulting installation, asserting every hop on the way. - */ - private function createInstallationHelper(): array - { - $projectId = $this->getProject()['$id']; - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; - - $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), [ - 'success' => $consoleUrl, - 'failure' => $consoleUrl, - ], true, false); - - $this->assertEquals(301, $authorize['headers']['status-code']); - - $loginUrl = $authorize['headers']['location'] ?? ''; - $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); - - $query = []; - \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); - $this->assertNotEmpty($query['client_id'] ?? ''); - $this->assertNotEmpty($query['redirect_uri'] ?? ''); - $this->assertNotEmpty($query['state'] ?? ''); - $this->assertSame('code', $query['response_type'] ?? ''); - - // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query - $gitea = new Client(); - $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); - - $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); - $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); - - $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ - '_csrf' => $this->giteaCookies['_csrf'], - 'user_name' => self::GITEA_USERNAME, - 'password' => self::GITEA_PASSWORD, - ]); - $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); - $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); - - // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them - $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); - $consent = $this->giteaCallHelper($gitea, Client::METHOD_GET, $authorizePath); - - if (\in_array($consent['headers']['status-code'], [302, 303])) { - // Already granted: Gitea redirects straight back with a code - $redirect = $consent['headers']['location'] ?? ''; - } else { - $this->assertEquals(200, $consent['headers']['status-code']); - - $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ - '_csrf' => $this->giteaCookies['_csrf'], - 'client_id' => $query['client_id'], - 'state' => $query['state'], - 'scope' => $query['scope'] ?? '', - 'nonce' => '', - 'redirect_uri' => $query['redirect_uri'], - ]); - $this->assertEquals(303, $grant['headers']['status-code']); - - $redirect = $grant['headers']['location'] ?? ''; - } - - $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); - - $callbackQuery = []; - \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); - $this->assertNotEmpty($callbackQuery['code'] ?? ''); - $this->assertNotEmpty($callbackQuery['state'] ?? ''); - - $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), [ - 'code' => $callbackQuery['code'], - 'state' => $callbackQuery['state'], - ], true, false); - - $this->assertEquals(301, $callback['headers']['status-code']); - $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); - - $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $this->getHeaders())); - - $this->assertEquals(200, $installations['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $installations['body']['total']); - - foreach ($installations['body']['installations'] as $installation) { - if ($installation['provider'] === 'gitea') { - return $installation; - } - } - - $this->fail('Gitea installation not found in listInstallations.'); - } - - // Client does not persist cookies between calls, so carry them manually and never follow redirects - private function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array - { - $headers = []; - - if (!empty($this->giteaCookies)) { - $headers['cookie'] = \implode('; ', \array_map( - fn (string $name) => $name . '=' . $this->giteaCookies[$name], - \array_keys($this->giteaCookies) - )); - } - - if ($method !== Client::METHOD_GET) { - $headers['content-type'] = 'application/x-www-form-urlencoded'; - } - - $response = $gitea->call($method, $path, $headers, $params, false, false); - - $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); - - return $response; - } - - private function giteaApiHelper(string $method, string $path, array $params = []): array - { - $gitea = new Client(); - $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); - - return $gitea->call($method, $path, [ - 'content-type' => 'application/json', - 'authorization' => 'Basic ' . \base64_encode(self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD), - ], $params); - } - private function gitHelper(string $command, string $directory): void { $identity = '-c user.email=gitea@appwrite.io -c user.name=' . self::GITEA_USERNAME; diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php new file mode 100644 index 0000000000..663eac41da --- /dev/null +++ b/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php @@ -0,0 +1,174 @@ + + */ + private function getTenantHeaders(array $tenant, ?string $projectId = null): array + { + return [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $tenant['session'], + 'x-appwrite-mode' => 'admin', + 'x-appwrite-project' => $projectId ?? $tenant['projectId'], + ]; + } + + public function testListRepositoriesRejectsForeignInstallation(): void + { + $this->createGiteaUserHelper(self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); + + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $installationB = $this->createInstallationHelper($b['projectId'], $b['headers'], self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); + + $this->assertNotEquals($installationA['$id'], $installationB['$id']); + // Different Gitea accounts, so a cross-tenant leak would be visible as + // the other owner's repositories, not a second identical listing. + $this->assertNotEquals($installationA['organization'], $installationB['organization']); + + // Foreign installation id on the caller's own project never reaches the + // provider: the ownership guard answers before any Gitea call. + $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $foreign['headers']['status-code']); + $this->assertEquals('installation_not_found', $foreign['body']['type']); + + $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $foreign['headers']['status-code']); + $this->assertEquals('installation_not_found', $foreign['body']['type']); + + // Addressing the other tenant's project directly dies earlier, on team + // membership, before any VCS code runs. + $foreignProject = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(401, $foreignProject['headers']['status-code']); + + // Each tenant still reaches their own installation. + $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $own['headers']['status-code']); + + $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $own['headers']['status-code']); + } + + public function testRepositoryEndpointsRejectForeignInstallation(): void + { + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $base = '/vcs/github/installations/' . $installationA['$id']; + + // Dummy repository ids are safe: the ownership guard answers before any + // provider call. The POSTs would write to tenant A's Gitea account if + // the guard regressed, which is exactly the point. + $probes = [ + [Client::METHOD_GET, $base . '/providerRepositories/1', []], + [Client::METHOD_GET, $base . '/providerRepositories/1/branches', []], + [Client::METHOD_GET, $base . '/providerRepositories/1/contents', []], + [Client::METHOD_POST, $base . '/providerRepositories', ['name' => 'cross-' . \uniqid(), 'private' => true]], + [Client::METHOD_POST, $base . '/detections', ['providerRepositoryId' => '1', 'type' => 'runtime']], + ]; + + foreach ($probes as [$method, $path, $params]) { + $response = $this->client->call($method, $path, $this->getTenantHeaders($b), $params); + + $this->assertEquals(404, $response['headers']['status-code'], $method . ' ' . $path); + $this->assertEquals('installation_not_found', $response['body']['type'], $method . ' ' . $path); + } + } + + public function testInvitedMemberCanUseInstallation(): void + { + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $path = '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories'; + + $before = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(401, $before['headers']['status-code']); + + $membership = $this->client->call(Client::METHOD_POST, '/teams/' . $a['teamId'] . '/memberships', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $a['session'], + ], [ + 'userId' => $b['userId'], + 'roles' => ['developer'], + 'url' => 'http://localhost:5000/join-us#title', + ]); + $this->assertEquals(201, $membership['headers']['status-code']); + $this->assertFalse($membership['body']['confirm']); + + $email = $this->getLastEmailByAddress($b['email'], fn ($email) => $this->assertStringContainsString('/join-us', (string) ($email['html'] ?? ''))); + $params = $this->extractQueryParamsFromEmailLink($email['html']); + $this->assertNotEmpty($params['secret'] ?? ''); + + $accept = $this->client->call(Client::METHOD_PATCH, '/teams/' . $a['teamId'] . '/memberships/' . $membership['body']['$id'] . '/status', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'userId' => $b['userId'], + 'secret' => $params['secret'], + ]); + $this->assertEquals(200, $accept['headers']['status-code']); + $this->assertTrue($accept['body']['confirm']); + + // Same request as before: membership in the team now authorizes B on + // project A, and the installation belongs to project A, so it works. + $after = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $after['headers']['status-code']); + + // Membership does not launder the installation into B's own project. + $stillForeign = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $stillForeign['headers']['status-code']); + $this->assertEquals('installation_not_found', $stillForeign['body']['type']); + } +} From fb4636596cf743a00d349a850645a2b8bbbfa77b Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 11:16:52 +0530 Subject: [PATCH 19/46] fix(vcs): make the callback error copy actionable and future-proof the suite The missing-state message now says what happened (the installation finished on GitHub, so it could not be linked to a project) and where to go, instead of a bare parameter complaint. The request message warns that approving on GitHub is not enough, since that route drops state and dead-ends the same way. The suite signs its state the way authorize does: harmless while the callback does not verify signatures, and keeps this file green once signing lands. The state validator gets headroom (4096) because the redirect URLs it carries are not length-limited, so a server-built state can outgrow 2048 on its own. --- .../Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 6 +++--- .../VCSGitHub/VCSGitHubCallbackConsoleClientTest.php | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 958f9ba53a..f2c67dee74 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -38,7 +38,7 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'GitHub installation ID', true) ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) - ->param('state', '', new Text(2048, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(4096, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) ->inject('vcsFactory') ->inject('project') @@ -60,7 +60,7 @@ class Get extends Action array $platform ) { if (empty($state)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.'); + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'This installation was completed on GitHub, so it could not be connected to a project. Open your project\'s settings in the Appwrite Console and connect GitHub from there.'); } $state = \json_decode($state, true) ?? []; @@ -163,7 +163,7 @@ class Get extends Action } } else { $error = $setupAction === 'request' - ? 'Your installation request was sent to the organization owners for approval. Installing the Appwrite GitHub App on an organization requires an owner, so ask one of them to create the installation from the Appwrite Console.' + ? 'Your request was sent to the organization owners. An owner must complete the installation from the Appwrite Console; approving the request on GitHub is not enough.' : 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.'; if (empty($redirectFailure)) { diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index be7e3a4293..11c9b63a11 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -8,6 +8,7 @@ use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; +use Utopia\System\System; /** * Failure paths of the GitHub App installation callback. Deliberately does not @@ -26,12 +27,17 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope private function getState(): string { + $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); + // Signed the way the authorize endpoint signs it. Harmless while the + // callback does not verify signatures, and keeps this suite green once + // it does. return (string) json_encode([ - 'projectId' => $this->getProject()['$id'], + 'projectId' => $projectId, 'success' => $redirect, 'failure' => $redirect, + 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); } @@ -71,6 +77,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Missing state parameter', (string) $response['body']); + $this->assertStringContainsString('completed on GitHub', (string) $response['body']); } } From 0140965f22f90d704b0fcffde78aed102d6ccd55 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 12:22:40 +0530 Subject: [PATCH 20/46] Trim test comments to the non-obvious --- .../VCSGitHub/VCSGitHubCallbackConsoleClientTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 11c9b63a11..2efe0f6f62 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -30,9 +30,8 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); - // Signed the way the authorize endpoint signs it. Harmless while the - // callback does not verify signatures, and keeps this suite green once - // it does. + // Signed like the authorize endpoint does: harmless while the callback + // does not verify signatures, green once it does. return (string) json_encode([ 'projectId' => $projectId, 'success' => $redirect, @@ -65,10 +64,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); } - /** - * Without state there is no project to redirect to, so the error has to at - * least say what went wrong. - */ public function testGetCallbackMissingState(): void { $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ From c81461e11b9ad90b4428e239f7a377d09ddecd8d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 12:23:02 +0530 Subject: [PATCH 21/46] Trim test comments to the non-obvious --- tests/e2e/Services/VCSGitea/VCSGiteaBase.php | 5 ++--- ...VCSGiteaInstallationsConsoleClientTest.php | 21 ++++++------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php index 859ae7a280..d1534670ee 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php @@ -58,7 +58,7 @@ trait VCSGiteaBase $this->assertNotEmpty($session['cookies']['a_session_console'] ?? ''); $sessionCookie = $session['cookies']['a_session_console']; - // Sessions propagate slowly under parallel load, so retry 401s like ProjectCustom does + // Sessions propagate slowly under parallel load, so retry 401s $team = null; for ($i = 0; $i < 5; $i++) { $team = $this->client->call(Client::METHOD_POST, '/teams', [ @@ -122,8 +122,7 @@ trait VCSGiteaBase 'username' => $username, 'email' => $username . '@localhost.test', 'password' => $password, - // Defaults to true, which turns the OAuth2 authorize into a - // redirect to the change-password page instead of the callback. + // Defaults to true, which redirects the OAuth2 authorize to the change-password page 'must_change_password' => false, ]); diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php index 663eac41da..587c92e86e 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php @@ -10,13 +10,10 @@ use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; /** - * Cross-tenant isolation of VCS installations. Two unrelated console users with - * their own teams, projects and Gitea accounts must not be able to use each - * other's installations, even knowing the installation id. - * - * ProjectCustom and SideConsole only satisfy the Scope contract; every call in - * this class authenticates as one of the two tenants built per test, never as - * the shared root user. + * Cross-tenant isolation of VCS installations: two unrelated console users must + * not be able to use each other's installations, even knowing the id. + * ProjectCustom and SideConsole only satisfy the Scope contract; every call + * authenticates as one of the per-test tenants, never as the shared root user. */ final class VCSGiteaInstallationsConsoleClientTest extends Scope { @@ -52,8 +49,7 @@ final class VCSGiteaInstallationsConsoleClientTest extends Scope $installationB = $this->createInstallationHelper($b['projectId'], $b['headers'], self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); $this->assertNotEquals($installationA['$id'], $installationB['$id']); - // Different Gitea accounts, so a cross-tenant leak would be visible as - // the other owner's repositories, not a second identical listing. + // Different Gitea accounts make a leak visible as the other owner's repositories $this->assertNotEquals($installationA['organization'], $installationB['organization']); // Foreign installation id on the caller's own project never reaches the @@ -77,7 +73,6 @@ final class VCSGiteaInstallationsConsoleClientTest extends Scope ]); $this->assertEquals(401, $foreignProject['headers']['status-code']); - // Each tenant still reaches their own installation. $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ 'type' => 'runtime', ]); @@ -97,9 +92,7 @@ final class VCSGiteaInstallationsConsoleClientTest extends Scope $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); $base = '/vcs/github/installations/' . $installationA['$id']; - // Dummy repository ids are safe: the ownership guard answers before any - // provider call. The POSTs would write to tenant A's Gitea account if - // the guard regressed, which is exactly the point. + // Dummy repository ids are safe: the ownership guard answers before any provider call $probes = [ [Client::METHOD_GET, $base . '/providerRepositories/1', []], [Client::METHOD_GET, $base . '/providerRepositories/1/branches', []], @@ -157,8 +150,6 @@ final class VCSGiteaInstallationsConsoleClientTest extends Scope $this->assertEquals(200, $accept['headers']['status-code']); $this->assertTrue($accept['body']['confirm']); - // Same request as before: membership in the team now authorizes B on - // project A, and the installation belongs to project A, so it works. $after = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ 'type' => 'runtime', ]); From 95e0dc1dfdf498188fdeb6d0db7b32e80ef21411 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 13:15:28 +0530 Subject: [PATCH 22/46] Keep the original control flow around the failure redirects The fix in these branches is the separator character and the request-aware message; inverting the guards on top of that buried a one-character change in a rewritten block. --- .../Modules/VCS/Http/GitHub/Callback/Get.php | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index f2c67dee74..1421333253 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -72,16 +72,16 @@ class Get extends Action if ($project->isEmpty()) { $error = 'Project with the ID from state could not be found.'; - if (empty($redirectFailure)) { - throw new Exception(Exception::PROJECT_NOT_FOUND, $error); + if (!empty($redirectFailure)) { + $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); + return; } - $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; - $response - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Pragma', 'no-cache') - ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); - return; + throw new Exception(Exception::PROJECT_NOT_FOUND, $error); } $region = $project->getAttribute('region', 'default'); @@ -166,16 +166,16 @@ class Get extends Action ? 'Your request was sent to the organization owners. An owner must complete the installation from the Appwrite Console; approving the request on GitHub is not enough.' : 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.'; - if (empty($redirectFailure)) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); + if (!empty($redirectFailure)) { + $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); + return; } - $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; - $response - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Pragma', 'no-cache') - ->redirect($redirectFailure . $separator . \http_build_query(['error' => $error])); - return; + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, $error); } $response From f201ee9d4a6b5795f3b552caf56e9e0d24b38fd3 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:04:18 +0530 Subject: [PATCH 23/46] Use match for setup_action and document its values GitHub sends install, update or request; install and update always carry an installation_id, so this branch treats them and anything unrecognized as the permissions case. --- .../Modules/VCS/Http/GitHub/Callback/Get.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 1421333253..7c2b1bb7a3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -162,9 +162,15 @@ class Get extends Action ])); } } else { - $error = $setupAction === 'request' - ? 'Your request was sent to the organization owners. An owner must complete the installation from the Appwrite Console; approving the request on GitHub is not enough.' - : 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.'; + // GitHub sends setup_action=install on a completed installation, + // update from the app's Configure page, and request when a member + // asked the owners for approval. install and update should always + // carry an installation_id, so reaching this branch with them (or + // any unrecognized value) is treated as the permissions case. + $error = match ($setupAction) { + 'request' => 'Your request was sent to the organization owners. An owner must complete the installation from the Appwrite Console; approving the request on GitHub is not enough.', + default => 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.', + }; if (!empty($redirectFailure)) { $separator = \str_contains($redirectFailure, '?') ? '&' : '?'; From c6e860096fe98da3e64dfca41415f932cfdeaf85 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:06:16 +0530 Subject: [PATCH 24/46] Cover every changed callback branch Present-but-empty state, the request message from the match, the project-not-found redirect, and the computed default redirect when state carries empty URLs. States are built through one signer so redirects for missing projects and empty URLs stay verifiable once signing lands. --- .../VCSGitHubCallbackConsoleClientTest.php | 65 ++++++++++++++++--- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 2efe0f6f62..fa48ad2506 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -25,21 +25,25 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope return 'http://localhost/console/project-default-' . $this->getProject()['$id'] . '/settings/git-installations'; } - private function getState(): string + // Signed like the authorize endpoint does: harmless while the callback + // does not verify signatures, green once it does. + private function buildState(string $projectId, string $success, string $failure): string { - $projectId = $this->getProject()['$id']; - $redirect = $this->getRedirect(); - - // Signed like the authorize endpoint does: harmless while the callback - // does not verify signatures, green once it does. return (string) json_encode([ 'projectId' => $projectId, - 'success' => $redirect, - 'failure' => $redirect, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + 'success' => $success, + 'failure' => $failure, + 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); } + private function getState(): string + { + $redirect = $this->getRedirect(); + + return $this->buildState($this->getProject()['$id'], $redirect, $redirect); + } + /** * @return array */ @@ -62,6 +66,9 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); + + parse_str((string) parse_url((string) $response['headers']['location'], PHP_URL_QUERY), $query); + $this->assertStringContainsString('sent to the organization owners', $query['error'] ?? ''); } public function testGetCallbackMissingState(): void @@ -74,4 +81,44 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); $this->assertStringContainsString('completed on GitHub', (string) $response['body']); } + + public function testGetCallbackEmptyState(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => '', + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('completed on GitHub', (string) $response['body']); + } + + public function testGetCallbackProjectNotFound(): void + { + $redirect = $this->getRedirect(); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => $this->buildState('missing-project', $redirect, $redirect), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($redirect . '?error=', (string) $response['headers']['location']); + + parse_str((string) parse_url((string) $response['headers']['location'], PHP_URL_QUERY), $query); + $this->assertStringContainsString('could not be found', $query['error'] ?? ''); + } + + public function testGetCallbackDefaultRedirect(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'request', + 'state' => $this->buildState($this->getProject()['$id'], '', ''), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); + } } From 637e97e518b470f28588981d3acdd22f80045553 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:08:49 +0530 Subject: [PATCH 25/46] Keep the isolation tests in the existing Gitea suite The multi-tenant tests move into VCSGiteaConsoleClientTest and the OAuth2 dance takes its project, headers and Gitea account as parameters in place, so the suite gains the coverage without relocating helpers or adding a class. --- tests/e2e/Services/VCSGitea/VCSGiteaBase.php | 273 ------------ .../VCSGitea/VCSGiteaConsoleClientTest.php | 387 ++++++++++++++++++ ...VCSGiteaInstallationsConsoleClientTest.php | 165 -------- 3 files changed, 387 insertions(+), 438 deletions(-) delete mode 100644 tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php index d1534670ee..34a63e1010 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaBase.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaBase.php @@ -2,18 +2,10 @@ namespace Tests\E2E\Services\VCSGitea; -use Tests\E2E\Client; -use Utopia\Database\Helpers\ID; use Utopia\System\System; trait VCSGiteaBase { - // Admin user created by dev/gitea/setup.sh (run by the CI Gitea job) - protected const GITEA_USERNAME = 'appwrite'; - protected const GITEA_PASSWORD = 'password'; - - protected array $giteaCookies = []; - protected function setUp(): void { parent::setUp(); @@ -22,269 +14,4 @@ trait VCSGiteaBase $this->markTestSkipped('Gitea VCS is not configured.'); } } - - /** - * Create a console user with their own team and project, so tests can act - * as two unrelated tenants side by side. - * - * @return array{userId: string, email: string, session: string, teamId: string, projectId: string, headers: array} - */ - protected function createTenantHelper(): array - { - $email = \uniqid('tenant-', true) . \getmypid() . \bin2hex(\random_bytes(4)) . '@localhost.test'; - $password = 'password'; - - $user = $this->client->call(Client::METHOD_POST, '/account', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - ], [ - 'userId' => ID::unique(), - 'email' => $email, - 'password' => $password, - 'name' => 'VCS Tenant', - ]); - $this->assertEquals(201, $user['headers']['status-code']); - - $session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - ], [ - 'email' => $email, - 'password' => $password, - ]); - $this->assertEquals(201, $session['headers']['status-code']); - $this->assertNotEmpty($session['cookies']['a_session_console'] ?? ''); - $sessionCookie = $session['cookies']['a_session_console']; - - // Sessions propagate slowly under parallel load, so retry 401s - $team = null; - for ($i = 0; $i < 5; $i++) { - $team = $this->client->call(Client::METHOD_POST, '/teams', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $sessionCookie, - 'x-appwrite-project' => 'console', - ], [ - 'teamId' => ID::unique(), - 'name' => 'VCS Tenant Team', - ]); - - if ($team['headers']['status-code'] !== 401) { - break; - } - - \usleep(500000); - } - $this->assertEquals(201, $team['headers']['status-code']); - - $project = null; - for ($i = 0; $i < 5; $i++) { - $project = $this->client->call(Client::METHOD_POST, '/projects', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $sessionCookie, - 'x-appwrite-project' => 'console', - ], [ - 'projectId' => ID::unique(), - 'region' => System::getEnv('_APP_REGION', 'default'), - 'name' => 'VCS Tenant Project', - 'teamId' => $team['body']['$id'], - ]); - - if ($project['headers']['status-code'] !== 401) { - break; - } - - \usleep(500000); - } - $this->assertEquals(201, $project['headers']['status-code']); - - return [ - 'userId' => $user['body']['$id'], - 'email' => $email, - 'session' => $sessionCookie, - 'teamId' => $team['body']['$id'], - 'projectId' => $project['body']['$id'], - 'headers' => [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $sessionCookie, - 'x-appwrite-mode' => 'admin', - ], - ]; - } - - protected function createGiteaUserHelper(string $username, string $password): void - { - $response = $this->giteaApiHelper(Client::METHOD_POST, '/api/v1/admin/users', [ - 'username' => $username, - 'email' => $username . '@localhost.test', - 'password' => $password, - // Defaults to true, which redirects the OAuth2 authorize to the change-password page - 'must_change_password' => false, - ]); - - // 422 means the user survived a previous run; the Gitea volume persists. - $this->assertContains($response['headers']['status-code'], [201, 422], \json_encode($response['body'])); - } - - /** - * Walk the OAuth2 dance against the local Gitea up to the grant and return - * what the callback expects, asserting every hop on the way. - * - * @return array{code: string, state: string, consoleUrl: string} - */ - protected function authorizeGiteaHelper(?string $projectId = null, ?array $headers = null, string $username = self::GITEA_USERNAME, string $password = self::GITEA_PASSWORD): array - { - $projectId ??= $this->getProject()['$id']; - $headers ??= $this->getHeaders(); - - // Start each dance from a fresh jar; Gitea reuses session cookies, so a - // stale jar would silently keep the previous user logged in. - $this->giteaCookies = []; - - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; - - $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $headers), [ - 'success' => $consoleUrl, - 'failure' => $consoleUrl, - ], true, false); - - $this->assertEquals(301, $authorize['headers']['status-code']); - - $loginUrl = $authorize['headers']['location'] ?? ''; - $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); - - $query = []; - \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); - $this->assertNotEmpty($query['client_id'] ?? ''); - $this->assertNotEmpty($query['redirect_uri'] ?? ''); - $this->assertNotEmpty($query['state'] ?? ''); - $this->assertSame('code', $query['response_type'] ?? ''); - - // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query - $gitea = new Client(); - $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); - - $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); - $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); - - $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ - '_csrf' => $this->giteaCookies['_csrf'], - 'user_name' => $username, - 'password' => $password, - ]); - $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); - $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); - - // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them - $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); - $consent = $this->giteaCallHelper($gitea, Client::METHOD_GET, $authorizePath); - - if (\in_array($consent['headers']['status-code'], [302, 303])) { - // Already granted: Gitea redirects straight back with a code - $redirect = $consent['headers']['location'] ?? ''; - } else { - $this->assertEquals(200, $consent['headers']['status-code']); - - $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ - '_csrf' => $this->giteaCookies['_csrf'], - 'client_id' => $query['client_id'], - 'state' => $query['state'], - 'scope' => $query['scope'] ?? '', - 'nonce' => '', - 'redirect_uri' => $query['redirect_uri'], - ]); - $this->assertEquals(303, $grant['headers']['status-code']); - - $redirect = $grant['headers']['location'] ?? ''; - } - - $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); - - $callbackQuery = []; - \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); - $this->assertNotEmpty($callbackQuery['code'] ?? ''); - $this->assertNotEmpty($callbackQuery['state'] ?? ''); - - return [ - 'code' => $callbackQuery['code'], - 'state' => $callbackQuery['state'], - 'consoleUrl' => $consoleUrl, - ]; - } - - /** - * Complete the OAuth2 dance and return the resulting installation. - */ - protected function createInstallationHelper(?string $projectId = null, ?array $headers = null, string $username = self::GITEA_USERNAME, string $password = self::GITEA_PASSWORD): array - { - $projectId ??= $this->getProject()['$id']; - $headers ??= $this->getHeaders(); - - ['code' => $code, 'state' => $state, 'consoleUrl' => $consoleUrl] = $this->authorizeGiteaHelper($projectId, $headers, $username, $password); - - $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $headers), [ - 'code' => $code, - 'state' => $state, - ], true, false); - - $this->assertEquals(301, $callback['headers']['status-code']); - $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); - - $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', \array_merge([ - 'x-appwrite-project' => $projectId, - ], $headers)); - - $this->assertEquals(200, $installations['headers']['status-code']); - $this->assertGreaterThanOrEqual(1, $installations['body']['total']); - - foreach ($installations['body']['installations'] as $installation) { - if ($installation['provider'] === 'gitea') { - return $installation; - } - } - - $this->fail('Gitea installation not found in listInstallations.'); - } - - // Client does not persist cookies between calls, so carry them manually and never follow redirects - protected function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array - { - $headers = []; - - if (!empty($this->giteaCookies)) { - $headers['cookie'] = \implode('; ', \array_map( - fn (string $name) => $name . '=' . $this->giteaCookies[$name], - \array_keys($this->giteaCookies) - )); - } - - if ($method !== Client::METHOD_GET) { - $headers['content-type'] = 'application/x-www-form-urlencoded'; - } - - $response = $gitea->call($method, $path, $headers, $params, false, false); - - $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); - - return $response; - } - - protected function giteaApiHelper(string $method, string $path, array $params = []): array - { - $gitea = new Client(); - $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); - - return $gitea->call($method, $path, [ - 'content-type' => 'application/json', - 'authorization' => 'Basic ' . \base64_encode(self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD), - ], $params); - } } diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index b9113161d0..6cead4c23d 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -19,6 +19,13 @@ final class VCSGiteaConsoleClientTest extends Scope use ProjectCustom; use SideConsole; + // Admin user created by gitea-bootstrap (docker-compose.override.yml, `gitea` profile) + private const GITEA_USERNAME = 'appwrite'; + private const GITEA_PASSWORD = 'password'; + private const GITEA_USERNAME_SECOND = 'appwrite2'; + + private array $giteaCookies = []; + public function testCreateInstallation(): void { $installation = $this->createInstallationHelper(); @@ -103,6 +110,144 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEventually(fn () => $this->assertExecutionOutputHelper($functionId, 'gitea-v2'), 30000, 1000); } + /** + * Walk the full OAuth2 dance against the local Gitea and return the + * resulting installation, asserting every hop on the way. + */ + private function createInstallationHelper(?string $projectId = null, ?array $headers = null, string $username = self::GITEA_USERNAME, string $password = self::GITEA_PASSWORD): array + { + $projectId ??= $this->getProject()['$id']; + $headers ??= $this->getHeaders(); + + // Each dance starts from a fresh jar; Gitea reuses session cookies, so + // a stale jar would silently keep the previous user logged in. + $this->giteaCookies = []; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers), [ + 'success' => $consoleUrl, + 'failure' => $consoleUrl, + ], true, false); + + $this->assertEquals(301, $authorize['headers']['status-code']); + + $loginUrl = $authorize['headers']['location'] ?? ''; + $this->assertStringContainsString('/login/oauth/authorize', (string) $loginUrl); + + $query = []; + \parse_str(\parse_url($loginUrl, PHP_URL_QUERY) ?: '', $query); + $this->assertNotEmpty($query['client_id'] ?? ''); + $this->assertNotEmpty($query['redirect_uri'] ?? ''); + $this->assertNotEmpty($query['state'] ?? ''); + $this->assertSame('code', $query['response_type'] ?? ''); + + // Location targets the browser-facing endpoint, unreachable in-container; reuse only its path and query + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + $this->giteaCallHelper($gitea, Client::METHOD_GET, '/user/login'); + $this->assertNotEmpty($this->giteaCookies['_csrf'] ?? '', 'Gitea did not issue a CSRF cookie.'); + + $login = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/user/login', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'user_name' => $username, + 'password' => $password, + ]); + $this->assertContains($login['headers']['status-code'], [302, 303], 'Gitea login failed.'); + $this->assertNotEmpty($this->giteaCookies['i_like_gitea'] ?? '', 'Gitea did not issue a session cookie.'); + + // Gitea stores client_id, state and redirect_uri in the session here; the grant must match them + $authorizePath = \parse_url($loginUrl, PHP_URL_PATH) . '?' . \parse_url($loginUrl, PHP_URL_QUERY); + $consent = $this->giteaCallHelper($gitea, Client::METHOD_GET, $authorizePath); + + if (\in_array($consent['headers']['status-code'], [302, 303])) { + // Already granted: Gitea redirects straight back with a code + $redirect = $consent['headers']['location'] ?? ''; + } else { + $this->assertEquals(200, $consent['headers']['status-code']); + + $grant = $this->giteaCallHelper($gitea, Client::METHOD_POST, '/login/oauth/grant', [ + '_csrf' => $this->giteaCookies['_csrf'], + 'client_id' => $query['client_id'], + 'state' => $query['state'], + 'scope' => $query['scope'] ?? '', + 'nonce' => '', + 'redirect_uri' => $query['redirect_uri'], + ]); + $this->assertEquals(303, $grant['headers']['status-code']); + + $redirect = $grant['headers']['location'] ?? ''; + } + + $this->assertStringContainsString('/v1/vcs/gitea/callback', (string) $redirect); + + $callbackQuery = []; + \parse_str(\parse_url($redirect, PHP_URL_QUERY) ?: '', $callbackQuery); + $this->assertNotEmpty($callbackQuery['code'] ?? ''); + $this->assertNotEmpty($callbackQuery['state'] ?? ''); + + $callback = $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers), [ + 'code' => $callbackQuery['code'], + 'state' => $callbackQuery['state'], + ], true, false); + + $this->assertEquals(301, $callback['headers']['status-code']); + $this->assertEquals($consoleUrl, $callback['headers']['location'] ?? ''); + + $installations = $this->client->call(Client::METHOD_GET, '/vcs/installations', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $headers)); + + $this->assertEquals(200, $installations['headers']['status-code']); + $this->assertGreaterThanOrEqual(1, $installations['body']['total']); + + foreach ($installations['body']['installations'] as $installation) { + if ($installation['provider'] === 'gitea') { + return $installation; + } + } + + $this->fail('Gitea installation not found in listInstallations.'); + } + + // Client does not persist cookies between calls, so carry them manually and never follow redirects + private function giteaCallHelper(Client $gitea, string $method, string $path, array $params = []): array + { + $headers = []; + + if (!empty($this->giteaCookies)) { + $headers['cookie'] = \implode('; ', \array_map( + fn (string $name) => $name . '=' . $this->giteaCookies[$name], + \array_keys($this->giteaCookies) + )); + } + + if ($method !== Client::METHOD_GET) { + $headers['content-type'] = 'application/x-www-form-urlencoded'; + } + + $response = $gitea->call($method, $path, $headers, $params, false, false); + + $this->giteaCookies = \array_merge($this->giteaCookies, $response['cookies']); + + return $response; + } + + private function giteaApiHelper(string $method, string $path, array $params = []): array + { + $gitea = new Client(); + $gitea->setEndpoint(System::getEnv('_APP_VCS_GITEA_ENDPOINT', 'http://gitea:3000')); + + return $gitea->call($method, $path, [ + 'content-type' => 'application/json', + 'authorization' => 'Basic ' . \base64_encode(self::GITEA_USERNAME . ':' . self::GITEA_PASSWORD), + ], $params); + } + private function gitHelper(string $command, string $directory): void { $identity = '-c user.email=gitea@appwrite.io -c user.name=' . self::GITEA_USERNAME; @@ -190,4 +335,246 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals('completed', $execution['body']['status'] ?? '', \json_encode($execution['body'])); $this->assertEquals($output, $execution['body']['responseBody'] ?? ''); } + + /** + * @return array{userId: string, email: string, session: string, teamId: string, projectId: string, headers: array} + */ + private function createTenantHelper(): array + { + $email = \uniqid('tenant-', true) . \getmypid() . \bin2hex(\random_bytes(4)) . '@localhost.test'; + $password = 'password'; + + $user = $this->client->call(Client::METHOD_POST, '/account', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'userId' => ID::unique(), + 'email' => $email, + 'password' => $password, + 'name' => 'VCS Tenant', + ]); + $this->assertEquals(201, $user['headers']['status-code']); + + $session = $this->client->call(Client::METHOD_POST, '/account/sessions/email', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'email' => $email, + 'password' => $password, + ]); + $this->assertEquals(201, $session['headers']['status-code']); + $sessionCookie = $session['cookies']['a_session_console']; + + // Sessions propagate slowly under parallel load, so retry 401s + $team = null; + for ($i = 0; $i < 5; $i++) { + $team = $this->client->call(Client::METHOD_POST, '/teams', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-project' => 'console', + ], [ + 'teamId' => ID::unique(), + 'name' => 'VCS Tenant Team', + ]); + + if ($team['headers']['status-code'] !== 401) { + break; + } + + \usleep(500000); + } + $this->assertEquals(201, $team['headers']['status-code']); + + $project = null; + for ($i = 0; $i < 5; $i++) { + $project = $this->client->call(Client::METHOD_POST, '/projects', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-project' => 'console', + ], [ + 'projectId' => ID::unique(), + 'region' => System::getEnv('_APP_REGION', 'default'), + 'name' => 'VCS Tenant Project', + 'teamId' => $team['body']['$id'], + ]); + + if ($project['headers']['status-code'] !== 401) { + break; + } + + \usleep(500000); + } + $this->assertEquals(201, $project['headers']['status-code']); + + return [ + 'userId' => $user['body']['$id'], + 'email' => $email, + 'session' => $sessionCookie, + 'teamId' => $team['body']['$id'], + 'projectId' => $project['body']['$id'], + 'headers' => [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $sessionCookie, + 'x-appwrite-mode' => 'admin', + ], + ]; + } + + private function createGiteaUserHelper(string $username, string $password): void + { + $response = $this->giteaApiHelper(Client::METHOD_POST, '/api/v1/admin/users', [ + 'username' => $username, + 'email' => $username . '@localhost.test', + 'password' => $password, + // Defaults to true, which redirects the OAuth2 authorize to the change-password page + 'must_change_password' => false, + ]); + + // 422 means the user survived a previous run; the Gitea volume persists. + $this->assertContains($response['headers']['status-code'], [201, 422], \json_encode($response['body'])); + } + + /** + * @param array{projectId: string, session: string} $tenant + * @return array + */ + private function getTenantHeaders(array $tenant, ?string $projectId = null): array + { + return [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'cookie' => 'a_session_console=' . $tenant['session'], + 'x-appwrite-mode' => 'admin', + 'x-appwrite-project' => $projectId ?? $tenant['projectId'], + ]; + } + + public function testListRepositoriesRejectsForeignInstallation(): void + { + $this->createGiteaUserHelper(self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); + + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $installationB = $this->createInstallationHelper($b['projectId'], $b['headers'], self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); + + $this->assertNotEquals($installationA['$id'], $installationB['$id']); + // Different Gitea accounts make a leak visible as the other owner's repositories + $this->assertNotEquals($installationA['organization'], $installationB['organization']); + + // Foreign installation id on the caller's own project never reaches the + // provider: the ownership guard answers before any Gitea call. + $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $foreign['headers']['status-code']); + $this->assertEquals('installation_not_found', $foreign['body']['type']); + + $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $foreign['headers']['status-code']); + $this->assertEquals('installation_not_found', $foreign['body']['type']); + + // Addressing the other tenant's project directly dies earlier, on team + // membership, before any VCS code runs. + $foreignProject = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(401, $foreignProject['headers']['status-code']); + + $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $own['headers']['status-code']); + + $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $own['headers']['status-code']); + } + + public function testRepositoryEndpointsRejectForeignInstallation(): void + { + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $base = '/vcs/github/installations/' . $installationA['$id']; + + // Dummy repository ids are safe: the ownership guard answers before any provider call + $probes = [ + [Client::METHOD_GET, $base . '/providerRepositories/1', []], + [Client::METHOD_GET, $base . '/providerRepositories/1/branches', []], + [Client::METHOD_GET, $base . '/providerRepositories/1/contents', []], + [Client::METHOD_POST, $base . '/providerRepositories', ['name' => 'cross-' . \uniqid(), 'private' => true]], + [Client::METHOD_POST, $base . '/detections', ['providerRepositoryId' => '1', 'type' => 'runtime']], + ]; + + foreach ($probes as [$method, $path, $params]) { + $response = $this->client->call($method, $path, $this->getTenantHeaders($b), $params); + + $this->assertEquals(404, $response['headers']['status-code'], $method . ' ' . $path); + $this->assertEquals('installation_not_found', $response['body']['type'], $method . ' ' . $path); + } + } + + public function testInvitedMemberCanUseInstallation(): void + { + $a = $this->createTenantHelper(); + $b = $this->createTenantHelper(); + + $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); + $path = '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories'; + + $before = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(401, $before['headers']['status-code']); + + $membership = $this->client->call(Client::METHOD_POST, '/teams/' . $a['teamId'] . '/memberships', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $a['session'], + ], [ + 'userId' => $b['userId'], + 'roles' => ['developer'], + 'url' => 'http://localhost:5000/join-us#title', + ]); + $this->assertEquals(201, $membership['headers']['status-code']); + + $email = $this->getLastEmailByAddress($b['email'], fn ($email) => $this->assertStringContainsString('/join-us', $email['html'] ?? '')); + $params = $this->extractQueryParamsFromEmailLink($email['html']); + $this->assertNotEmpty($params['secret'] ?? ''); + + $accept = $this->client->call(Client::METHOD_PATCH, '/teams/' . $a['teamId'] . '/memberships/' . $membership['body']['$id'] . '/status', [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + ], [ + 'userId' => $b['userId'], + 'secret' => $params['secret'], + ]); + $this->assertEquals(200, $accept['headers']['status-code']); + + // Membership in the team now authorizes B on project A, where the installation lives + $after = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ + 'type' => 'runtime', + ]); + $this->assertEquals(200, $after['headers']['status-code']); + + // Membership does not launder the installation into B's own project. + $stillForeign = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b), [ + 'type' => 'runtime', + ]); + $this->assertEquals(404, $stillForeign['headers']['status-code']); + $this->assertEquals('installation_not_found', $stillForeign['body']['type']); + } } diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php deleted file mode 100644 index 587c92e86e..0000000000 --- a/tests/e2e/Services/VCSGitea/VCSGiteaInstallationsConsoleClientTest.php +++ /dev/null @@ -1,165 +0,0 @@ - - */ - private function getTenantHeaders(array $tenant, ?string $projectId = null): array - { - return [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'cookie' => 'a_session_console=' . $tenant['session'], - 'x-appwrite-mode' => 'admin', - 'x-appwrite-project' => $projectId ?? $tenant['projectId'], - ]; - } - - public function testListRepositoriesRejectsForeignInstallation(): void - { - $this->createGiteaUserHelper(self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); - - $a = $this->createTenantHelper(); - $b = $this->createTenantHelper(); - - $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); - $installationB = $this->createInstallationHelper($b['projectId'], $b['headers'], self::GITEA_USERNAME_SECOND, self::GITEA_PASSWORD); - - $this->assertNotEquals($installationA['$id'], $installationB['$id']); - // Different Gitea accounts make a leak visible as the other owner's repositories - $this->assertNotEquals($installationA['organization'], $installationB['organization']); - - // Foreign installation id on the caller's own project never reaches the - // provider: the ownership guard answers before any Gitea call. - $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ - 'type' => 'runtime', - ]); - $this->assertEquals(404, $foreign['headers']['status-code']); - $this->assertEquals('installation_not_found', $foreign['body']['type']); - - $foreign = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ - 'type' => 'runtime', - ]); - $this->assertEquals(404, $foreign['headers']['status-code']); - $this->assertEquals('installation_not_found', $foreign['body']['type']); - - // Addressing the other tenant's project directly dies earlier, on team - // membership, before any VCS code runs. - $foreignProject = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($b, $a['projectId']), [ - 'type' => 'runtime', - ]); - $this->assertEquals(401, $foreignProject['headers']['status-code']); - - $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories', $this->getTenantHeaders($a), [ - 'type' => 'runtime', - ]); - $this->assertEquals(200, $own['headers']['status-code']); - - $own = $this->client->call(Client::METHOD_GET, '/vcs/github/installations/' . $installationB['$id'] . '/providerRepositories', $this->getTenantHeaders($b), [ - 'type' => 'runtime', - ]); - $this->assertEquals(200, $own['headers']['status-code']); - } - - public function testRepositoryEndpointsRejectForeignInstallation(): void - { - $a = $this->createTenantHelper(); - $b = $this->createTenantHelper(); - - $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); - $base = '/vcs/github/installations/' . $installationA['$id']; - - // Dummy repository ids are safe: the ownership guard answers before any provider call - $probes = [ - [Client::METHOD_GET, $base . '/providerRepositories/1', []], - [Client::METHOD_GET, $base . '/providerRepositories/1/branches', []], - [Client::METHOD_GET, $base . '/providerRepositories/1/contents', []], - [Client::METHOD_POST, $base . '/providerRepositories', ['name' => 'cross-' . \uniqid(), 'private' => true]], - [Client::METHOD_POST, $base . '/detections', ['providerRepositoryId' => '1', 'type' => 'runtime']], - ]; - - foreach ($probes as [$method, $path, $params]) { - $response = $this->client->call($method, $path, $this->getTenantHeaders($b), $params); - - $this->assertEquals(404, $response['headers']['status-code'], $method . ' ' . $path); - $this->assertEquals('installation_not_found', $response['body']['type'], $method . ' ' . $path); - } - } - - public function testInvitedMemberCanUseInstallation(): void - { - $a = $this->createTenantHelper(); - $b = $this->createTenantHelper(); - - $installationA = $this->createInstallationHelper($a['projectId'], $a['headers']); - $path = '/vcs/github/installations/' . $installationA['$id'] . '/providerRepositories'; - - $before = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ - 'type' => 'runtime', - ]); - $this->assertEquals(401, $before['headers']['status-code']); - - $membership = $this->client->call(Client::METHOD_POST, '/teams/' . $a['teamId'] . '/memberships', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - 'cookie' => 'a_session_console=' . $a['session'], - ], [ - 'userId' => $b['userId'], - 'roles' => ['developer'], - 'url' => 'http://localhost:5000/join-us#title', - ]); - $this->assertEquals(201, $membership['headers']['status-code']); - $this->assertFalse($membership['body']['confirm']); - - $email = $this->getLastEmailByAddress($b['email'], fn ($email) => $this->assertStringContainsString('/join-us', (string) ($email['html'] ?? ''))); - $params = $this->extractQueryParamsFromEmailLink($email['html']); - $this->assertNotEmpty($params['secret'] ?? ''); - - $accept = $this->client->call(Client::METHOD_PATCH, '/teams/' . $a['teamId'] . '/memberships/' . $membership['body']['$id'] . '/status', [ - 'origin' => 'http://localhost', - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - ], [ - 'userId' => $b['userId'], - 'secret' => $params['secret'], - ]); - $this->assertEquals(200, $accept['headers']['status-code']); - $this->assertTrue($accept['body']['confirm']); - - $after = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b, $a['projectId']), [ - 'type' => 'runtime', - ]); - $this->assertEquals(200, $after['headers']['status-code']); - - // Membership does not launder the installation into B's own project. - $stillForeign = $this->client->call(Client::METHOD_GET, $path, $this->getTenantHeaders($b), [ - 'type' => 'runtime', - ]); - $this->assertEquals(404, $stillForeign['headers']['status-code']); - $this->assertEquals('installation_not_found', $stillForeign['body']['type']); - } -} From 772e1243dcf378f33c5df426bc808f0e1d2a0241 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:09:03 +0530 Subject: [PATCH 26/46] Handle unexpected setup_action values explicitly and assert status codes only The match now names GitHub's three documented values and gives anything else its own message instead of folding it into the permissions case. Tests assert status codes and redirect targets; error prose is not a contract. --- .../Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 7 ++++--- .../VCSGitHub/VCSGitHubCallbackConsoleClientTest.php | 8 -------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 7c2b1bb7a3..ce92a37f48 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -165,11 +165,12 @@ class Get extends Action // GitHub sends setup_action=install on a completed installation, // update from the app's Configure page, and request when a member // asked the owners for approval. install and update should always - // carry an installation_id, so reaching this branch with them (or - // any unrecognized value) is treated as the permissions case. + // carry an installation_id, so without one they mean the caller + // lacked permission to install. $error = match ($setupAction) { 'request' => 'Your request was sent to the organization owners. An owner must complete the installation from the Appwrite Console; approving the request on GitHub is not enough.', - default => 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.', + 'install', 'update', '' => 'Installation of the Appwrite GitHub App on organization accounts is restricted to organization owners. As a member of the organization, you do not have the necessary permissions to install this GitHub App. Please contact the organization owner to create the installation from the Appwrite Console.', + default => 'Unexpected setup action "' . $setupAction . '" received from GitHub. Please restart the installation from the Appwrite Console.', }; if (!empty($redirectFailure)) { diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index fa48ad2506..4eeff226ca 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -66,9 +66,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); - - parse_str((string) parse_url((string) $response['headers']['location'], PHP_URL_QUERY), $query); - $this->assertStringContainsString('sent to the organization owners', $query['error'] ?? ''); } public function testGetCallbackMissingState(): void @@ -79,7 +76,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('completed on GitHub', (string) $response['body']); } public function testGetCallbackEmptyState(): void @@ -91,7 +87,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('completed on GitHub', (string) $response['body']); } public function testGetCallbackProjectNotFound(): void @@ -106,9 +101,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith($redirect . '?error=', (string) $response['headers']['location']); - - parse_str((string) parse_url((string) $response['headers']['location'], PHP_URL_QUERY), $query); - $this->assertStringContainsString('could not be found', $query['error'] ?? ''); } public function testGetCallbackDefaultRedirect(): void From 1e6c9ef7727aef824c08321f6dde17657efacf62 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:16:50 +0530 Subject: [PATCH 27/46] Cover invalid-JSON state, over-2048 state, and unexpected setup_action The long-state case is the one that distinguishes the widened validator from the old one; the invalid-JSON case covers the null-guarded decode through to the project lookup; the unexpected setup_action selects the match's default arm. --- .../VCSGitHubCallbackConsoleClientTest.php | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 4eeff226ca..84a0a6715f 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -113,4 +113,42 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); } + + public function testGetCallbackInvalidJsonState(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => 'not-json', + ], followRedirects: false); + + $this->assertEquals(404, $response['headers']['status-code']); + $this->assertEquals('project_not_found', $response['body']['type']); + } + + public function testGetCallbackLongState(): void + { + // Above the old 2048 cap: redirect URLs are not length-limited, so a + // state this size is one the server itself can produce. + $failure = $this->getRedirect() . '?pad=' . str_repeat('a', 2400); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'request', + 'state' => $this->buildState($this->getProject()['$id'], $this->getRedirect(), $failure), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($failure, (string) $response['headers']['location']); + } + + public function testGetCallbackUnexpectedSetupAction(): void + { + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'foo', + 'state' => $this->getState(), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); + } } From 168cc05084c58ef50106ebbf73bb22f50a861202 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 14:17:08 +0530 Subject: [PATCH 28/46] Assert the guard passes on the owning project for every guarded endpoint --- tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 6cead4c23d..f05372a8a3 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -523,6 +523,14 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(404, $response['headers']['status-code'], $method . ' ' . $path); $this->assertEquals('installation_not_found', $response['body']['type'], $method . ' ' . $path); } + + // On the owning project the guard must pass; whatever the provider + // answers about the dummy repository, it is not installation_not_found. + foreach ($probes as [$method, $path, $params]) { + $response = $this->client->call($method, $path, $this->getTenantHeaders($a), $params); + + $this->assertNotEquals('installation_not_found', $response['body']['type'] ?? '', $method . ' ' . $path); + } } public function testInvitedMemberCanUseInstallation(): void From 777f95c53cfbcfd4fdc1c507b605662a3ba97e51 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:26:17 +0530 Subject: [PATCH 29/46] Assert only the status on the invalid-JSON state case The callback renders the HTML error view, so the body is a string and has no type field to read. --- .../Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 84a0a6715f..b4535f019c 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -123,7 +123,6 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(404, $response['headers']['status-code']); - $this->assertEquals('project_not_found', $response['body']['type']); } public function testGetCallbackLongState(): void From 40ed5c7b48e7a9d119ef8ee88b2d65ded0d76f2d Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:28:58 +0530 Subject: [PATCH 30/46] Fix static analysis on the Gitea cookie reset and email probe Assigning an empty literal narrowed the cookie jar to array{}, so the reads after each Gitea call were flagged; the Rector fix casts the email body before the string assertion. --- tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index f05372a8a3..edce81d0e1 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -121,7 +121,9 @@ final class VCSGiteaConsoleClientTest extends Scope // Each dance starts from a fresh jar; Gitea reuses session cookies, so // a stale jar would silently keep the previous user logged in. - $this->giteaCookies = []; + /** @var array $cookies */ + $cookies = []; + $this->giteaCookies = $cookies; $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ @@ -558,7 +560,7 @@ final class VCSGiteaConsoleClientTest extends Scope ]); $this->assertEquals(201, $membership['headers']['status-code']); - $email = $this->getLastEmailByAddress($b['email'], fn ($email) => $this->assertStringContainsString('/join-us', $email['html'] ?? '')); + $email = $this->getLastEmailByAddress($b['email'], fn ($email) => $this->assertStringContainsString('/join-us', (string) ($email['html'] ?? ''))); $params = $this->extractQueryParamsFromEmailLink($email['html']); $this->assertNotEmpty($params['secret'] ?? ''); From b03dbcdcdd20f249a9bef87a7f8ec3f06bcf6eaa Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:29:51 +0530 Subject: [PATCH 31/46] Accept either rejection code for invalid JSON state Without signing the decode leaves no project and the callback answers 404; with signing it fails the signature first and answers 400. The point of the case is that neither is a fatal error. --- .../Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index b4535f019c..204d11d543 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -122,7 +122,9 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope 'state' => 'not-json', ], followRedirects: false); - $this->assertEquals(404, $response['headers']['status-code']); + // Rejected rather than fatal: the decode yields no project, and once + // state is signed it fails the signature check before that. + $this->assertContains($response['headers']['status-code'], [400, 404]); } public function testGetCallbackLongState(): void From 11e0566ae85c2861621f3bea5adecc6ef6453cd6 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:38:22 +0530 Subject: [PATCH 32/46] fix(vcs): apply the callback redirect fix to every provider Authorize defaults success and failure to empty strings and signs them, so the shared OAuth2 callback's array_merge let those empty values win over the computed console URLs: Gitea, GitLab and Bitbucket redirect to '' on success and throw instead of redirecting on failure. Same defect the GitHub callback had, on three more providers. The state validators also rejected an empty value before the friendly message and capped at 2048, which the signature can push a server-built state past. Tests move onto Gitea, where the callback can be driven end to end without provider credentials: missing state, empty state, a state whose project was changed after signing, the failure redirect carrying its error as a query string, and empty redirect URLs falling back to the console default. --- .../Modules/VCS/Http/Callback/Base.php | 7 +- .../Modules/VCS/Http/Origin/Callback/Get.php | 2 +- .../VCSGitea/VCSGiteaConsoleClientTest.php | 81 +++++++++++++++++++ 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index 9e76682b13..81161ecf0f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -55,7 +55,7 @@ abstract class Base extends Action ->label('scope', 'public') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->param('state', '', new Text(2048), 'OAuth2 state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(4096, 0), 'OAuth2 state. Contains info sent when starting authorization flow.', true) ->inject('response') ->inject('dbForPlatform') ->inject('platform') @@ -104,9 +104,8 @@ abstract class Base extends Action 'failure' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations", ]; - $state = \array_merge($defaultState, $state); - $redirectSuccess = $state['success'] ?? ''; - $redirectFailure = $state['failure'] ?? ''; + $redirectSuccess = empty($state['success']) ? $defaultState['success'] : $state['success']; + $redirectFailure = empty($state['failure']) ? $defaultState['failure'] : $state['failure']; if (empty($code)) { $this->failure($response, $redirectFailure, 'OAuth2 authorization code is missing.'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php index 6c51fa124e..9488a34343 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php @@ -37,7 +37,7 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'Origin installation ID', true) ->param('installation_receipt', '', new Text(8192, 0), 'Origin installation receipt JWT, signed by Cursor.', true) - ->param('state', '', new Text(2048, 0), 'Origin state. Contains info sent when starting the installation flow.', true) + ->param('state', '', new Text(4096, 0), 'Origin state. Contains info sent when starting the installation flow.', true) ->inject('response') ->inject('dbForPlatform') ->inject('platform') diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 3b65774a78..af54f68046 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -329,4 +329,85 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals('completed', $execution['body']['status'] ?? '', \json_encode($execution['body'])); $this->assertEquals($output, $execution['body']['responseBody'] ?? ''); } + + private function buildGiteaState(string $projectId, string $success, string $failure): string + { + return (string) \json_encode([ + 'projectId' => $projectId, + 'success' => $success, + 'failure' => $failure, + 'signature' => \hash_hmac('sha256', \json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + ]); + } + + /** + * @param array $params + * @return array + */ + private function callGiteaCallbackHelper(array $params): array + { + return $this->client->call(Client::METHOD_GET, '/vcs/gitea/callback', \array_merge([ + 'x-appwrite-project' => $this->getProject()['$id'], + ], $this->getHeaders()), $params, true, false); + } + + public function testCreateInstallationWithoutState(): void + { + $response = $this->callGiteaCallbackHelper(['code' => 'unused']); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithEmptyState(): void + { + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => '']); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithTamperedState(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); + $state['projectId'] = 'victim-project'; + + $response = $this->callGiteaCallbackHelper([ + 'code' => 'unused', + 'state' => (string) \json_encode($state), + ]); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithoutCode(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + // Signed state, no code: the failure redirect must carry the error as a + // query string rather than glue it onto the path. + $response = $this->callGiteaCallbackHelper([ + 'state' => $this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), + ]); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($consoleUrl . '?error=', (string) $response['headers']['location']); + } + + public function testCreateInstallationWithEmptyRedirects(): void + { + $projectId = $this->getProject()['$id']; + + // Authorize signs empty redirect URLs when none were given, so the + // callback has to fall back to the computed console URL instead of + // redirecting nowhere. + $response = $this->callGiteaCallbackHelper([ + 'state' => $this->buildGiteaState($projectId, '', ''), + ]); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith('http://localhost/console/project-default-' . $projectId . '/settings/git-installations?error=', (string) $response['headers']['location']); + } } From b1fc741eb6d65c53686a569e85564765869bcf85 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:42:43 +0530 Subject: [PATCH 33/46] Drop the GitHub cases Gitea now covers Empty state, project not found, the default redirect, invalid JSON and the widened state length all run against the shared callback on Gitea. What stays is GitHub's own file: setup_action, installation_id, and the missing-state 400 users actually hit. --- .../VCSGitHubCallbackConsoleClientTest.php | 80 +++---------------- 1 file changed, 9 insertions(+), 71 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php index 204d11d543..c5cb05a8e8 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php @@ -25,23 +25,20 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope return 'http://localhost/console/project-default-' . $this->getProject()['$id'] . '/settings/git-installations'; } - // Signed like the authorize endpoint does: harmless while the callback - // does not verify signatures, green once it does. - private function buildState(string $projectId, string $success, string $failure): string - { - return (string) json_encode([ - 'projectId' => $projectId, - 'success' => $success, - 'failure' => $failure, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), - ]); - } private function getState(): string { + $projectId = $this->getProject()['$id']; $redirect = $this->getRedirect(); - return $this->buildState($this->getProject()['$id'], $redirect, $redirect); + // Signed like the authorize endpoint does: harmless while the callback + // does not verify signatures, green once it does. + return (string) json_encode([ + 'projectId' => $projectId, + 'success' => $redirect, + 'failure' => $redirect, + 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + ]); } /** @@ -78,69 +75,10 @@ final class VCSGitHubCallbackConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); } - public function testGetCallbackEmptyState(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => '', - ], followRedirects: false); - $this->assertEquals(400, $response['headers']['status-code']); - } - public function testGetCallbackProjectNotFound(): void - { - $redirect = $this->getRedirect(); - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => $this->buildState('missing-project', $redirect, $redirect), - ], followRedirects: false); - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($redirect . '?error=', (string) $response['headers']['location']); - } - - public function testGetCallbackDefaultRedirect(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'request', - 'state' => $this->buildState($this->getProject()['$id'], '', ''), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); - } - - public function testGetCallbackInvalidJsonState(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => 'not-json', - ], followRedirects: false); - - // Rejected rather than fatal: the decode yields no project, and once - // state is signed it fails the signature check before that. - $this->assertContains($response['headers']['status-code'], [400, 404]); - } - - public function testGetCallbackLongState(): void - { - // Above the old 2048 cap: redirect URLs are not length-limited, so a - // state this size is one the server itself can produce. - $failure = $this->getRedirect() . '?pad=' . str_repeat('a', 2400); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'request', - 'state' => $this->buildState($this->getProject()['$id'], $this->getRedirect(), $failure), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($failure, (string) $response['headers']['location']); - } public function testGetCallbackUnexpectedSetupAction(): void { From df93274a352e734077ec3b4f9d3bef7f7d968ede Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:46:19 +0530 Subject: [PATCH 34/46] Test the callback on Gitea only The Gitea suite drives the shared callback end to end without provider credentials, so the GitHub copies of those cases are redundant. --- .../VCSGitHubCallbackConsoleClientTest.php | 93 ------------------- 1 file changed, 93 deletions(-) delete mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php deleted file mode 100644 index c5cb05a8e8..0000000000 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubCallbackConsoleClientTest.php +++ /dev/null @@ -1,93 +0,0 @@ -getProject()['$id'] . '/settings/git-installations'; - } - - - private function getState(): string - { - $projectId = $this->getProject()['$id']; - $redirect = $this->getRedirect(); - - // Signed like the authorize endpoint does: harmless while the callback - // does not verify signatures, green once it does. - return (string) json_encode([ - 'projectId' => $projectId, - 'success' => $redirect, - 'failure' => $redirect, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $redirect, $redirect]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), - ]); - } - - /** - * @return array - */ - private function getCallbackHeaders(): array - { - return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); - } - - /** - * GitHub sends the user back without an installation id when an organisation - * member can only request the installation. The console has to receive that - * as a query string, not glued onto the path. - */ - public function testGetCallbackInstallationRequest(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'request', - 'state' => $this->getState(), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); - } - - public function testGetCallbackMissingState(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } - - - - - - - public function testGetCallbackUnexpectedSetupAction(): void - { - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'foo', - 'state' => $this->getState(), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($this->getRedirect() . '?error=', (string) $response['headers']['location']); - } -} From b2c502d393cd621e14a1c7f12e8d1756acd78af4 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:50:26 +0530 Subject: [PATCH 35/46] Cover invalid, unknown-project and over-2048 state on Gitea The long-state case is the one that tells the widened validator apart from the old one; unknown project exercises the not-found redirect; invalid JSON lands on the signature check. --- .../VCSGitea/VCSGiteaConsoleClientTest.php | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index af54f68046..3dd09c90dd 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -410,4 +410,41 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith('http://localhost/console/project-default-' . $projectId . '/settings/git-installations?error=', (string) $response['headers']['location']); } + + public function testCreateInstallationWithInvalidState(): void + { + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => 'not-json']); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithUnknownProject(): void + { + $consoleUrl = 'http://localhost/console/project-default-missing/settings/git-installations'; + + $response = $this->callGiteaCallbackHelper([ + 'code' => 'unused', + 'state' => $this->buildGiteaState('missing-project', $consoleUrl, $consoleUrl), + ]); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($consoleUrl . '?error=', (string) $response['headers']['location']); + } + + public function testCreateInstallationWithLongState(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + // Past the old 2048 cap: redirect URLs are not length-limited, so the + // authorize endpoint can produce a state this size itself. + $failure = $consoleUrl . '?pad=' . \str_repeat('a', 2400); + + $response = $this->callGiteaCallbackHelper([ + 'state' => $this->buildGiteaState($projectId, $consoleUrl, $failure), + ]); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($failure, (string) $response['headers']['location']); + } } From b669b8d8220962aa593dbf590404a0abe7888b8b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 13:21:54 +0100 Subject: [PATCH 36/46] chore: run the mails worker with 6 coroutines One process with six coroutines instead of one process with one, matching the other workers. SMTP is stream-based and coroutine-hooked, so sends overlap on one event loop. Co-Authored-By: Claude Fable 5.1 --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 1aa6df871c..90c3538ea5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1145,7 +1145,7 @@ services: environment: - _APP_ENV - _APP_WORKERS_NUM=1 - - _APP_WORKER_MAX_COROUTINES=1 + - _APP_WORKER_MAX_COROUTINES=6 - _APP_WORKER_PER_CORE - _APP_POOL_ADAPTER - _APP_OPENSSL_KEY_V1 From 97da563b0913232ff6040f081331df21d845c2ce Mon Sep 17 00:00:00 2001 From: Atharva Deosthale Date: Wed, 2 Sep 2026 17:56:09 +0530 Subject: [PATCH 37/46] fix(databases): expose transactionId on DocumentsDB and VectorsDB create SDK methods The createDocument and createDocuments routes for DocumentsDB and VectorsDB already accept a transactionId body parameter, but their SDK Method definitions omit it from the parameter allow-list, so the generated SDKs cannot stage document creation inside a transaction. TablesDB's createRow and createRows already list it. Mirror that here. --- .../Databases/Http/DocumentsDB/Collections/Documents/Create.php | 2 ++ .../Databases/Http/VectorsDB/Collections/Documents/Create.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php index 90f65fd516..ea7c96c1b4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/DocumentsDB/Collections/Documents/Create.php @@ -76,6 +76,7 @@ class Create extends DocumentCreate new Parameter('documentId', optional: false), new Parameter('data', optional: false), new Parameter('permissions', optional: true), + new Parameter('transactionId', optional: true), ] ), new Method( @@ -96,6 +97,7 @@ class Create extends DocumentCreate new Parameter('databaseId', optional: false), new Parameter('collectionId', optional: false), new Parameter('documents', optional: false), + new Parameter('transactionId', optional: true), ] ) ]) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Create.php index 86488634c3..680308544f 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/VectorsDB/Collections/Documents/Create.php @@ -70,6 +70,7 @@ class Create extends DocumentCreate new Parameter('documentId', optional: false), new Parameter('data', optional: false), new Parameter('permissions', optional: true), + new Parameter('transactionId', optional: true), ] ), new Method( @@ -90,6 +91,7 @@ class Create extends DocumentCreate new Parameter('databaseId', optional: false), new Parameter('collectionId', optional: false), new Parameter('documents', optional: false), + new Parameter('transactionId', optional: true), ] ) ]) From bdd63a8bd6ed513c960a2e53c0cf8c24763e2b63 Mon Sep 17 00:00:00 2001 From: "Luke B. Silver" <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 13:26:09 +0100 Subject: [PATCH 38/46] refactor(queues): remove intermediate publisher resources (#13449) --- app/init/resources.php | 20 +++----------------- app/init/resources/request.php | 8 ++++---- 2 files changed, 7 insertions(+), 21 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 757dc404c2..35252bec17 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -109,20 +109,6 @@ $container->set('authorization', fn () => new Authorization(), []); $container->set('publisher', fn (Group $pools) => new BrokerPool(publisher: $pools->get('publisher')), ['pools']); -$container->set('publisherDatabases', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherFunctions', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherMigrations', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherMails', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherDeletes', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherMessaging', fn (Publisher $publisher) => $publisher, ['publisher']); - -$container->set('publisherWebhooks', fn (Publisher $publisher) => $publisher, ['publisher']); - $container->set('publisherForAudits', fn (Publisher $publisher) => new AuditPublisher( $publisher, new Queue(System::getEnv('_APP_AUDITS_QUEUE_NAME', Event::AUDITS_QUEUE_NAME)) @@ -204,10 +190,10 @@ $container->set('publisherForJobs', fn (Publisher $publisher) => new JobsPublish new Queue(System::getEnv('_APP_JOBS_QUEUE_NAME', Event::JOBS_QUEUE_NAME)) ), ['publisher']); -$container->set('publisherForDatabase', fn (Publisher $publisherDatabases) => new DatabasePublisher( - $publisherDatabases, +$container->set('publisherForDatabase', fn (Publisher $publisher) => new DatabasePublisher( + $publisher, new Queue(System::getEnv('_APP_DATABASE_QUEUE_NAME', Event::DATABASE_QUEUE_NAME)) -), ['publisherDatabases']); +), ['publisher']); $container->set('publisherForDeletes', fn (Publisher $publisher) => new DeletePublisher( $publisher, diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 8039571243..8aa7f7cf70 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -672,7 +672,7 @@ return function (Container $context): void { return; }, ['user', 'store', 'proofForToken']); - $context->set('dbForProject', function (DatabaseFactory $databaseFactory, Database $dbForPlatform, Document $project, Response $response, Publisher $publisher, Publisher $publisherFunctions, Publisher $publisherWebhooks, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Webhook $queueForWebhooks, Realtime $queueForRealtime, UsageContext $usage, Request $request) { + $context->set('dbForProject', function (DatabaseFactory $databaseFactory, Database $dbForPlatform, Document $project, Response $response, Publisher $publisher, Event $queueForEvents, FunctionPublisher $publisherForFunctions, Webhook $queueForWebhooks, Realtime $queueForRealtime, UsageContext $usage, Request $request) { if ($project->isEmpty() || $project->getId() === 'console') { return $dbForPlatform; } @@ -796,7 +796,7 @@ return function (Container $context): void { // Clone the queues, to prevent events triggered by the database listener // from overwriting the events that are supposed to be triggered in the shutdown hook. $queueForEventsClone = new Event($publisher); - $queueForWebhooks = new Webhook($publisherWebhooks); + $queueForWebhooksClone = clone $queueForWebhooks; $queueForRealtime = new Realtime(); $database @@ -811,7 +811,7 @@ return function (Container $context): void { $response, $queueForEventsClone->from($queueForEvents), $publisherForFunctions, - $queueForWebhooks->from($queueForEvents), + $queueForWebhooksClone->from($queueForEvents), $queueForRealtime->from($queueForEvents) )) ->on(Database::EVENT_DOCUMENT_CREATE, 'purge-function-events-cache', fn ($event, $document) => $functionsEventsCacheListener($event, $document, $project, $database)) @@ -819,7 +819,7 @@ return function (Container $context): void { ->on(Database::EVENT_DOCUMENT_DELETE, 'purge-function-events-cache', fn ($event, $document) => $functionsEventsCacheListener($event, $document, $project, $database)); return $database; - }, ['databaseFactory', 'dbForPlatform', 'project', 'response', 'publisher', 'publisherFunctions', 'publisherWebhooks', 'queueForEvents', 'publisherForFunctions', 'queueForWebhooks', 'queueForRealtime', 'usage', 'request']); + }, ['databaseFactory', 'dbForPlatform', 'project', 'response', 'publisher', 'queueForEvents', 'publisherForFunctions', 'queueForWebhooks', 'queueForRealtime', 'usage', 'request']); $context->set('schema', function ($utopia, $dbForProject, $authorization) { From 0533b540a2643993d18474648993841b4bc2b1e6 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 17:59:15 +0530 Subject: [PATCH 39/46] Drive a full Gitea install without redirect URLs Authorize signs empty success and failure into state when none are given, and before the fallback fix the callback then redirected to an empty location. The helper's existing assertion on the callback's Location now proves the console default is used instead. Also corrects a test comment that described GitHub's separator bug, which the shared callback never had. --- .../VCSGitea/VCSGiteaConsoleClientTest.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 3dd09c90dd..afd57fa44f 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -113,17 +113,17 @@ final class VCSGiteaConsoleClientTest extends Scope * Walk the full OAuth2 dance against the local Gitea and return the * resulting installation, asserting every hop on the way. */ - private function createInstallationHelper(): array + private function createInstallationHelper(bool $redirects = true): array { $projectId = $this->getProject()['$id']; $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ 'x-appwrite-project' => $projectId, - ], $this->getHeaders()), [ + ], $this->getHeaders()), $redirects ? [ 'success' => $consoleUrl, 'failure' => $consoleUrl, - ], true, false); + ] : [], true, false); $this->assertEquals(301, $authorize['headers']['status-code']); @@ -386,8 +386,7 @@ final class VCSGiteaConsoleClientTest extends Scope $projectId = $this->getProject()['$id']; $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; - // Signed state, no code: the failure redirect must carry the error as a - // query string rather than glue it onto the path. + // Signed state, no code: the failure redirect carries the error as a query string $response = $this->callGiteaCallbackHelper([ 'state' => $this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), ]); @@ -447,4 +446,14 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(301, $response['headers']['status-code']); $this->assertStringStartsWith($failure, (string) $response['headers']['location']); } + + public function testCreateInstallationWithoutRedirects(): void + { + // Authorize signs empty success and failure URLs when none are given; + // the helper asserts the callback still lands on the console default + // rather than redirecting to an empty location. + $installation = $this->createInstallationHelper(redirects: false); + + $this->assertNotEmpty($installation['$id']); + } } From ab4ce263dc2c8074309567adc9d3431e6d69e3b4 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 2 Sep 2026 12:35:41 +0000 Subject: [PATCH 40/46] test(databases): cover transactionId on DocumentsDB and VectorsDB create Lock createDocument/createDocuments SDK method metadata so generated clients keep transactionId, and reject unknown transaction IDs on the create and bulk-create routes. Co-authored-by: Jake Barnby --- .../Transactions/TransactionsBase.php | 41 +++++++ .../Transactions/TransactionsBase.php | 45 +++++++ .../Http/CreateTransactionIdTest.php | 70 +++++++++++ tests/unit/SDK/Specification/FormatTest.php | 111 ++++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100644 tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php diff --git a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php index a49b9cf25b..16f235a473 100644 --- a/tests/e2e/Services/Databases/Transactions/TransactionsBase.php +++ b/tests/e2e/Services/Databases/Transactions/TransactionsBase.php @@ -1785,6 +1785,26 @@ trait TransactionsBase $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('Created via normal route', $response['body']['name']); + + /** + * Test for FAILURE + */ + $unknown = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRecordIdParam() => 'doc_unknown_txn', + 'data' => [ + 'name' => 'Unknown transaction', + 'counter' => 1, + 'category' => 'test' + ], + 'transactionId' => ID::unique() + ]); + + $this->assertEquals(404, $unknown['headers']['status-code']); + $this->assertEquals(Exception::TRANSACTION_NOT_FOUND, $unknown['body']['type']); } /** @@ -2267,6 +2287,27 @@ trait TransactionsBase $this->assertEquals("Bulk created {$i}", $response['body']['name']); $this->assertEquals('bulk_created', $response['body']['category']); } + + /** + * Test for FAILURE + */ + $unknown = $this->client->call(Client::METHOD_POST, $this->getRecordUrl($databaseId, $collectionId, null), array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + $this->getRecordResource() => [ + [ + '$id' => 'bulk_unknown_txn', + 'name' => 'Unknown transaction', + 'category' => 'bulk_unknown' + ] + ], + 'transactionId' => ID::unique() + ]); + + $this->assertEquals(404, $unknown['headers']['status-code']); + $this->assertEquals(Exception::TRANSACTION_NOT_FOUND, $unknown['body']['type']); } /** diff --git a/tests/e2e/Services/Databases/VectorsDB/Transactions/TransactionsBase.php b/tests/e2e/Services/Databases/VectorsDB/Transactions/TransactionsBase.php index b8cc9f6f0d..9821685ac2 100644 --- a/tests/e2e/Services/Databases/VectorsDB/Transactions/TransactionsBase.php +++ b/tests/e2e/Services/Databases/VectorsDB/Transactions/TransactionsBase.php @@ -2,6 +2,7 @@ namespace Tests\E2E\Services\Databases\VectorsDB\Transactions; +use Appwrite\Extend\Exception; use Tests\E2E\Client; use Utopia\Database\Database; use Utopia\Database\Helpers\ID; @@ -1489,6 +1490,27 @@ trait TransactionsBase $this->assertEquals(200, $response['headers']['status-code']); $this->assertEquals('Created via normal route', $response['body']['metadata']['name']); + + /** + * Test for FAILURE + */ + $unknown = $this->client->call(Client::METHOD_POST, "/vectorsdb/{$databaseId}/collections/{$collectionId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'documentId' => 'doc_unknown_txn', + 'data' => [ + 'embeddings' => $this->generateEmbeddings(3), + 'metadata' => [ + 'name' => 'Unknown transaction', + ] + ], + 'transactionId' => ID::unique() + ]); + + $this->assertEquals(404, $unknown['headers']['status-code']); + $this->assertEquals(Exception::TRANSACTION_NOT_FOUND, $unknown['body']['type']); } /** @@ -1933,6 +1955,29 @@ trait TransactionsBase $this->assertEquals("Bulk created {$i}", $response['body']['metadata']['name']); $this->assertEquals('bulk_created', $response['body']['metadata']['category']); } + + /** + * Test for FAILURE + */ + $unknown = $this->client->call(Client::METHOD_POST, "/vectorsdb/{$databaseId}/collections/{$collectionId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'documents' => [ + [ + '$id' => 'bulk_unknown_txn', + 'embeddings' => $this->generateEmbeddings(3), + 'metadata' => [ + 'name' => 'Unknown transaction', + ] + ] + ], + 'transactionId' => ID::unique() + ]); + + $this->assertEquals(404, $unknown['headers']['status-code']); + $this->assertEquals(Exception::TRANSACTION_NOT_FOUND, $unknown['body']['type']); } /** diff --git a/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php b/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php new file mode 100644 index 0000000000..c461e43f15 --- /dev/null +++ b/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php @@ -0,0 +1,70 @@ + $expected + */ + #[DataProvider('createRoutes')] + public function testCreateSdkMethodsExposeTransactionId(Route $route, array $expected): void + { + $methods = $route->getLabel('sdk'); + $this->assertIsArray($methods); + + $names = []; + foreach ($methods as $method) { + $this->assertInstanceOf(Method::class, $method); + $names[$method->getMethodName()] = \array_map( + static fn (Parameter $parameter): string => $parameter->getName(), + $method->getParameters(), + ); + } + + foreach ($expected as $methodName) { + $this->assertArrayHasKey($methodName, $names); + $this->assertContains( + 'transactionId', + $names[$methodName], + $route->getPath() . ' ' . $methodName . ' must expose transactionId', + ); + } + } + + /** + * @return array}> + */ + public static function createRoutes(): array + { + Method::$processed = []; + Method::$errors = []; + + return [ + 'documentsdb' => [ + new DocumentsDBCreate(), + ['createDocument', 'createDocuments'], + ], + 'vectorsdb' => [ + new VectorsDBCreate(), + ['createDocument', 'createDocuments'], + ], + ]; + } +} diff --git a/tests/unit/SDK/Specification/FormatTest.php b/tests/unit/SDK/Specification/FormatTest.php index 96d05875cf..61754fbf57 100644 --- a/tests/unit/SDK/Specification/FormatTest.php +++ b/tests/unit/SDK/Specification/FormatTest.php @@ -2,6 +2,7 @@ namespace Tests\Unit\SDK\Specification; +use Appwrite\SDK\AuthType; use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Parameter; @@ -1210,4 +1211,114 @@ final class FormatTest extends TestCase $this->assertSame('example.com', $properties['domain']['example']); $this->assertSame('FFFFFF', $properties['background']['example']); } + + public function testExplicitMethodParametersAreTheSdkMethodList(): void + { + Method::$processed = []; + Method::$errors = []; + + $route = $this->createDocumentsRoute( + createDocument: [ + new Parameter('databaseId', optional: false), + new Parameter('collectionId', optional: false), + new Parameter('documentId', optional: false), + new Parameter('data', optional: false), + new Parameter('permissions', optional: true), + new Parameter('transactionId', optional: true), + ], + createDocuments: [ + new Parameter('databaseId', optional: false), + new Parameter('collectionId', optional: false), + new Parameter('documents', optional: false), + new Parameter('transactionId', optional: true), + ], + ); + + $methods = $this->sdkMethods((new OpenAPI3(new Container(), [], [$route], [], [], 0, 'console'))->parse()); + + $this->assertContains('transactionId', $methods['createDocument']); + $this->assertContains('transactionId', $methods['createDocuments']); + } + + public function testOmittedMethodParametersAreDroppedFromTheSdkMethodList(): void + { + Method::$processed = []; + Method::$errors = []; + + $route = $this->createDocumentsRoute( + createDocument: [ + new Parameter('databaseId', optional: false), + new Parameter('collectionId', optional: false), + new Parameter('documentId', optional: false), + new Parameter('data', optional: false), + new Parameter('permissions', optional: true), + ], + createDocuments: [ + new Parameter('databaseId', optional: false), + new Parameter('collectionId', optional: false), + new Parameter('documents', optional: false), + ], + ); + + $methods = $this->sdkMethods((new OpenAPI3(new Container(), [], [$route], [], [], 0, 'console'))->parse()); + + $this->assertNotContains('transactionId', $methods['createDocument']); + $this->assertNotContains('transactionId', $methods['createDocuments']); + } + + /** + * @param list $createDocument + * @param list $createDocuments + */ + private function createDocumentsRoute(array $createDocument, array $createDocuments): Route + { + return (new Route('POST', '/v1/documentsdb/:databaseId/collections/:collectionId/documents')) + ->desc('Create document') + ->label('sdk', [ + new Method( + namespace: 'documentsDB', + group: 'documents', + name: 'createDocument', + description: 'Create document.', + auth: [AuthType::ADMIN], + responses: [], + parameters: $createDocument, + ), + new Method( + namespace: 'documentsDB', + group: 'documents', + name: 'createDocuments', + description: 'Create documents.', + auth: [AuthType::ADMIN], + responses: [], + parameters: $createDocuments, + ), + ]) + ->param('databaseId', '', new Text(256), 'Database ID.') + ->param('collectionId', '', new Text(256), 'Collection ID.') + ->param('documentId', '', new Text(256), 'Document ID.', true) + ->param('data', [], new JSON(), 'Document data.', true) + ->param('permissions', null, new Nullable(new Text(256)), 'Permissions.', true) + ->param('documents', [], new JSON(), 'Documents.', true) + ->param('transactionId', null, new Nullable(new Text(256)), 'Transaction ID.', true); + } + + /** + * @return array> + */ + private function sdkMethods(array $openApi): array + { + $path = '/documentsdb/{databaseId}/collections/{collectionId}/documents'; + $this->assertArrayHasKey($path, $openApi['paths']); + + $methods = []; + foreach ($openApi['paths'][$path]['post']['x-appwrite']['methods'] as $method) { + $methods[$method['name']] = $method['parameters']; + } + + $this->assertArrayHasKey('createDocument', $methods); + $this->assertArrayHasKey('createDocuments', $methods); + + return $methods; + } } From 442390482454a084b37f7bc9fe389bf11e696d7b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 2 Sep 2026 12:36:04 +0000 Subject: [PATCH 41/46] test(databases): bootstrap init.php in create transactionId unit test Co-authored-by: Jake Barnby --- .../Platform/Modules/Databases/Http/CreateTransactionIdTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php b/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php index c461e43f15..8d9890e959 100644 --- a/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php +++ b/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Utopia\Http\Route; +require_once __DIR__ . '/../../../../../../app/init.php'; require_once __DIR__ . '/../../../../../../src/Appwrite/Platform/Modules/Databases/Constants.php'; final class CreateTransactionIdTest extends TestCase From 4d1487fbb0fa2c45174d575cf4f582a9621f5228 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 2 Sep 2026 12:37:36 +0000 Subject: [PATCH 42/46] test(databases): drop HTTP create unit test Keep transactionId create coverage in the existing e2e txn suites. Co-authored-by: Jake Barnby --- .../Http/CreateTransactionIdTest.php | 71 ------------------- 1 file changed, 71 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php diff --git a/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php b/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php deleted file mode 100644 index 8d9890e959..0000000000 --- a/tests/unit/Platform/Modules/Databases/Http/CreateTransactionIdTest.php +++ /dev/null @@ -1,71 +0,0 @@ - $expected - */ - #[DataProvider('createRoutes')] - public function testCreateSdkMethodsExposeTransactionId(Route $route, array $expected): void - { - $methods = $route->getLabel('sdk'); - $this->assertIsArray($methods); - - $names = []; - foreach ($methods as $method) { - $this->assertInstanceOf(Method::class, $method); - $names[$method->getMethodName()] = \array_map( - static fn (Parameter $parameter): string => $parameter->getName(), - $method->getParameters(), - ); - } - - foreach ($expected as $methodName) { - $this->assertArrayHasKey($methodName, $names); - $this->assertContains( - 'transactionId', - $names[$methodName], - $route->getPath() . ' ' . $methodName . ' must expose transactionId', - ); - } - } - - /** - * @return array}> - */ - public static function createRoutes(): array - { - Method::$processed = []; - Method::$errors = []; - - return [ - 'documentsdb' => [ - new DocumentsDBCreate(), - ['createDocument', 'createDocuments'], - ], - 'vectorsdb' => [ - new VectorsDBCreate(), - ['createDocument', 'createDocuments'], - ], - ]; - } -} From 5f92312419d3f044e077a9a1c51baa01e35056ed Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:47:06 +0100 Subject: [PATCH 43/46] perf: cache geo and audit attribution lookups --- src/Appwrite/Event/Message/Audit.php | 2 ++ src/Appwrite/Geo/Geo.php | 5 +-- tests/unit/Geo/GeoTest.php | 48 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 tests/unit/Geo/GeoTest.php diff --git a/src/Appwrite/Event/Message/Audit.php b/src/Appwrite/Event/Message/Audit.php index 63d7842bfe..1305b87e92 100644 --- a/src/Appwrite/Event/Message/Audit.php +++ b/src/Appwrite/Event/Message/Audit.php @@ -30,6 +30,8 @@ final class Audit extends Base '$id' => $this->project->getId(), '$sequence' => $this->project->getSequence(), 'database' => $this->project->getAttribute('database', ''), + 'teamId' => $this->project->getAttribute('teamId', ''), + 'teamInternalId' => $this->project->getAttribute('teamInternalId', ''), ], 'user' => $this->user->getArrayCopy(), 'impersonatorUser' => $this->impersonatorUser->getArrayCopy(), diff --git a/src/Appwrite/Geo/Geo.php b/src/Appwrite/Geo/Geo.php index 9dce01cd05..ac9292a76d 100644 --- a/src/Appwrite/Geo/Geo.php +++ b/src/Appwrite/Geo/Geo.php @@ -11,7 +11,7 @@ use Utopia\Locale\Locale; class Geo { public const CACHE_SIZE = 10_000; - public const CACHE_VALUE_SIZE = 512; + public const CACHE_VALUE_SIZE = 1024; public function __construct( private ?Client $client, @@ -77,7 +77,8 @@ class Geo 'weatherCode' => $record['weatherCode'] ?? null, 'postalCode' => $record['postalCode'] ?? null, 'city' => $record['city']['en'] ?? null, - 'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? null, + 'state' => $record['subdivision']['en'] ?? $record['subdivisions'][0]['en'] ?? $record['subdivisions'][0]['names']['en'] ?? null, + 'subdivisions' => $record['subdivisions'] ?? null, 'autonomousSystemNumber' => $autonomousSystemNumber === null ? null : (string) $autonomousSystemNumber, 'autonomousSystemOrganization' => $record['autonomousSystemOrganization'] ?? null, 'connectionType' => $record['connection'] ?? null, diff --git a/tests/unit/Geo/GeoTest.php b/tests/unit/Geo/GeoTest.php new file mode 100644 index 0000000000..44caf7f490 --- /dev/null +++ b/tests/unit/Geo/GeoTest.php @@ -0,0 +1,48 @@ +createMock(Client::class); + $client->expects($this->once()) + ->method('lookup') + ->with('203.0.113.10') + ->willReturn([ + 'countryCode' => 'DE', + 'continentCode' => 'EU', + 'city' => ['en' => 'Frankfurt'], + 'subdivisions' => [ + ['iso_code' => 'HE', 'names' => ['en' => 'Hesse']], + ], + 'isp' => 'Example ISP', + ]); + + $cache = new Table(16); + $cache->column('value', Table::TYPE_STRING, Geo::CACHE_VALUE_SIZE); + $cache->create(); + + $geo = new Geo($client, new Locale('en'), $cache); + $geo->get('203.0.113.10'); + $cached = $geo->get('203.0.113.10'); + + $this->assertSame('Frankfurt', $cached->getAttribute('city')); + $this->assertSame('Hesse', $cached->getAttribute('subdivisions')[0]['names']['en']); + $this->assertSame('Example ISP', $cached->getAttribute('isp')); + } +} From da652e4b19f310ee4ff001330d95388eb63a2959 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 16:59:47 +0100 Subject: [PATCH 44/46] fix(storage): remove the bucket document when its files collection cannot be created Bucket creation writes the bucket document, then creates the bucket_ collection. The second step is DDL, cannot share a transaction with the first, and needs the document's sequence for its name. Any failure between the two (a lock wait timeout, a killed pod) left a bucket document with no collection under it: a bucket that can never hold a file, and that every later request fails on with "Collection not found". Cloud found 227 such buckets across regions. Delete the document when collection creation throws, then rethrow, so a failed create leaves nothing behind. The test asserts the buckets collection is empty after the failed request, and fails on the unpatched code. Co-Authored-By: Claude Fable 5.1 --- .../Modules/Storage/Http/Buckets/Create.php | 11 ++- .../Storage/Http/Buckets/CreateTest.php | 80 +++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php index c7101cf191..ea8c798ccd 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Create.php @@ -153,7 +153,16 @@ class Create extends Action $bucket = $dbForProject->getDocument('buckets', $bucketId); - $dbForProject->createCollection('bucket_' . $bucket->getSequence(), $attributes, $indexes, permissions: $permissions, documentSecurity: $fileSecurity); + // The files collection is DDL and cannot share a transaction with the + // document insert above, and its name needs the document's sequence, + // so the document is committed first. A bucket without its collection + // can never hold a file: remove it rather than leave an orphan. + try { + $dbForProject->createCollection('bucket_' . $bucket->getSequence(), $attributes, $indexes, permissions: $permissions, documentSecurity: $fileSecurity); + } catch (\Throwable $th) { + $dbForProject->deleteDocument('buckets', $bucketId); + throw $th; + } } catch (DuplicateException) { throw new Exception(Exception::STORAGE_BUCKET_ALREADY_EXISTS); } diff --git a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php new file mode 100644 index 0000000000..b9edcbfc67 --- /dev/null +++ b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php @@ -0,0 +1,80 @@ +createStub(Database::class); + $dbForProject->method('createDocument')->willReturnCallback( + static function (string $collection, Document $document) use (&$buckets): Document { + $buckets[$document->getId()] = $document->setAttribute('$sequence', '7'); + + return $buckets[$document->getId()]; + } + ); + $dbForProject->method('getDocument')->willReturnCallback( + static fn (string $collection, string $id): Document => $buckets[$id] ?? new Document() + ); + $dbForProject->method('deleteDocument')->willReturnCallback( + static function (string $collection, string $id) use (&$buckets): bool { + unset($buckets[$id]); + + return true; + } + ); + $dbForProject->method('createCollection')->willThrowException( + new RuntimeException('Lock wait timeout exceeded') + ); + + $response = $this->createStub(Response::class); + $queueForEvents = $this->createStub(Event::class); + + try { + (new Create())->action( + 'photos', + 'Photos', + null, + false, + true, + 30000000, + [], + null, + null, + true, + true, + $response, + $dbForProject, + $queueForEvents, + ); + $this->fail('A failed collection creation must fail the request.'); + } catch (RuntimeException) { + } + + $this->assertSame([], $buckets, 'A bucket whose files collection was never created must not survive the request.'); + } +} From d4abb88c8777d8f8223d57221ca8e27878ccf38c Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 17:05:23 +0100 Subject: [PATCH 45/46] test(storage): drop the bucket create unit test It asserted the double's bookkeeping of a deleteDocument call rather than anything a user can observe. Co-Authored-By: Claude Fable 5.1 --- .../Storage/Http/Buckets/CreateTest.php | 80 ------------------- 1 file changed, 80 deletions(-) delete mode 100644 tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php diff --git a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php b/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php deleted file mode 100644 index b9edcbfc67..0000000000 --- a/tests/unit/Platform/Modules/Storage/Http/Buckets/CreateTest.php +++ /dev/null @@ -1,80 +0,0 @@ -createStub(Database::class); - $dbForProject->method('createDocument')->willReturnCallback( - static function (string $collection, Document $document) use (&$buckets): Document { - $buckets[$document->getId()] = $document->setAttribute('$sequence', '7'); - - return $buckets[$document->getId()]; - } - ); - $dbForProject->method('getDocument')->willReturnCallback( - static fn (string $collection, string $id): Document => $buckets[$id] ?? new Document() - ); - $dbForProject->method('deleteDocument')->willReturnCallback( - static function (string $collection, string $id) use (&$buckets): bool { - unset($buckets[$id]); - - return true; - } - ); - $dbForProject->method('createCollection')->willThrowException( - new RuntimeException('Lock wait timeout exceeded') - ); - - $response = $this->createStub(Response::class); - $queueForEvents = $this->createStub(Event::class); - - try { - (new Create())->action( - 'photos', - 'Photos', - null, - false, - true, - 30000000, - [], - null, - null, - true, - true, - $response, - $dbForProject, - $queueForEvents, - ); - $this->fail('A failed collection creation must fail the request.'); - } catch (RuntimeException) { - } - - $this->assertSame([], $buckets, 'A bucket whose files collection was never created must not survive the request.'); - } -} From f9b9172185f7dc216820813b987af1558e81fd92 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 2 Sep 2026 21:45:36 +0100 Subject: [PATCH 46/46] test(vcs): expect the Gitea callback redirect in the project's region The callback builds its fallback redirect from the project's region, but the expected URL hardcoded project-default-. Cloud CI creates its test projects in another region, so testCreateInstallationWithEmptyRedirects and testCreateInstallationWithoutRedirects fail there since #13426. Expose the created project's region from the ProjectCustom scope and build the expected URL from it, so the test reads no environment. Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01UQPB3JWhybuoo1gHhqDBLa --- tests/e2e/Scopes/ProjectCustom.php | 1 + .../VCSGitea/VCSGiteaConsoleClientTest.php | 24 ++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/e2e/Scopes/ProjectCustom.php b/tests/e2e/Scopes/ProjectCustom.php index 951ad4b78b..098095ece8 100644 --- a/tests/e2e/Scopes/ProjectCustom.php +++ b/tests/e2e/Scopes/ProjectCustom.php @@ -255,6 +255,7 @@ trait ProjectCustom return [ '$id' => $project['body']['$id'], 'name' => $project['body']['name'], + 'region' => $project['body']['region'], 'apiKey' => $key['body']['secret'], 'webhookId' => $webhook['body']['$id'], 'signatureKey' => $webhook['body']['secret'], diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 233f5f99eb..ac0a58fa59 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -124,7 +124,7 @@ final class VCSGiteaConsoleClientTest extends Scope /** @var array $cookies */ $cookies = []; $this->giteaCookies = $cookies; - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + $consoleUrl = $this->gitInstallationsUrl($projectId); $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ 'x-appwrite-project' => $projectId, @@ -376,7 +376,7 @@ final class VCSGiteaConsoleClientTest extends Scope public function testCreateInstallationWithTamperedState(): void { $projectId = $this->getProject()['$id']; - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + $consoleUrl = $this->gitInstallationsUrl($projectId); $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); $state['projectId'] = 'victim-project'; @@ -392,7 +392,7 @@ final class VCSGiteaConsoleClientTest extends Scope public function testCreateInstallationWithoutCode(): void { $projectId = $this->getProject()['$id']; - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + $consoleUrl = $this->gitInstallationsUrl($projectId); // Signed state, no code: the failure redirect carries the error as a query string $response = $this->callGiteaCallbackHelper([ @@ -415,7 +415,7 @@ final class VCSGiteaConsoleClientTest extends Scope ]); $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith('http://localhost/console/project-default-' . $projectId . '/settings/git-installations?error=', (string) $response['headers']['location']); + $this->assertStringStartsWith($this->gitInstallationsUrl($projectId) . '?error=', (string) $response['headers']['location']); } public function testCreateInstallationWithInvalidState(): void @@ -427,7 +427,7 @@ final class VCSGiteaConsoleClientTest extends Scope public function testCreateInstallationWithUnknownProject(): void { - $consoleUrl = 'http://localhost/console/project-default-missing/settings/git-installations'; + $consoleUrl = $this->gitInstallationsUrl('missing'); $response = $this->callGiteaCallbackHelper([ 'code' => 'unused', @@ -441,7 +441,7 @@ final class VCSGiteaConsoleClientTest extends Scope public function testCreateInstallationWithLongState(): void { $projectId = $this->getProject()['$id']; - $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + $consoleUrl = $this->gitInstallationsUrl($projectId); // Past the old 2048 cap: redirect URLs are not length-limited, so the // authorize endpoint can produce a state this size itself. @@ -465,6 +465,18 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertNotEmpty($installation['$id']); } + /** + * The callback builds its fallback redirect from the project's region, so the + * expected URL follows the region the scope's project was created in: Cloud + * CI creates projects in a region other than default. + */ + private function gitInstallationsUrl(string $projectId): string + { + $region = $this->getProject()['region']; + + return "http://localhost/console/project-{$region}-{$projectId}/settings/git-installations"; + } + /** * @return array{userId: string, email: string, session: string, teamId: string, projectId: string, headers: array} */