From dfd64e5ac11e8ee61f70317112d5a34d5d42fcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 09:58:48 +0200 Subject: [PATCH 01/38] Add codebase VCS endpoints --- app/config/variables.php | 18 +++ docker-compose.yml | 3 + .../vcs/create-codebase-installation.md | 1 + .../VCS/Http/Codebase/Authorize/Get.php | 97 +++++++++++++ .../VCS/Http/Codebase/Callback/Get.php | 137 ++++++++++++++++++ .../VCS/Http/Codebase/Events/Create.php | 46 ++++++ .../Platform/Modules/VCS/Services/Http.php | 8 + 7 files changed, 310 insertions(+) create mode 100644 docs/references/vcs/create-codebase-installation.md create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php diff --git a/app/config/variables.php b/app/config/variables.php index 7e32f6ea16..346f9bcc59 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1392,6 +1392,24 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_VCS_CODEBASE_CLIENT_ID', + 'description' => 'Codebase app client ID. You can find it in your Codebase app details on Cursor.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], + [ + 'name' => '_APP_VCS_CODEBASE_PRIVATE_KEY', + 'description' => 'Codebase app private key. You can generate private keys from your Codebase app settings on Cursor.', + 'introduction' => '2.0.0', + 'default' => '', + 'required' => false, + 'question' => '', + 'filter' => '' + ], [ 'name' => '_APP_VCS_WEBHOOK_URL', 'description' => 'Base URL Appwrite advertises to self-hosted VCS providers when registering repository webhooks, if different from the public domain (e.g. a Docker-internal address). Falls back to the public domain when unset.', diff --git a/docker-compose.yml b/docker-compose.yml index 493b597c62..1372bca12a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -210,6 +210,8 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET + - _APP_VCS_CODEBASE_CLIENT_ID + - _APP_VCS_CODEBASE_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET @@ -512,6 +514,7 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET + - _APP_VCS_CODEBASE_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_FUNCTIONS_TIMEOUT - _APP_SITES_TIMEOUT diff --git a/docs/references/vcs/create-codebase-installation.md b/docs/references/vcs/create-codebase-installation.md new file mode 100644 index 0000000000..5fcff97be9 --- /dev/null +++ b/docs/references/vcs/create-codebase-installation.md @@ -0,0 +1 @@ +Begin Appwrite's Codebase app installation to set up version control integration. This endpoint responds with a redirect URL to the Codebase app's installation page on Cursor. The Codebase app must be configured in your environment for this endpoint to work. \ No newline at end of file diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php new file mode 100644 index 0000000000..97e3dd6d60 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -0,0 +1,97 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/vcs/codebase/authorize') + ->desc('Create Codebase app installation') + ->groups(['api', 'vcs']) + ->label('scope', 'vcs.read') + ->label('error', APP_VIEWS_DIR . '/general/error.phtml') + ->label('sdk', new Method( + namespace: 'vcs', + group: 'installations', + name: 'createCodebaseInstallation', + description: '/docs/references/vcs/create-codebase-installation.md', + auth: [AuthType::ADMIN], + responses: [ + new SDKResponse( + code: Response::STATUS_CODE_MOVED_PERMANENTLY, + model: Response::MODEL_NONE, + ) + ], + contentType: ContentType::HTML, + type: MethodType::WEBAUTH, + // Preview SDK builds show the whole surface, so they do not hide. + hide: System::getEnv('_APP_SDK_PREVIEW', 'disabled') !== 'enabled', + )) + ->param('success', '', fn ($redirectValidator) => $redirectValidator, 'URL to redirect back to console after a successful installation attempt.', true, ['redirectValidator']) + ->param('failure', '', fn ($redirectValidator) => $redirectValidator, 'URL to redirect back to console after a failed installation attempt.', true, ['redirectValidator']) + ->inject('response') + ->inject('project') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + string $success, + string $failure, + Response $response, + Document $project, + array $platform + ) { + $clientId = System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID'); + + if (empty($clientId)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Codebase client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); + } + + // The callback endpoint is public and Codebase performs no token + // exchange, so the callback verifies this signature before trusting + // the projectId and redirect URLs in state. + $state = \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', '')), + ]); + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $hostname = $platform['consoleHostname'] ?? ''; + + $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query([ + 'client_id' => $clientId, + 'state' => $state, + 'redirect_uri' => $protocol . '://' . $hostname . '/v1/vcs/codebase/callback', + ]); + + $response + ->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/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php new file mode 100644 index 0000000000..a0390e3972 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php @@ -0,0 +1,137 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/vcs/codebase/callback') + ->desc('Get installation from Codebase app') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->label('error', APP_VIEWS_DIR . '/general/error.phtml') + ->param('installation_id', '', new Text(256, 0), 'Codebase installation ID', true) + ->param('state', '', new Text(2048), 'Codebase state. Contains info sent when starting the installation flow.', true) + ->inject('response') + ->inject('dbForPlatform') + ->inject('platform') + ->callback($this->action(...)); + } + + public function action( + string $providerInstallationId, + string $state, + Response $response, + Database $dbForPlatform, + array $platform + ) { + 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'] ?? ''; + + // This endpoint is public and Codebase performs no token exchange -- + // 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()) { + $this->failure($response, $redirectFailure, 'Project with the ID from state could not be found.', Exception::PROJECT_NOT_FOUND); + return; + } + + $region = $project->getAttribute('region', 'default'); + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $hostname = $platform['consoleHostname'] ?? ''; + + $defaultState = [ + 'success' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations", + 'failure' => $protocol . '://' . $hostname . "/console/project-$region-$projectId/settings/git-installations", + ]; + + $state = \array_merge($defaultState, \array_filter($state)); + $redirectSuccess = $state['success'] ?? ''; + $redirectFailure = $state['failure'] ?? ''; + + if (empty($providerInstallationId)) { + $this->failure($response, $redirectFailure, 'Codebase installation ID is missing.'); + return; + } + + $projectInternalId = $project->getSequence(); + + $installation = $dbForPlatform->findOne('installations', [ + Query::equal('providerInstallationId', [$providerInstallationId]), + Query::equal('projectInternalId', [$projectInternalId]), + Query::equal('provider', ['codebase']), + ]); + + if ($installation->isEmpty()) { + $teamId = $project->getAttribute('teamId', ''); + + $dbForPlatform->createDocument('installations', new Document([ + '$id' => ID::unique(), + '$permissions' => $this->getPermissions($teamId, $projectId), + 'providerInstallationId' => $providerInstallationId, + 'projectId' => $projectId, + 'projectInternalId' => $projectInternalId, + 'provider' => 'codebase', + 'organization' => '', + 'personal' => false, + ])); + } + + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirectSuccess); + } + + /** + * Redirect back to the console with an error, or throw when no redirect is available. + */ + private function failure(Response $response, string $redirect, string $error, string $type = Exception::GENERAL_ARGUMENT_INVALID): void + { + if (empty($redirect)) { + throw new Exception($type, $error); + } + + $separator = \str_contains($redirect, '?') ? '&' : '?'; + $response + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Pragma', 'no-cache') + ->redirect($redirect . $separator . \http_build_query(['error' => $error])); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php new file mode 100644 index 0000000000..432e3fb81d --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php @@ -0,0 +1,46 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) + ->setHttpPath('/v1/vcs/codebase/events') + ->desc('Create event') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->inject('request') + ->inject('response') + ->callback($this->action(...)); + } + + public function action( + Request $request, + Response $response + ) { + $payload = \json_decode($request->getRawPayload(), true) ?? []; + + $event = $request->getHeaderLine('x-codebase-event', '') ?: ($payload['event'] ?? ''); + Span::add('vcs.codebase.event.name', $event); + + // Codebase does not deliver push or pull request events yet, so events + // are only acknowledged to prevent delivery retries. + $response->json(['success' => true]); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index e078c8fca0..13a8e5bb74 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -5,6 +5,9 @@ namespace Appwrite\Platform\Modules\VCS\Services; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Authorize\Get as GetBitbucketAuthorize; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Callback\Get as GetBitbucketCallback; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Events\Create as CreateBitbucketEvent; +use Appwrite\Platform\Modules\VCS\Http\Codebase\Authorize\Get as GetCodebaseAuthorize; +use Appwrite\Platform\Modules\VCS\Http\Codebase\Callback\Get as GetCodebaseCallback; +use Appwrite\Platform\Modules\VCS\Http\Codebase\Events\Create as CreateCodebaseEvent; use Appwrite\Platform\Modules\VCS\Http\Gitea\Authorize\Get as GetGiteaAuthorize; use Appwrite\Platform\Modules\VCS\Http\Gitea\Callback\Get as GetGiteaCallback; use Appwrite\Platform\Modules\VCS\Http\Gitea\Events\Create as CreateGiteaEvent; @@ -50,6 +53,10 @@ class Http extends Service $this->addAction(GetBitbucketAuthorize::getName(), new GetBitbucketAuthorize()); $this->addAction(GetBitbucketCallback::getName(), new GetBitbucketCallback()); + // Codebase Authorization & Callback + $this->addAction(GetCodebaseAuthorize::getName(), new GetCodebaseAuthorize()); + $this->addAction(GetCodebaseCallback::getName(), new GetCodebaseCallback()); + // Installations $this->addAction(GetInstallation::getName(), new GetInstallation()); $this->addAction(ListInstallations::getName(), new ListInstallations()); @@ -69,5 +76,6 @@ class Http extends Service $this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent()); $this->addAction(CreateGitlabEvent::getName(), new CreateGitlabEvent()); $this->addAction(CreateBitbucketEvent::getName(), new CreateBitbucketEvent()); + $this->addAction(CreateCodebaseEvent::getName(), new CreateCodebaseEvent()); } } From 319a7e63a5bd40a7f8226962ad1e7820a475b543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:00:24 +0200 Subject: [PATCH 02/38] Add extensive logging for debugging purposes --- .../VCS/Http/Codebase/Authorize/Get.php | 5 +++ .../VCS/Http/Codebase/Callback/Get.php | 40 ++++++++++++++++++- .../VCS/Http/Codebase/Events/Create.php | 8 ++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index 97e3dd6d60..8d8cb8ca0c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\MethodType; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Utopia\Console; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -89,6 +90,10 @@ class Get extends Action 'redirect_uri' => $protocol . '://' . $hostname . '/v1/vcs/codebase/callback', ]); + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); + Console::log('[CODEBASE DEBUG] Redirecting to install URL: ' . $url); + $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php index a0390e3972..5bf3df064c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php @@ -4,7 +4,9 @@ namespace Appwrite\Platform\Modules\VCS\Http\Codebase\Callback; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; +use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -35,6 +37,7 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'Codebase installation ID', true) ->param('state', '', new Text(2048), 'Codebase state. Contains info sent when starting the installation flow.', true) + ->inject('request') ->inject('response') ->inject('dbForPlatform') ->inject('platform') @@ -44,10 +47,29 @@ class Get extends Action public function action( string $providerInstallationId, string $state, + Request $request, Response $response, Database $dbForPlatform, array $platform ) { + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + $params = $request->getParams(); + Console::log('[CODEBASE DEBUG] Callback received'); + Console::log('[CODEBASE DEBUG] Callback params: ' . \json_encode($params)); + Console::log('[CODEBASE DEBUG] Callback headers: ' . \json_encode($request->getHeaders())); + Console::log('[CODEBASE DEBUG] Callback installation_id: "' . $providerInstallationId . '"'); + + // Surface anything that looks like an OAuth2 token flow (e.g. a code + // exchange), since Codebase is not expected to send one. + $oauthParams = \array_intersect_key($params, \array_flip([ + 'code', 'token', 'access_token', 'refresh_token', 'expires_in', 'token_type', 'scope', 'id_token', 'setup_action', 'authuser', 'session_state', + ])); + if (!empty($oauthParams)) { + Console::log('[CODEBASE DEBUG] Possible OAuth2 flow params detected: ' . \json_encode($oauthParams)); + } else { + Console::log('[CODEBASE DEBUG] No OAuth2 flow params detected'); + } + if (empty($state)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.'); } @@ -56,14 +78,20 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] Decoded state: ' . \json_encode($state)); + // This endpoint is public and Codebase performs no token exchange -- // 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'] ?? '')) { + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] State signature mismatch, rejecting callback'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } + Console::log('[CODEBASE DEBUG] State signature valid'); $project = $dbForPlatform->getDocument('projects', $projectId); @@ -101,7 +129,7 @@ class Get extends Action if ($installation->isEmpty()) { $teamId = $project->getAttribute('teamId', ''); - $dbForPlatform->createDocument('installations', new Document([ + $installation = $dbForPlatform->createDocument('installations', new Document([ '$id' => ID::unique(), '$permissions' => $this->getPermissions($teamId, $projectId), 'providerInstallationId' => $providerInstallationId, @@ -111,8 +139,15 @@ class Get extends Action 'organization' => '', 'personal' => false, ])); + + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] Created installation "' . $installation->getId() . '" for project "' . $projectId . '"'); + } else { + Console::log('[CODEBASE DEBUG] Installation already exists: "' . $installation->getId() . '"'); } + Console::log('[CODEBASE DEBUG] Redirecting to success URL: ' . $redirectSuccess); + $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') @@ -124,6 +159,9 @@ class Get extends Action */ private function failure(Response $response, string $redirect, string $error, string $type = Exception::GENERAL_ARGUMENT_INVALID): void { + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] Callback failed: ' . $error . ' (redirect: "' . $redirect . '")'); + if (empty($redirect)) { throw new Exception($type, $error); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php index 432e3fb81d..e44aae970d 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\Codebase\Events; use Appwrite\Platform\Action; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Utopia\Console; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; @@ -34,10 +35,17 @@ class Create extends Action Request $request, Response $response ) { + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + Console::log('[CODEBASE DEBUG] Event received'); + Console::log('[CODEBASE DEBUG] Event headers: ' . \json_encode($request->getHeaders())); + Console::log('[CODEBASE DEBUG] Event query params: ' . \json_encode($request->getParams())); + Console::log('[CODEBASE DEBUG] Event raw payload: ' . $request->getRawPayload()); + $payload = \json_decode($request->getRawPayload(), true) ?? []; $event = $request->getHeaderLine('x-codebase-event', '') ?: ($payload['event'] ?? ''); Span::add('vcs.codebase.event.name', $event); + Console::log('[CODEBASE DEBUG] Event name resolved to: "' . $event . '"'); // Codebase does not deliver push or pull request events yet, so events // are only acknowledged to prevent delivery retries. From 30e363cc14d92b66203495ef0a07827bdcfcaa7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:00:49 +0200 Subject: [PATCH 03/38] Code quality nitpick --- docs/references/vcs/create-codebase-installation.md | 1 - .../Platform/Modules/VCS/Http/Codebase/Authorize/Get.php | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 docs/references/vcs/create-codebase-installation.md diff --git a/docs/references/vcs/create-codebase-installation.md b/docs/references/vcs/create-codebase-installation.md deleted file mode 100644 index 5fcff97be9..0000000000 --- a/docs/references/vcs/create-codebase-installation.md +++ /dev/null @@ -1 +0,0 @@ -Begin Appwrite's Codebase app installation to set up version control integration. This endpoint responds with a redirect URL to the Codebase app's installation page on Cursor. The Codebase app must be configured in your environment for this endpoint to work. \ No newline at end of file diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index 8d8cb8ca0c..360ab62831 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -37,7 +37,7 @@ class Get extends Action namespace: 'vcs', group: 'installations', name: 'createCodebaseInstallation', - description: '/docs/references/vcs/create-codebase-installation.md', + description: 'Begin Appwrite\'s Codebase app installation to set up version control integration. This endpoint responds with a redirect URL to the Codebase app\'s installation page on Cursor. The Codebase app must be configured in your environment for this endpoint to work.', auth: [AuthType::ADMIN], responses: [ new SDKResponse( From 31411328598d6205ba1694bce7d73e8f896bc21b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:09:55 +0200 Subject: [PATCH 04/38] Fix "requested_scopes requires at least one scope" --- .../Platform/Modules/VCS/Http/Codebase/Authorize/Get.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index 360ab62831..4b75b1f258 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -86,6 +86,7 @@ class Get extends Action $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query([ 'client_id' => $clientId, + 'source' => 'app-metadata', 'state' => $state, 'redirect_uri' => $protocol . '://' . $hostname . '/v1/vcs/codebase/callback', ]); From 7cae8eaa78f06dedabbb4a3b9b1b4fc5f396f99f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:29:39 +0200 Subject: [PATCH 05/38] Simplify useless integration (not working) --- .../VCS/Http/Codebase/Authorize/Get.php | 44 +++++++++++++------ .../VCS/Http/Codebase/Callback/Get.php | 19 ++++++++ 2 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index 4b75b1f258..d1c8fedb60 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -71,25 +71,41 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Codebase client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); } - // The callback endpoint is public and Codebase performs no token - // exchange, so the callback verifies this signature before trusting - // the projectId and redirect URLs in state. - $state = \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', '')), - ]); - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = $platform['consoleHostname'] ?? ''; - $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query([ + $callback = $protocol . '://' . $hostname . '/v1/vcs/codebase/callback'; + + $query = [ 'client_id' => $clientId, 'source' => 'app-metadata', - 'state' => $state, - 'redirect_uri' => $protocol . '://' . $hostname . '/v1/vcs/codebase/callback', - ]); + ]; + + // The correct redirect parameter name is undocumented, so send the + // callback URL under every plausible spelling -- Codebase should honor + // whichever one it recognises and ignore the rest. + // TODO: Narrow to the single correct parameter once confirmed. + foreach ([ + 'redirect_uri', + 'redirect_url', + 'redirectUri', + 'redirectUrl', + 'callback_uri', + 'callback_url', + 'callbackUri', + 'callbackUrl', + 'callback', + 'return_uri', + 'return_url', + 'returnUri', + 'returnUrl', + 'return_to', + 'returnTo', + ] as $param) { + $query[$param] = $callback; + } + + $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query($query); // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. Console::log('[CODEBASE DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php index 5bf3df064c..12943767ad 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php @@ -36,6 +36,7 @@ class Get extends Action ->label('scope', 'public') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'Codebase installation ID', true) + ->param('installation_receipt', '', new Text(4096, 0), 'Codebase installation receipt JWT, signed by Cursor.', true) ->param('state', '', new Text(2048), 'Codebase state. Contains info sent when starting the installation flow.', true) ->inject('request') ->inject('response') @@ -46,6 +47,7 @@ class Get extends Action public function action( string $providerInstallationId, + string $installationReceipt, string $state, Request $request, Response $response, @@ -70,6 +72,23 @@ class Get extends Action Console::log('[CODEBASE DEBUG] No OAuth2 flow params detected'); } + // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + // Decode only -- the EdDSA signature is not verified, so the claims + // must not be trusted for authorization decisions yet. + if (!empty($installationReceipt)) { + $segments = \explode('.', $installationReceipt); + if (\count($segments) === 3) { + $receiptHeader = \json_decode(\base64_decode(\strtr($segments[0], '-_', '+/')), true) ?? []; + $receiptClaims = \json_decode(\base64_decode(\strtr($segments[1], '-_', '+/')), true) ?? []; + Console::log('[CODEBASE DEBUG] Receipt JWT header: ' . \json_encode($receiptHeader)); + Console::log('[CODEBASE DEBUG] Receipt JWT claims: ' . \json_encode($receiptClaims)); + } else { + Console::log('[CODEBASE DEBUG] installation_receipt is not a JWT: ' . $installationReceipt); + } + } else { + Console::log('[CODEBASE DEBUG] No installation_receipt received'); + } + if (empty($state)) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing state parameter. Please restart the installation from the Appwrite Console.'); } From fc1dc71fea89b5b5518e0c631ef83837f17a17c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:32:21 +0200 Subject: [PATCH 06/38] JWKs verficiation --- .../VCS/Http/Codebase/Callback/Get.php | 139 +++++++++++++++++- 1 file changed, 138 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php index 12943767ad..f1d23c5d43 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php @@ -11,6 +11,7 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; +use Utopia\Fetch\Client as FetchClient; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -21,6 +22,18 @@ class Get extends Action use HTTP; use AppwritePermission; + /** + * Issuer and JWKS endpoint published by Cursor's OIDC discovery document at + * https://api.cursor.com/v1/origin/.well-known/openid-configuration + */ + private const RECEIPT_ISSUER = 'https://api.cursor.com/v1/origin'; + private const RECEIPT_JWKS_URL = 'https://api.cursor.com/v1/origin/keys'; + + /** + * Clock-skew tolerance, in seconds, applied to receipt exp/nbf checks. + */ + private const RECEIPT_LEEWAY = 60; + public static function getName() { return 'getVCSCodebaseCallback'; @@ -137,6 +150,22 @@ class Get extends Action return; } + // Authenticate the callback by verifying the installation receipt + // against Cursor's published JWKS. This proves the request genuinely + // originates from Codebase (the callback is public and has no token + // exchange) before any installation is created. + try { + $receiptClaims = $this->verifyReceipt($installationReceipt, $providerInstallationId); + } catch (\Throwable $e) { + Console::log('[CODEBASE DEBUG] Receipt verification failed: ' . $e->getMessage()); + $this->failure($response, $redirectFailure, 'Could not verify the Codebase installation receipt.'); + return; + } + + // The Cursor namespace (team/workspace) owning the installation. + $organization = $receiptClaims['namespace_id'] ?? ''; + Console::log('[CODEBASE DEBUG] Receipt verified, namespace_id: "' . $organization . '"'); + $projectInternalId = $project->getSequence(); $installation = $dbForPlatform->findOne('installations', [ @@ -155,13 +184,17 @@ class Get extends Action 'projectId' => $projectId, 'projectInternalId' => $projectInternalId, 'provider' => 'codebase', - 'organization' => '', + 'organization' => $organization, 'personal' => false, ])); // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. Console::log('[CODEBASE DEBUG] Created installation "' . $installation->getId() . '" for project "' . $projectId . '"'); } else { + $installation = $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ + 'organization' => $organization, + ])); + Console::log('[CODEBASE DEBUG] Installation already exists: "' . $installation->getId() . '"'); } @@ -191,4 +224,108 @@ class Get extends Action ->addHeader('Pragma', 'no-cache') ->redirect($redirect . $separator . \http_build_query(['error' => $error])); } + + /** + * Verify a Codebase installation receipt (an EdDSA-signed JWT) against + * Cursor's published JWKS and validate its claims. Returns the verified + * claims, or throws on any failure. + * + * @return array + */ + private function verifyReceipt(string $jwt, string $expectedInstallationId): array + { + if (empty($jwt)) { + throw new \Exception('receipt is missing'); + } + + $segments = \explode('.', $jwt); + if (\count($segments) !== 3) { + throw new \Exception('receipt is not a JWT'); + } + + [$headerB64, $payloadB64, $signatureB64] = $segments; + $header = \json_decode($this->base64UrlDecode($headerB64), true); + $claims = \json_decode($this->base64UrlDecode($payloadB64), true); + $signature = $this->base64UrlDecode($signatureB64); + + if (!\is_array($header) || !\is_array($claims)) { + throw new \Exception('receipt segments are not valid JSON'); + } + + if (($header['alg'] ?? '') !== 'EdDSA') { + throw new \Exception('unexpected signing algorithm'); + } + + $kid = $header['kid'] ?? ''; + if (empty($kid)) { + throw new \Exception('receipt is missing key id'); + } + + $publicKey = null; + foreach ($this->fetchJwks() as $key) { + if (($key['kid'] ?? '') === $kid && ($key['kty'] ?? '') === 'OKP' && ($key['crv'] ?? '') === 'Ed25519') { + $publicKey = $this->base64UrlDecode($key['x'] ?? ''); + break; + } + } + + if ($publicKey === null || \strlen($publicKey) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { + throw new \Exception('no matching signing key in JWKS'); + } + + if (!\sodium_crypto_sign_verify_detached($signature, $headerB64 . '.' . $payloadB64, $publicKey)) { + throw new \Exception('signature is invalid'); + } + + if (($claims['iss'] ?? '') !== self::RECEIPT_ISSUER) { + throw new \Exception('unexpected issuer'); + } + + $clientId = System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID', ''); + if (($claims['aud'] ?? '') !== $clientId) { + throw new \Exception('receipt audience does not match this app'); + } + + // Bind the receipt to the installation id in the callback so a valid + // receipt cannot be replayed to attach a different installation. + if (($claims['sub'] ?? '') !== $expectedInstallationId) { + throw new \Exception('receipt subject does not match installation id'); + } + + $now = \time(); + if (isset($claims['exp']) && $now >= (int)$claims['exp'] + self::RECEIPT_LEEWAY) { + throw new \Exception('receipt has expired'); + } + if (isset($claims['nbf']) && $now < (int)$claims['nbf'] - self::RECEIPT_LEEWAY) { + throw new \Exception('receipt is not yet valid'); + } + + return $claims; + } + + /** + * Fetch Cursor's origin JWKS (the Ed25519 public keys that sign receipts). + * + * @return array> + */ + private function fetchJwks(): array + { + $client = new FetchClient(); + $client->addHeader('Accept', 'application/json'); + + $response = $client->fetch(url: self::RECEIPT_JWKS_URL, method: FetchClient::METHOD_GET); + $body = \json_decode($response->getBody(), true); + + return \is_array($body) && isset($body['keys']) && \is_array($body['keys']) ? $body['keys'] : []; + } + + private function base64UrlDecode(string $data): string + { + $remainder = \strlen($data) % 4; + if ($remainder > 0) { + $data .= \str_repeat('=', 4 - $remainder); + } + + return (string)\base64_decode(\strtr($data, '-_', '+/'), true); + } } From d0f0d2b32537399833fd56928e09f578cc75147e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:33:37 +0200 Subject: [PATCH 07/38] Cleanup of failed PoC --- .../VCS/Http/Codebase/Authorize/Get.php | 37 ++----------------- 1 file changed, 4 insertions(+), 33 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index d1c8fedb60..62fdd2ee28 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -71,41 +71,12 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Codebase client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); } - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; - $hostname = $platform['consoleHostname'] ?? ''; - - $callback = $protocol . '://' . $hostname . '/v1/vcs/codebase/callback'; - - $query = [ + // Codebase ignores a redirect parameter on the install URL; the + // callback URL is taken from the app's own configuration on Cursor. + $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query([ 'client_id' => $clientId, 'source' => 'app-metadata', - ]; - - // The correct redirect parameter name is undocumented, so send the - // callback URL under every plausible spelling -- Codebase should honor - // whichever one it recognises and ignore the rest. - // TODO: Narrow to the single correct parameter once confirmed. - foreach ([ - 'redirect_uri', - 'redirect_url', - 'redirectUri', - 'redirectUrl', - 'callback_uri', - 'callback_url', - 'callbackUri', - 'callbackUrl', - 'callback', - 'return_uri', - 'return_url', - 'returnUri', - 'returnUrl', - 'return_to', - 'returnTo', - ] as $param) { - $query[$param] = $callback; - } - - $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query($query); + ]); // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. Console::log('[CODEBASE DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); From 39053e075b7f2fba6d09b460ea727268240f16a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:41:09 +0200 Subject: [PATCH 08/38] Added cursor oauth2 provider --- src/Appwrite/Auth/OAuth2/Cursor.php | 311 ++++++++++++++++++ .../VCS/Http/Codebase/Authorize/Get.php | 9 +- .../VCS/Http/Codebase/Callback/Get.php | 132 +------- 3 files changed, 329 insertions(+), 123 deletions(-) create mode 100644 src/Appwrite/Auth/OAuth2/Cursor.php diff --git a/src/Appwrite/Auth/OAuth2/Cursor.php b/src/Appwrite/Auth/OAuth2/Cursor.php new file mode 100644 index 0000000000..4babadf334 --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/Cursor.php @@ -0,0 +1,311 @@ + $this->appID, + ]; + + if (empty($this->getScopes())) { + // Without an explicit scope list the install page rejects the + // request ("requested_scopes requires at least one scope") unless + // told to read the scopes from the app's registered metadata. + $params['source'] = 'app-metadata'; + } else { + $params['scope'] = \implode(' ', $this->getScopes()); + } + + return 'https://cursor.com/codebase/apps/install?' . \http_build_query($params); + } + + /** + * Verifies an installation receipt JWT against Cursor's JWKS and returns + * its claims. The `sub` claim is the installation ID, `state` echoes the + * state given to the install URL, and `namespace_id` is the workspace. + * + * @return array + * @throws Exception + */ + public function verifyReceipt(string $receipt): array + { + $segments = \explode('.', $receipt); + + if (\count($segments) !== 3) { + throw new Exception('Installation receipt is not a JWT'); + } + + [$headerSegment, $claimsSegment, $signatureSegment] = $segments; + + $header = \json_decode($this->base64UrlDecode($headerSegment), true); + $claims = \json_decode($this->base64UrlDecode($claimsSegment), true); + $signature = $this->base64UrlDecode($signatureSegment); + + if (!\is_array($header) || !\is_array($claims)) { + throw new Exception('Installation receipt is malformed'); + } + + if (($header['alg'] ?? '') !== 'EdDSA') { + throw new Exception('Installation receipt has an unexpected algorithm'); + } + + // Receipts are documented to carry this media type; tolerate its + // absence, but never another Cursor-signed token kind in its place. + if (isset($header['typ']) && $header['typ'] !== 'origin-installation-receipt+jwt') { + throw new Exception('Installation receipt has an unexpected type'); + } + + $key = $this->getPublicKey($header['kid'] ?? ''); + + if ( + \strlen($signature) !== SODIUM_CRYPTO_SIGN_BYTES + || !\sodium_crypto_sign_verify_detached($signature, $headerSegment . '.' . $claimsSegment, $key) + ) { + throw new Exception('Installation receipt signature is invalid'); + } + + if (($claims['iss'] ?? '') !== $this->endpoint) { + throw new Exception('Installation receipt issuer is invalid'); + } + + if (($claims['aud'] ?? '') !== $this->appID) { + throw new Exception('Installation receipt audience does not match the configured client ID'); + } + + $now = \time(); + $leeway = 60; + + // Receipts are short-lived, single-use artifacts (about a five-minute + // window per Cursor's verification rules). + if (isset($claims['exp']) && $now >= (int)$claims['exp'] + $leeway) { + throw new Exception('Installation receipt has expired'); + } + + if (isset($claims['nbf']) && $now < (int)$claims['nbf'] - $leeway) { + throw new Exception('Installation receipt is not yet valid'); + } + + if (isset($claims['iat']) && $now < (int)$claims['iat'] - $leeway) { + throw new Exception('Installation receipt is issued in the future'); + } + + if (empty($claims['sub'])) { + throw new Exception('Installation receipt is missing the installation ID'); + } + + return $claims; + } + + /** + * App JWT authenticating this app to Cursor's API, signed with the app's + * Ed25519 private key. Used as a Bearer token for app-level endpoints, + * most importantly minting installation access tokens. + */ + public function getAppJWT(): string + { + $now = \time(); + + $headerSegment = $this->base64UrlEncode(\json_encode([ + 'alg' => 'EdDSA', + 'kid' => $this->appID, + 'typ' => 'JWT', + ])); + + $claimsSegment = $this->base64UrlEncode(\json_encode([ + 'iss' => $this->appID, + 'aud' => 'origin-apps', + 'iat' => $now, + 'exp' => $now + 300, + ])); + + $signature = \sodium_crypto_sign_detached($headerSegment . '.' . $claimsSegment, $this->getSigningKey()); + + return $headerSegment . '.' . $claimsSegment . '.' . $this->base64UrlEncode($signature); + } + + /** + * There is no authorization-code exchange: the "code" is the installation + * ID from the receipt's `sub` claim, exchanged for a short-lived (at most + * 15 minutes) installation access token. An empty or omitted scope list + * inherits the full installation grant. + * + * @param string $code + * + * @return array + */ + protected function getTokens(string $code): array + { + if (empty($this->tokens)) { + $payload = empty($this->getScopes()) + ? '{}' + : \json_encode(['scopes' => $this->getScopes()]); + + $response = \json_decode($this->request( + 'POST', + $this->endpoint . '/app/installations/' . \urlencode($code) . '/access_tokens', + [ + 'Authorization: Bearer ' . $this->getAppJWT(), + 'Content-Type: application/json', + ], + $payload + ), true); + + $this->tokens = [ + 'access_token' => $response['token'] ?? '', + 'expires_in' => isset($response['expiresAt']) + ? \max(0, (int)\strtotime($response['expiresAt']) - \time()) + : 0, + ]; + } + + return $this->tokens; + } + + /** + * Installation tokens cannot be refreshed -- a new one is minted from the + * installation ID with the app JWT. + * + * @param string $refreshToken + * + * @return array + */ + public function refreshTokens(string $refreshToken): array + { + $this->tokens = []; + + return $this->getTokens($refreshToken); + } + + /** + * Cursor Origin apps act as an installation, never as a user, so there is + * no user identity behind an access token. + */ + public function getUserID(string $accessToken): string + { + return ''; + } + + public function getUserEmail(string $accessToken): string + { + return ''; + } + + public function isEmailVerified(string $accessToken): bool + { + return false; + } + + public function getUserName(string $accessToken): string + { + return ''; + } + + /** + * Ed25519 public key for a JWKS key ID. + * + * @throws Exception + */ + protected function getPublicKey(string $kid): string + { + if (empty($this->jwks)) { + $this->jwks = \json_decode($this->request('GET', $this->endpoint . '/keys'), true)['keys'] ?? []; + } + + foreach ($this->jwks as $key) { + if ( + ($key['kid'] ?? '') === $kid + && ($key['kty'] ?? '') === 'OKP' + && ($key['crv'] ?? '') === 'Ed25519' + ) { + $publicKey = $this->base64UrlDecode($key['x'] ?? ''); + + if (\strlen($publicKey) === SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { + return $publicKey; + } + } + } + + throw new Exception('Installation receipt is signed with an unknown key'); + } + + /** + * Ed25519 secret key derived from the PKCS#8 PEM in $appSecret. + * + * @throws Exception + */ + protected function getSigningKey(): string + { + // Keys configured through environment variables carry literal \n + // sequences instead of newlines. + $pem = \str_replace(['\n', '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----'], '', $this->appSecret); + $der = \base64_decode(\preg_replace('/\s+/', '', $pem) ?? '', true); + + // RFC 8410 PKCS#8 wraps the 32-byte Ed25519 seed in a nested octet + // string tagged 0x04 0x22 0x04 0x20. + $offset = \is_string($der) ? \strpos($der, "\x04\x22\x04\x20") : false; + + if ($offset === false || \strlen($der) < $offset + 4 + SODIUM_CRYPTO_SIGN_SEEDBYTES) { + throw new Exception('Private key is not a PKCS#8 Ed25519 key'); + } + + $seed = \substr($der, $offset + 4, SODIUM_CRYPTO_SIGN_SEEDBYTES); + + return \sodium_crypto_sign_secretkey(\sodium_crypto_sign_seed_keypair($seed)); + } + + protected function base64UrlEncode(string $data): string + { + return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '='); + } + + protected function base64UrlDecode(string $data): string + { + $remainder = \strlen($data) % 4; + if ($remainder > 0) { + $data .= \str_repeat('=', 4 - $remainder); + } + + return (string)\base64_decode(\strtr($data, '-_', '+/'), true); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php index 62fdd2ee28..cb519c1ace 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\Codebase\Authorize; +use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -71,12 +72,8 @@ class Get extends Action throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Codebase client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); } - // Codebase ignores a redirect parameter on the install URL; the - // callback URL is taken from the app's own configuration on Cursor. - $url = 'https://cursor.com/codebase/apps/install?' . \http_build_query([ - 'client_id' => $clientId, - 'source' => 'app-metadata', - ]); + $oauth2 = new OAuth2Cursor($clientId, System::getEnv('_APP_VCS_CODEBASE_PRIVATE_KEY', ''), ''); + $url = $oauth2->getLoginURL(); // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. Console::log('[CODEBASE DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php index f1d23c5d43..023ba6f369 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\Codebase\Callback; +use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Utopia\Request; @@ -11,7 +12,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Query; -use Utopia\Fetch\Client as FetchClient; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -22,18 +22,6 @@ class Get extends Action use HTTP; use AppwritePermission; - /** - * Issuer and JWKS endpoint published by Cursor's OIDC discovery document at - * https://api.cursor.com/v1/origin/.well-known/openid-configuration - */ - private const RECEIPT_ISSUER = 'https://api.cursor.com/v1/origin'; - private const RECEIPT_JWKS_URL = 'https://api.cursor.com/v1/origin/keys'; - - /** - * Clock-skew tolerance, in seconds, applied to receipt exp/nbf checks. - */ - private const RECEIPT_LEEWAY = 60; - public static function getName() { return 'getVCSCodebaseCallback'; @@ -154,8 +142,21 @@ class Get extends Action // against Cursor's published JWKS. This proves the request genuinely // originates from Codebase (the callback is public and has no token // exchange) before any installation is created. + $oauth2 = new OAuth2Cursor( + System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID', ''), + System::getEnv('_APP_VCS_CODEBASE_PRIVATE_KEY', ''), + '' + ); + try { - $receiptClaims = $this->verifyReceipt($installationReceipt, $providerInstallationId); + $receiptClaims = $oauth2->verifyReceipt($installationReceipt); + + // Bind the receipt to the installation id in the callback so a + // valid receipt cannot be replayed to attach a different + // installation. + if (($receiptClaims['sub'] ?? '') !== $providerInstallationId) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Receipt subject does not match installation ID'); + } } catch (\Throwable $e) { Console::log('[CODEBASE DEBUG] Receipt verification failed: ' . $e->getMessage()); $this->failure($response, $redirectFailure, 'Could not verify the Codebase installation receipt.'); @@ -225,107 +226,4 @@ class Get extends Action ->redirect($redirect . $separator . \http_build_query(['error' => $error])); } - /** - * Verify a Codebase installation receipt (an EdDSA-signed JWT) against - * Cursor's published JWKS and validate its claims. Returns the verified - * claims, or throws on any failure. - * - * @return array - */ - private function verifyReceipt(string $jwt, string $expectedInstallationId): array - { - if (empty($jwt)) { - throw new \Exception('receipt is missing'); - } - - $segments = \explode('.', $jwt); - if (\count($segments) !== 3) { - throw new \Exception('receipt is not a JWT'); - } - - [$headerB64, $payloadB64, $signatureB64] = $segments; - $header = \json_decode($this->base64UrlDecode($headerB64), true); - $claims = \json_decode($this->base64UrlDecode($payloadB64), true); - $signature = $this->base64UrlDecode($signatureB64); - - if (!\is_array($header) || !\is_array($claims)) { - throw new \Exception('receipt segments are not valid JSON'); - } - - if (($header['alg'] ?? '') !== 'EdDSA') { - throw new \Exception('unexpected signing algorithm'); - } - - $kid = $header['kid'] ?? ''; - if (empty($kid)) { - throw new \Exception('receipt is missing key id'); - } - - $publicKey = null; - foreach ($this->fetchJwks() as $key) { - if (($key['kid'] ?? '') === $kid && ($key['kty'] ?? '') === 'OKP' && ($key['crv'] ?? '') === 'Ed25519') { - $publicKey = $this->base64UrlDecode($key['x'] ?? ''); - break; - } - } - - if ($publicKey === null || \strlen($publicKey) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { - throw new \Exception('no matching signing key in JWKS'); - } - - if (!\sodium_crypto_sign_verify_detached($signature, $headerB64 . '.' . $payloadB64, $publicKey)) { - throw new \Exception('signature is invalid'); - } - - if (($claims['iss'] ?? '') !== self::RECEIPT_ISSUER) { - throw new \Exception('unexpected issuer'); - } - - $clientId = System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID', ''); - if (($claims['aud'] ?? '') !== $clientId) { - throw new \Exception('receipt audience does not match this app'); - } - - // Bind the receipt to the installation id in the callback so a valid - // receipt cannot be replayed to attach a different installation. - if (($claims['sub'] ?? '') !== $expectedInstallationId) { - throw new \Exception('receipt subject does not match installation id'); - } - - $now = \time(); - if (isset($claims['exp']) && $now >= (int)$claims['exp'] + self::RECEIPT_LEEWAY) { - throw new \Exception('receipt has expired'); - } - if (isset($claims['nbf']) && $now < (int)$claims['nbf'] - self::RECEIPT_LEEWAY) { - throw new \Exception('receipt is not yet valid'); - } - - return $claims; - } - - /** - * Fetch Cursor's origin JWKS (the Ed25519 public keys that sign receipts). - * - * @return array> - */ - private function fetchJwks(): array - { - $client = new FetchClient(); - $client->addHeader('Accept', 'application/json'); - - $response = $client->fetch(url: self::RECEIPT_JWKS_URL, method: FetchClient::METHOD_GET); - $body = \json_decode($response->getBody(), true); - - return \is_array($body) && isset($body['keys']) && \is_array($body['keys']) ? $body['keys'] : []; - } - - private function base64UrlDecode(string $data): string - { - $remainder = \strlen($data) % 4; - if ($remainder > 0) { - $data .= \str_repeat('=', 4 - $remainder); - } - - return (string)\base64_decode(\strtr($data, '-_', '+/'), true); - } } From b08e71a6a0a4564bbe4883836f8000bd7d271db5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:41:45 +0200 Subject: [PATCH 09/38] Prepare empty env vars --- .env | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.env b/.env index e1032c8a8d..49be7302b3 100644 --- a/.env +++ b/.env @@ -170,5 +170,7 @@ _APP_VCS_GITLAB_WEBHOOK_SECRET= _APP_VCS_BITBUCKET_CLIENT_ID= _APP_VCS_BITBUCKET_CLIENT_SECRET= _APP_VCS_BITBUCKET_WEBHOOK_SECRET= +_APP_VCS_CODEBASE_CLIENT_ID= +_APP_VCS_CODEBASE_PRIVATE_KEY="" _APP_VCS_WEBHOOK_URL=http://appwrite/v1 _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 From 4ccc6e66641ff36c344674c149ea82ee8f85852b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 10:43:16 +0200 Subject: [PATCH 10/38] Suport cursor oauth2 on project-level --- app/config/oAuthProviders.php | 11 +++ app/init/models.php | 2 + src/Appwrite/Auth/OAuth2/Cursor.php | 15 +++++ .../Project/Http/Project/OAuth2/Base.php | 1 + .../Http/Project/OAuth2/Cursor/Update.php | 65 ++++++++++++++++++ .../Modules/Project/Services/Http.php | 2 + src/Appwrite/Utopia/Response.php | 1 + .../Utopia/Response/Model/OAuth2Cursor.php | 67 +++++++++++++++++++ .../Project/OAuth2/OAuth2ProviderTest.php | 2 +- 9 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php create mode 100644 src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php diff --git a/app/config/oAuthProviders.php b/app/config/oAuthProviders.php index 31f1585b5c..113a088032 100644 --- a/app/config/oAuthProviders.php +++ b/app/config/oAuthProviders.php @@ -101,6 +101,17 @@ return [ 'mock' => false, 'class' => 'Appwrite\\Auth\\OAuth2\\Box', ], + 'cursor' => [ + 'name' => 'Cursor', + 'developers' => 'https://cursor.com/docs/api', + 'icon' => 'icon-cursor', + 'enabled' => true, + 'sandbox' => false, + 'form' => false, + 'beta' => false, + 'mock' => false, + 'class' => 'Appwrite\\Auth\\OAuth2\\Cursor', + ], 'dailymotion' => [ 'name' => 'Dailymotion', 'developers' => 'https://developers.dailymotion.com/api/', diff --git a/app/init/models.php b/app/init/models.php index 41651c9262..6f257f5f4b 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -126,6 +126,7 @@ use Appwrite\Utopia\Response\Model\OAuth2Autodesk; use Appwrite\Utopia\Response\Model\OAuth2Bitbucket; use Appwrite\Utopia\Response\Model\OAuth2Bitly; use Appwrite\Utopia\Response\Model\OAuth2Box; +use Appwrite\Utopia\Response\Model\OAuth2Cursor; use Appwrite\Utopia\Response\Model\OAuth2Dailymotion; use Appwrite\Utopia\Response\Model\OAuth2Discord; use Appwrite\Utopia\Response\Model\OAuth2Disqus; @@ -463,6 +464,7 @@ Response::setModel(new OAuth2Okta()); Response::setModel(new OAuth2Kick()); Response::setModel(new OAuth2Apple()); Response::setModel(new OAuth2Microsoft()); +Response::setModel(new OAuth2Cursor()); Response::setModel(new OAuth2ProviderList()); Response::setModel(new PolicyPasswordDictionary()); Response::setModel(new PolicyPasswordHistory()); diff --git a/src/Appwrite/Auth/OAuth2/Cursor.php b/src/Appwrite/Auth/OAuth2/Cursor.php index 4babadf334..2f3149c01a 100644 --- a/src/Appwrite/Auth/OAuth2/Cursor.php +++ b/src/Appwrite/Auth/OAuth2/Cursor.php @@ -217,6 +217,21 @@ class Cursor extends OAuth2 return $this->getTokens($refreshToken); } + /** + * End-to-end credential check used when enabling the provider: signing + * the app JWT proves the private key is well-formed, and the zero-cost + * rate-limit endpoint rejects it with a 401 unless the app ID and key + * match a registered app. + * + * @throws Exception + */ + public function verifyCredentials(): void + { + $this->request('GET', $this->endpoint . '/rate_limit', [ + 'Authorization: Bearer ' . $this->getAppJWT(), + ]); + } + /** * Cursor Origin apps act as an installation, never as a user, so there is * no user identity behind an access token. diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php index cb58c1cfc0..0739e7a275 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -319,6 +319,7 @@ abstract class Base extends Action 'kick' => Kick\Update::class, 'apple' => Apple\Update::class, 'microsoft' => Microsoft\Update::class, + 'cursor' => Cursor\Update::class, ]; } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php new file mode 100644 index 0000000000..c11cd8221e --- /dev/null +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php @@ -0,0 +1,65 @@ +addAction(UpdateOAuth2Kick::getName(), new UpdateOAuth2Kick()); $this->addAction(UpdateOAuth2Apple::getName(), new UpdateOAuth2Apple()); $this->addAction(UpdateOAuth2Microsoft::getName(), new UpdateOAuth2Microsoft()); + $this->addAction(UpdateOAuth2Cursor::getName(), new UpdateOAuth2Cursor()); } } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 80dd69bd38..c1270b5976 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -327,6 +327,7 @@ class Response extends SwooleResponse public const MODEL_OAUTH2_OKTA = 'oAuth2Okta'; public const MODEL_OAUTH2_KICK = 'oAuth2Kick'; public const MODEL_OAUTH2_MICROSOFT = 'oAuth2Microsoft'; + public const MODEL_OAUTH2_CURSOR = 'oAuth2Cursor'; public const MODEL_OAUTH2_PROVIDER_LIST = 'oAuth2ProviderList'; // Health diff --git a/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php b/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php new file mode 100644 index 0000000000..7f7f7956d1 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php @@ -0,0 +1,67 @@ + 'cursor', + ]; + + public function getProviderLabel(): string + { + return 'Cursor'; + } + + public function getClientIdExample(): string + { + return 'app_01k5wz9v3rq8tembsy0d4hxnpc'; + } + + public function getClientSecretExample(): string + { + return '-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n-----END PRIVATE KEY-----'; + } + + public function getClientIdLabel(): string + { + return 'app ID'; + } + + public function getClientSecretFieldName(): string + { + return 'privateKey'; + } + + public function getClientSecretLabel(): string + { + return 'private key'; + } + + public function getClientSecretDescription(): string + { + return parent::getClientSecretDescription() . ' Ed25519 key in PKCS#8 PEM format; only its public key is registered with Cursor.'; + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'OAuth2Cursor'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_OAUTH2_CURSOR; + } +} diff --git a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php index 63c1b1012f..b133346585 100644 --- a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php +++ b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php @@ -38,7 +38,7 @@ final class OAuth2ProviderTest extends TestCase $expected = [ 'amazon', 'apple', 'appwrite', 'auth0', 'authentik', 'autodesk', - 'bitbucket', 'bitly', 'box', 'dailymotion', 'discord', 'disqus', + 'bitbucket', 'bitly', 'box', 'cursor', 'dailymotion', 'discord', 'disqus', 'dropbox', 'etsy', 'facebook', 'figma', 'fusionauth', 'github', 'gitlab', 'google', 'keycloak', 'kick', 'linkedin', 'microsoft', 'notion', 'oidc', 'okta', 'paypal', 'paypalSandbox', 'podio', From 5716cd220f2cd42fed85f9fa6d857d929c091c7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 12:34:27 +0200 Subject: [PATCH 11/38] support Cursor for Console --- app/config/console.php | 5 ++++- docker-compose.yml | 2 ++ .../Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/config/console.php b/app/config/console.php index bf1850182a..9e6fd7e35b 100644 --- a/app/config/console.php +++ b/app/config/console.php @@ -57,7 +57,10 @@ $console = [ 'oAuthProviders' => [ 'githubEnabled' => true, 'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''), - 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', '') + 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', ''), + 'cursorEnabled' => true, + 'cursorSecret' => System::getEnv('_APP_CONSOLE_CURSOR_PRIVATE_KEY', ''), + 'cursorAppid' => System::getEnv('_APP_CONSOLE_CURSOR_APP_ID', ''), ], 'smtpBaseTemplate' => APP_BRANDED_EMAIL_BASE_TEMPLATE, ]; diff --git a/docker-compose.yml b/docker-compose.yml index 1372bca12a..b6e8fa0118 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -217,6 +217,8 @@ services: - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET - _APP_ASSISTANT_OPENAI_API_KEY - _APP_CONSOLE_COUNTRIES_DENYLIST + - _APP_CONSOLE_CURSOR_APP_ID + - _APP_CONSOLE_CURSOR_PRIVATE_KEY - _APP_EXPERIMENT_LOGGING_PROVIDER - _APP_EXPERIMENT_LOGGING_CONFIG - _APP_DATABASE_SHARED_TABLES diff --git a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php index b133346585..1fefb7c983 100644 --- a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php +++ b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php @@ -135,6 +135,7 @@ final class OAuth2ProviderTest extends TestCase yield 'dailymotion' => ['dailymotion', $actions['dailymotion'], 'apiKey', 'apiSecret']; yield 'bitbucket' => ['bitbucket', $actions['bitbucket'], 'key', 'secret']; yield 'dropbox' => ['dropbox', DropboxUpdate::class, 'appKey', 'appSecret']; + yield 'cursor' => ['cursor', $actions['cursor'], 'clientId', 'privateKey']; yield 'etsy' => ['etsy', $actions['etsy'], 'keyString', 'sharedSecret']; yield 'facebook' => ['facebook', $actions['facebook'], 'appId', 'appSecret']; yield 'notion' => ['notion', $actions['notion'], 'oauthClientId', 'oauthClientSecret']; From 394ff720bcd08b5a7e6f4660b2890f637b6bdac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 13:32:19 +0200 Subject: [PATCH 12/38] Cleanup --- app/config/console.php | 3 - app/config/oAuthProviders.php | 11 --- app/config/variables.php | 8 +- app/init/models.php | 2 - docker-compose.yml | 8 +- src/Appwrite/Auth/OAuth2/Cursor.php | 35 ++++---- .../Project/Http/Project/OAuth2/Base.php | 1 - .../Http/Project/OAuth2/Cursor/Update.php | 65 --------------- .../Modules/Project/Services/Http.php | 2 - .../VCS/Http/Codebase/Events/Create.php | 54 ------------ .../{Codebase => Origin}/Authorize/Get.php | 25 +++--- .../{Codebase => Origin}/Callback/Get.php | 83 ++++++++++--------- .../Modules/VCS/Http/Origin/Events/Create.php | 58 +++++++++++++ .../Platform/Modules/VCS/Services/Http.php | 14 ++-- src/Appwrite/Utopia/Response.php | 1 - .../Utopia/Response/Model/OAuth2Cursor.php | 67 --------------- .../Project/OAuth2/OAuth2ProviderTest.php | 3 +- 17 files changed, 145 insertions(+), 295 deletions(-) delete mode 100644 src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php delete mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php rename src/Appwrite/Platform/Modules/VCS/Http/{Codebase => Origin}/Authorize/Get.php (66%) rename src/Appwrite/Platform/Modules/VCS/Http/{Codebase => Origin}/Callback/Get.php (67%) create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php delete mode 100644 src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php diff --git a/app/config/console.php b/app/config/console.php index 9e6fd7e35b..8a741030b6 100644 --- a/app/config/console.php +++ b/app/config/console.php @@ -58,9 +58,6 @@ $console = [ 'githubEnabled' => true, 'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''), 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', ''), - 'cursorEnabled' => true, - 'cursorSecret' => System::getEnv('_APP_CONSOLE_CURSOR_PRIVATE_KEY', ''), - 'cursorAppid' => System::getEnv('_APP_CONSOLE_CURSOR_APP_ID', ''), ], 'smtpBaseTemplate' => APP_BRANDED_EMAIL_BASE_TEMPLATE, ]; diff --git a/app/config/oAuthProviders.php b/app/config/oAuthProviders.php index 113a088032..31f1585b5c 100644 --- a/app/config/oAuthProviders.php +++ b/app/config/oAuthProviders.php @@ -101,17 +101,6 @@ return [ 'mock' => false, 'class' => 'Appwrite\\Auth\\OAuth2\\Box', ], - 'cursor' => [ - 'name' => 'Cursor', - 'developers' => 'https://cursor.com/docs/api', - 'icon' => 'icon-cursor', - 'enabled' => true, - 'sandbox' => false, - 'form' => false, - 'beta' => false, - 'mock' => false, - 'class' => 'Appwrite\\Auth\\OAuth2\\Cursor', - ], 'dailymotion' => [ 'name' => 'Dailymotion', 'developers' => 'https://developers.dailymotion.com/api/', diff --git a/app/config/variables.php b/app/config/variables.php index 346f9bcc59..443dc19f66 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -1393,8 +1393,8 @@ return [ 'filter' => '' ], [ - 'name' => '_APP_VCS_CODEBASE_CLIENT_ID', - 'description' => 'Codebase app client ID. You can find it in your Codebase app details on Cursor.', + 'name' => '_APP_VCS_ORIGIN_CLIENT_ID', + 'description' => 'Origin app client ID. You can find it in your Origin app details on Cursor.', 'introduction' => '2.0.0', 'default' => '', 'required' => false, @@ -1402,8 +1402,8 @@ return [ 'filter' => '' ], [ - 'name' => '_APP_VCS_CODEBASE_PRIVATE_KEY', - 'description' => 'Codebase app private key. You can generate private keys from your Codebase app settings on Cursor.', + 'name' => '_APP_VCS_ORIGIN_PRIVATE_KEY', + 'description' => 'Origin app private key. You can generate private keys from your Origin app settings on Cursor.', 'introduction' => '2.0.0', 'default' => '', 'required' => false, diff --git a/app/init/models.php b/app/init/models.php index 6f257f5f4b..41651c9262 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -126,7 +126,6 @@ use Appwrite\Utopia\Response\Model\OAuth2Autodesk; use Appwrite\Utopia\Response\Model\OAuth2Bitbucket; use Appwrite\Utopia\Response\Model\OAuth2Bitly; use Appwrite\Utopia\Response\Model\OAuth2Box; -use Appwrite\Utopia\Response\Model\OAuth2Cursor; use Appwrite\Utopia\Response\Model\OAuth2Dailymotion; use Appwrite\Utopia\Response\Model\OAuth2Discord; use Appwrite\Utopia\Response\Model\OAuth2Disqus; @@ -464,7 +463,6 @@ Response::setModel(new OAuth2Okta()); Response::setModel(new OAuth2Kick()); Response::setModel(new OAuth2Apple()); Response::setModel(new OAuth2Microsoft()); -Response::setModel(new OAuth2Cursor()); Response::setModel(new OAuth2ProviderList()); Response::setModel(new PolicyPasswordDictionary()); Response::setModel(new PolicyPasswordHistory()); diff --git a/docker-compose.yml b/docker-compose.yml index b6e8fa0118..64f16a1347 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -210,15 +210,13 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET - - _APP_VCS_CODEBASE_CLIENT_ID - - _APP_VCS_CODEBASE_PRIVATE_KEY + - _APP_VCS_ORIGIN_CLIENT_ID + - _APP_VCS_ORIGIN_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_MIGRATIONS_FIREBASE_CLIENT_ID - _APP_MIGRATIONS_FIREBASE_CLIENT_SECRET - _APP_ASSISTANT_OPENAI_API_KEY - _APP_CONSOLE_COUNTRIES_DENYLIST - - _APP_CONSOLE_CURSOR_APP_ID - - _APP_CONSOLE_CURSOR_PRIVATE_KEY - _APP_EXPERIMENT_LOGGING_PROVIDER - _APP_EXPERIMENT_LOGGING_CONFIG - _APP_DATABASE_SHARED_TABLES @@ -516,7 +514,7 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET - - _APP_VCS_CODEBASE_PRIVATE_KEY + - _APP_VCS_ORIGIN_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_FUNCTIONS_TIMEOUT - _APP_SITES_TIMEOUT diff --git a/src/Appwrite/Auth/OAuth2/Cursor.php b/src/Appwrite/Auth/OAuth2/Cursor.php index 2f3149c01a..f1186ba57c 100644 --- a/src/Appwrite/Auth/OAuth2/Cursor.php +++ b/src/Appwrite/Auth/OAuth2/Cursor.php @@ -11,7 +11,12 @@ use Appwrite\Auth\OAuth2; // Cursor Origin apps are not a user-login provider: installs are approved on // cursor.com and confirmed with an EdDSA-signed installation receipt instead // of an authorization code, and API access tokens are minted from an app JWT -// signed with the app's Ed25519 private key ($appSecret holds the PKCS#8 PEM). +// signed with the app's Ed25519 private key. +// +// Like Apple, the secret is a JSON object: `{"privateKey": "..."}` with the +// PKCS#8 PEM. The private key is only needed to mint tokens -- an empty +// object `{}` is valid configuration for starting an install flow, which +// only requires the app ID. class Cursor extends OAuth2 { /** @@ -57,6 +62,7 @@ class Cursor extends OAuth2 $params['scope'] = \implode(' ', $this->getScopes()); } + // The Origin console still lives under the /codebase path on cursor.com. return 'https://cursor.com/codebase/apps/install?' . \http_build_query($params); } @@ -217,21 +223,6 @@ class Cursor extends OAuth2 return $this->getTokens($refreshToken); } - /** - * End-to-end credential check used when enabling the provider: signing - * the app JWT proves the private key is well-formed, and the zero-cost - * rate-limit endpoint rejects it with a 401 unless the app ID and key - * match a registered app. - * - * @throws Exception - */ - public function verifyCredentials(): void - { - $this->request('GET', $this->endpoint . '/rate_limit', [ - 'Authorization: Bearer ' . $this->getAppJWT(), - ]); - } - /** * Cursor Origin apps act as an installation, never as a user, so there is * no user identity behind an access token. @@ -285,15 +276,23 @@ class Cursor extends OAuth2 } /** - * Ed25519 secret key derived from the PKCS#8 PEM in $appSecret. + * Ed25519 secret key derived from the PKCS#8 PEM in the secret's + * `privateKey` field. * * @throws Exception */ protected function getSigningKey(): string { + $secret = \json_decode($this->appSecret, true); + $pem = \is_array($secret) ? ($secret['privateKey'] ?? '') : ''; + + if (empty($pem) || !\is_string($pem)) { + throw new Exception('Private key is not configured'); + } + // Keys configured through environment variables carry literal \n // sequences instead of newlines. - $pem = \str_replace(['\n', '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----'], '', $this->appSecret); + $pem = \str_replace(['\n', '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----'], '', $pem); $der = \base64_decode(\preg_replace('/\s+/', '', $pem) ?? '', true); // RFC 8410 PKCS#8 wraps the 32-byte Ed25519 seed in a nested octet diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php index 0739e7a275..cb58c1cfc0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -319,7 +319,6 @@ abstract class Base extends Action 'kick' => Kick\Update::class, 'apple' => Apple\Update::class, 'microsoft' => Microsoft\Update::class, - 'cursor' => Cursor\Update::class, ]; } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php deleted file mode 100644 index c11cd8221e..0000000000 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Cursor/Update.php +++ /dev/null @@ -1,65 +0,0 @@ -addAction(UpdateOAuth2Kick::getName(), new UpdateOAuth2Kick()); $this->addAction(UpdateOAuth2Apple::getName(), new UpdateOAuth2Apple()); $this->addAction(UpdateOAuth2Microsoft::getName(), new UpdateOAuth2Microsoft()); - $this->addAction(UpdateOAuth2Cursor::getName(), new UpdateOAuth2Cursor()); } } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php deleted file mode 100644 index e44aae970d..0000000000 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Events/Create.php +++ /dev/null @@ -1,54 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) - ->setHttpPath('/v1/vcs/codebase/events') - ->desc('Create event') - ->groups(['api', 'vcs']) - ->label('scope', 'public') - ->inject('request') - ->inject('response') - ->callback($this->action(...)); - } - - public function action( - Request $request, - Response $response - ) { - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] Event received'); - Console::log('[CODEBASE DEBUG] Event headers: ' . \json_encode($request->getHeaders())); - Console::log('[CODEBASE DEBUG] Event query params: ' . \json_encode($request->getParams())); - Console::log('[CODEBASE DEBUG] Event raw payload: ' . $request->getRawPayload()); - - $payload = \json_decode($request->getRawPayload(), true) ?? []; - - $event = $request->getHeaderLine('x-codebase-event', '') ?: ($payload['event'] ?? ''); - Span::add('vcs.codebase.event.name', $event); - Console::log('[CODEBASE DEBUG] Event name resolved to: "' . $event . '"'); - - // Codebase does not deliver push or pull request events yet, so events - // are only acknowledged to prevent delivery retries. - $response->json(['success' => true]); - } -} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php similarity index 66% rename from src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php rename to src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php index cb519c1ace..804edd6e11 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/vcs/codebase/authorize') - ->desc('Create Codebase app installation') + ->setHttpPath('/v1/vcs/origin/authorize') + ->desc('Create Origin app installation') ->groups(['api', 'vcs']) ->label('scope', 'vcs.read') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->label('sdk', new Method( namespace: 'vcs', group: 'installations', - name: 'createCodebaseInstallation', - description: 'Begin Appwrite\'s Codebase app installation to set up version control integration. This endpoint responds with a redirect URL to the Codebase app\'s installation page on Cursor. The Codebase app must be configured in your environment for this endpoint to work.', + name: 'createOriginInstallation', + description: 'Begin Appwrite\'s Origin app installation to set up version control integration. This endpoint responds with a redirect URL to the Origin app\'s installation page on Cursor. The Origin app must be configured in your environment for this endpoint to work.', auth: [AuthType::ADMIN], responses: [ new SDKResponse( @@ -66,18 +66,19 @@ class Get extends Action Document $project, array $platform ) { - $clientId = System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID'); + $clientId = System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID'); if (empty($clientId)) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Codebase client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Origin client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); } - $oauth2 = new OAuth2Cursor($clientId, System::getEnv('_APP_VCS_CODEBASE_PRIVATE_KEY', ''), ''); + // The Cursor adapter expects its secret as a JSON object. + $oauth2 = new OAuth2Cursor($clientId, \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), ''); $url = $oauth2->getLoginURL(); - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); - Console::log('[CODEBASE DEBUG] Redirecting to install URL: ' . $url); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); + Console::log('[ORIGIN DEBUG] Redirecting to install URL: ' . $url); $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php similarity index 67% rename from src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php rename to src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php index 023ba6f369..437a47c0dd 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Codebase/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php @@ -1,6 +1,6 @@ setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/vcs/codebase/callback') - ->desc('Get installation from Codebase app') + ->setHttpPath('/v1/vcs/origin/callback') + ->desc('Get installation from Origin app') ->groups(['api', 'vcs']) ->label('scope', 'public') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') - ->param('installation_id', '', new Text(256, 0), 'Codebase installation ID', true) - ->param('installation_receipt', '', new Text(4096, 0), 'Codebase installation receipt JWT, signed by Cursor.', true) - ->param('state', '', new Text(2048), 'Codebase state. Contains info sent when starting the installation flow.', true) + ->param('installation_id', '', new Text(256, 0), 'Origin installation ID', true) + ->param('installation_receipt', '', new Text(4096, 0), 'Origin installation receipt JWT, signed by Cursor.', true) + ->param('state', '', new Text(2048), 'Origin state. Contains info sent when starting the installation flow.', true) ->inject('request') ->inject('response') ->inject('dbForPlatform') @@ -55,25 +55,25 @@ class Get extends Action Database $dbForPlatform, array $platform ) { - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. $params = $request->getParams(); - Console::log('[CODEBASE DEBUG] Callback received'); - Console::log('[CODEBASE DEBUG] Callback params: ' . \json_encode($params)); - Console::log('[CODEBASE DEBUG] Callback headers: ' . \json_encode($request->getHeaders())); - Console::log('[CODEBASE DEBUG] Callback installation_id: "' . $providerInstallationId . '"'); + Console::log('[ORIGIN DEBUG] Callback received'); + Console::log('[ORIGIN DEBUG] Callback params: ' . \json_encode($params)); + Console::log('[ORIGIN DEBUG] Callback headers: ' . \json_encode($request->getHeaders())); + Console::log('[ORIGIN DEBUG] Callback installation_id: "' . $providerInstallationId . '"'); // Surface anything that looks like an OAuth2 token flow (e.g. a code - // exchange), since Codebase is not expected to send one. + // exchange), since Origin is not expected to send one. $oauthParams = \array_intersect_key($params, \array_flip([ 'code', 'token', 'access_token', 'refresh_token', 'expires_in', 'token_type', 'scope', 'id_token', 'setup_action', 'authuser', 'session_state', ])); if (!empty($oauthParams)) { - Console::log('[CODEBASE DEBUG] Possible OAuth2 flow params detected: ' . \json_encode($oauthParams)); + Console::log('[ORIGIN DEBUG] Possible OAuth2 flow params detected: ' . \json_encode($oauthParams)); } else { - Console::log('[CODEBASE DEBUG] No OAuth2 flow params detected'); + Console::log('[ORIGIN DEBUG] No OAuth2 flow params detected'); } - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. // Decode only -- the EdDSA signature is not verified, so the claims // must not be trusted for authorization decisions yet. if (!empty($installationReceipt)) { @@ -81,13 +81,13 @@ class Get extends Action if (\count($segments) === 3) { $receiptHeader = \json_decode(\base64_decode(\strtr($segments[0], '-_', '+/')), true) ?? []; $receiptClaims = \json_decode(\base64_decode(\strtr($segments[1], '-_', '+/')), true) ?? []; - Console::log('[CODEBASE DEBUG] Receipt JWT header: ' . \json_encode($receiptHeader)); - Console::log('[CODEBASE DEBUG] Receipt JWT claims: ' . \json_encode($receiptClaims)); + Console::log('[ORIGIN DEBUG] Receipt JWT header: ' . \json_encode($receiptHeader)); + Console::log('[ORIGIN DEBUG] Receipt JWT claims: ' . \json_encode($receiptClaims)); } else { - Console::log('[CODEBASE DEBUG] installation_receipt is not a JWT: ' . $installationReceipt); + Console::log('[ORIGIN DEBUG] installation_receipt is not a JWT: ' . $installationReceipt); } } else { - Console::log('[CODEBASE DEBUG] No installation_receipt received'); + Console::log('[ORIGIN DEBUG] No installation_receipt received'); } if (empty($state)) { @@ -98,20 +98,20 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] Decoded state: ' . \json_encode($state)); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] Decoded state: ' . \json_encode($state)); - // This endpoint is public and Codebase performs no token exchange -- + // This endpoint is public and Origin performs no token exchange -- // 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'] ?? '')) { - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] State signature mismatch, rejecting callback'); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] State signature mismatch, rejecting callback'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } - Console::log('[CODEBASE DEBUG] State signature valid'); + Console::log('[ORIGIN DEBUG] State signature valid'); $project = $dbForPlatform->getDocument('projects', $projectId); @@ -134,17 +134,18 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; if (empty($providerInstallationId)) { - $this->failure($response, $redirectFailure, 'Codebase installation ID is missing.'); + $this->failure($response, $redirectFailure, 'Origin installation ID is missing.'); return; } // Authenticate the callback by verifying the installation receipt // against Cursor's published JWKS. This proves the request genuinely - // originates from Codebase (the callback is public and has no token + // originates from Origin (the callback is public and has no token // exchange) before any installation is created. + // The Cursor adapter expects its secret as a JSON object. $oauth2 = new OAuth2Cursor( - System::getEnv('_APP_VCS_CODEBASE_CLIENT_ID', ''), - System::getEnv('_APP_VCS_CODEBASE_PRIVATE_KEY', ''), + System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), + \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), '' ); @@ -158,21 +159,21 @@ class Get extends Action throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Receipt subject does not match installation ID'); } } catch (\Throwable $e) { - Console::log('[CODEBASE DEBUG] Receipt verification failed: ' . $e->getMessage()); - $this->failure($response, $redirectFailure, 'Could not verify the Codebase installation receipt.'); + Console::log('[ORIGIN DEBUG] Receipt verification failed: ' . $e->getMessage()); + $this->failure($response, $redirectFailure, 'Could not verify the Origin installation receipt.'); return; } // The Cursor namespace (team/workspace) owning the installation. $organization = $receiptClaims['namespace_id'] ?? ''; - Console::log('[CODEBASE DEBUG] Receipt verified, namespace_id: "' . $organization . '"'); + Console::log('[ORIGIN DEBUG] Receipt verified, namespace_id: "' . $organization . '"'); $projectInternalId = $project->getSequence(); $installation = $dbForPlatform->findOne('installations', [ Query::equal('providerInstallationId', [$providerInstallationId]), Query::equal('projectInternalId', [$projectInternalId]), - Query::equal('provider', ['codebase']), + Query::equal('provider', ['origin']), ]); if ($installation->isEmpty()) { @@ -184,22 +185,22 @@ class Get extends Action 'providerInstallationId' => $providerInstallationId, 'projectId' => $projectId, 'projectInternalId' => $projectInternalId, - 'provider' => 'codebase', + 'provider' => 'origin', 'organization' => $organization, 'personal' => false, ])); - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] Created installation "' . $installation->getId() . '" for project "' . $projectId . '"'); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] Created installation "' . $installation->getId() . '" for project "' . $projectId . '"'); } else { $installation = $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ 'organization' => $organization, ])); - Console::log('[CODEBASE DEBUG] Installation already exists: "' . $installation->getId() . '"'); + Console::log('[ORIGIN DEBUG] Installation already exists: "' . $installation->getId() . '"'); } - Console::log('[CODEBASE DEBUG] Redirecting to success URL: ' . $redirectSuccess); + Console::log('[ORIGIN DEBUG] Redirecting to success URL: ' . $redirectSuccess); $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') @@ -212,8 +213,8 @@ class Get extends Action */ private function failure(Response $response, string $redirect, string $error, string $type = Exception::GENERAL_ARGUMENT_INVALID): void { - // TODO: Temporary debug logging while the Codebase integration is verified -- remove afterwards. - Console::log('[CODEBASE DEBUG] Callback failed: ' . $error . ' (redirect: "' . $redirect . '")'); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] Callback failed: ' . $error . ' (redirect: "' . $redirect . '")'); if (empty($redirect)) { throw new Exception($type, $error); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php new file mode 100644 index 0000000000..aac94ffa02 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -0,0 +1,58 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_POST) + ->setHttpPath('/v1/vcs/origin/events') + ->desc('Create event') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->inject('request') + ->inject('response') + ->callback($this->action(...)); + } + + public function action( + Request $request, + Response $response + ) { + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN DEBUG] Event received'); + Console::log('[ORIGIN DEBUG] Event headers: ' . \json_encode($request->getHeaders())); + Console::log('[ORIGIN DEBUG] Event query params: ' . \json_encode($request->getParams())); + Console::log('[ORIGIN DEBUG] Event raw payload: ' . $request->getRawPayload()); + + $payload = \json_decode($request->getRawPayload(), true) ?? []; + + // The delivery header is not confirmed yet -- accept both the Origin + // name and the pre-rename Codebase one. + $event = $request->getHeaderLine('x-origin-event', '') + ?: $request->getHeaderLine('x-codebase-event', '') + ?: ($payload['event'] ?? ''); + Span::add('vcs.origin.event.name', $event); + Console::log('[ORIGIN DEBUG] Event name resolved to: "' . $event . '"'); + + // Origin does not deliver push or pull request events yet, so events + // are only acknowledged to prevent delivery retries. + $response->json(['success' => true]); + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index 13a8e5bb74..71498db484 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -5,9 +5,6 @@ namespace Appwrite\Platform\Modules\VCS\Services; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Authorize\Get as GetBitbucketAuthorize; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Callback\Get as GetBitbucketCallback; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Events\Create as CreateBitbucketEvent; -use Appwrite\Platform\Modules\VCS\Http\Codebase\Authorize\Get as GetCodebaseAuthorize; -use Appwrite\Platform\Modules\VCS\Http\Codebase\Callback\Get as GetCodebaseCallback; -use Appwrite\Platform\Modules\VCS\Http\Codebase\Events\Create as CreateCodebaseEvent; use Appwrite\Platform\Modules\VCS\Http\Gitea\Authorize\Get as GetGiteaAuthorize; use Appwrite\Platform\Modules\VCS\Http\Gitea\Callback\Get as GetGiteaCallback; use Appwrite\Platform\Modules\VCS\Http\Gitea\Events\Create as CreateGiteaEvent; @@ -28,6 +25,9 @@ use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Detections\Cre use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\Get as GetRepository; use Appwrite\Platform\Modules\VCS\Http\Installations\Repositories\XList as ListRepositories; use Appwrite\Platform\Modules\VCS\Http\Installations\XList as ListInstallations; +use Appwrite\Platform\Modules\VCS\Http\Origin\Authorize\Get as GetOriginAuthorize; +use Appwrite\Platform\Modules\VCS\Http\Origin\Callback\Get as GetOriginCallback; +use Appwrite\Platform\Modules\VCS\Http\Origin\Events\Create as CreateOriginEvent; use Utopia\Platform\Service; class Http extends Service @@ -53,9 +53,9 @@ class Http extends Service $this->addAction(GetBitbucketAuthorize::getName(), new GetBitbucketAuthorize()); $this->addAction(GetBitbucketCallback::getName(), new GetBitbucketCallback()); - // Codebase Authorization & Callback - $this->addAction(GetCodebaseAuthorize::getName(), new GetCodebaseAuthorize()); - $this->addAction(GetCodebaseCallback::getName(), new GetCodebaseCallback()); + // Origin Authorization & Callback + $this->addAction(GetOriginAuthorize::getName(), new GetOriginAuthorize()); + $this->addAction(GetOriginCallback::getName(), new GetOriginCallback()); // Installations $this->addAction(GetInstallation::getName(), new GetInstallation()); @@ -76,6 +76,6 @@ class Http extends Service $this->addAction(CreateGiteaEvent::getName(), new CreateGiteaEvent()); $this->addAction(CreateGitlabEvent::getName(), new CreateGitlabEvent()); $this->addAction(CreateBitbucketEvent::getName(), new CreateBitbucketEvent()); - $this->addAction(CreateCodebaseEvent::getName(), new CreateCodebaseEvent()); + $this->addAction(CreateOriginEvent::getName(), new CreateOriginEvent()); } } diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index c1270b5976..80dd69bd38 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -327,7 +327,6 @@ class Response extends SwooleResponse public const MODEL_OAUTH2_OKTA = 'oAuth2Okta'; public const MODEL_OAUTH2_KICK = 'oAuth2Kick'; public const MODEL_OAUTH2_MICROSOFT = 'oAuth2Microsoft'; - public const MODEL_OAUTH2_CURSOR = 'oAuth2Cursor'; public const MODEL_OAUTH2_PROVIDER_LIST = 'oAuth2ProviderList'; // Health diff --git a/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php b/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php deleted file mode 100644 index 7f7f7956d1..0000000000 --- a/src/Appwrite/Utopia/Response/Model/OAuth2Cursor.php +++ /dev/null @@ -1,67 +0,0 @@ - 'cursor', - ]; - - public function getProviderLabel(): string - { - return 'Cursor'; - } - - public function getClientIdExample(): string - { - return 'app_01k5wz9v3rq8tembsy0d4hxnpc'; - } - - public function getClientSecretExample(): string - { - return '-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n-----END PRIVATE KEY-----'; - } - - public function getClientIdLabel(): string - { - return 'app ID'; - } - - public function getClientSecretFieldName(): string - { - return 'privateKey'; - } - - public function getClientSecretLabel(): string - { - return 'private key'; - } - - public function getClientSecretDescription(): string - { - return parent::getClientSecretDescription() . ' Ed25519 key in PKCS#8 PEM format; only its public key is registered with Cursor.'; - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'OAuth2Cursor'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_OAUTH2_CURSOR; - } -} diff --git a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php index 1fefb7c983..63c1b1012f 100644 --- a/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php +++ b/tests/unit/Platform/Modules/Project/OAuth2/OAuth2ProviderTest.php @@ -38,7 +38,7 @@ final class OAuth2ProviderTest extends TestCase $expected = [ 'amazon', 'apple', 'appwrite', 'auth0', 'authentik', 'autodesk', - 'bitbucket', 'bitly', 'box', 'cursor', 'dailymotion', 'discord', 'disqus', + 'bitbucket', 'bitly', 'box', 'dailymotion', 'discord', 'disqus', 'dropbox', 'etsy', 'facebook', 'figma', 'fusionauth', 'github', 'gitlab', 'google', 'keycloak', 'kick', 'linkedin', 'microsoft', 'notion', 'oidc', 'okta', 'paypal', 'paypalSandbox', 'podio', @@ -135,7 +135,6 @@ final class OAuth2ProviderTest extends TestCase yield 'dailymotion' => ['dailymotion', $actions['dailymotion'], 'apiKey', 'apiSecret']; yield 'bitbucket' => ['bitbucket', $actions['bitbucket'], 'key', 'secret']; yield 'dropbox' => ['dropbox', DropboxUpdate::class, 'appKey', 'appSecret']; - yield 'cursor' => ['cursor', $actions['cursor'], 'clientId', 'privateKey']; yield 'etsy' => ['etsy', $actions['etsy'], 'keyString', 'sharedSecret']; yield 'facebook' => ['facebook', $actions['facebook'], 'appId', 'appSecret']; yield 'notion' => ['notion', $actions['notion'], 'oauthClientId', 'oauthClientSecret']; From b2b02b139271b1327ccc64841a399bb69a964a0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 14:38:35 +0200 Subject: [PATCH 13/38] feat: finish Origin (Cursor) VCS provider support Completes the Origin integration started on this branch: - Register 'origin' in the VCS provider registry, wiring the new utopia-php/vcs Origin adapter (dev-feat-origin) so installations, repository listing/creation, branches, contents, detections and namespaces work through the existing provider-agnostic endpoints - Authorize flow now sends HMAC-signed state and the registered callback to Cursor's install page, with the full scope list the integration needs - Callback verifies the installation receipt first, reads state from its claims (query parameter as fallback), binds the receipt subject to the installation, and resolves the owner slug for the installation's organization - Events endpoint verifies deliveries against Origin's published Ed25519 JWKS (with timestamp freshness and key-rotation refetch) and dispatches push, pull request and installation events into the same deployment pipeline as GitHub, including multi-ref pushes - Build statuses ride the adapter's check-run-backed commit statuses; PR comments work through the existing Comment flow unchanged - New GET /v1/vcs/archives endpoint packages a repository server-side behind an HMAC-signed expiring link, and Appwrite\Vcs\SourceArchive routes every deployment source fetch through it when a provider (Origin) offers no archive downloads Removes the temporary [ORIGIN DEBUG] logging. Co-Authored-By: Claude Fable 5 --- app/config/vcs.php | 22 ++ app/init/constants.php | 1 + composer.json | 2 +- composer.lock | 17 +- .../vcs/create-origin-installation.md | 1 + src/Appwrite/Auth/OAuth2/Cursor.php | 12 +- .../Platform/Modules/Compute/Base.php | 11 +- .../Http/Deployments/Duplicate/Create.php | 8 +- .../Modules/Functions/Workers/Builds.php | 6 +- .../Http/Deployments/Duplicate/Create.php | 6 +- .../Modules/VCS/Http/Archives/Get.php | 114 ++++++ .../Modules/VCS/Http/GitHub/Deployment.php | 6 +- .../Modules/VCS/Http/Origin/Authorize/Get.php | 59 ++- .../Modules/VCS/Http/Origin/Callback/Get.php | 131 +++---- .../Modules/VCS/Http/Origin/Events/Create.php | 360 +++++++++++++++++- .../Platform/Modules/VCS/Services/Http.php | 4 + src/Appwrite/Vcs/SourceArchive.php | 66 ++++ 17 files changed, 687 insertions(+), 139 deletions(-) create mode 100644 docs/references/vcs/create-origin-installation.md create mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php create mode 100644 src/Appwrite/Vcs/SourceArchive.php diff --git a/app/config/vcs.php b/app/config/vcs.php index d3a5dc8279..141b987b42 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -5,6 +5,7 @@ */ use Appwrite\Auth\OAuth2\Bitbucket as OAuth2Bitbucket; +use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; @@ -12,6 +13,7 @@ use Utopia\VCS\Adapter\Git\Bitbucket; use Utopia\VCS\Adapter\Git\Gitea; use Utopia\VCS\Adapter\Git\GitHub; use Utopia\VCS\Adapter\Git\GitLab; +use Utopia\VCS\Adapter\Git\Origin; return [ 'github' => [ @@ -62,6 +64,26 @@ return [ 'webhookSecret' => ['required' => true, 'envVariable' => '_APP_VCS_GITLAB_WEBHOOK_SECRET'], ], ], + 'origin' => [ + 'adapter' => Origin::class, + // Origin apps authenticate like GitHub Apps (an app-signed JWT minting + // installation tokens), so the same Ed25519 private key doubles as the + // OAuth2 secret. Auth\OAuth2\Cursor expects it JSON-wrapped, the way + // Apple does. + 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Cursor($clientId, \json_encode([ + 'privateKey' => $clientSecret, + ]), ''), + 'variables' => [ + 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_CLIENT_ID'], + // Factory::fromInstallation() reads appId + privateKey; the app id + // and OAuth2 client id are the same value on Origin. + 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_CLIENT_ID'], + 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_PRIVATE_KEY'], + 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_PRIVATE_KEY'], + // No webhookSecret: Origin signs deliveries with its own Ed25519 + // key, verified against its published JWKS instead of a shared secret. + ], + ], 'bitbucket' => [ 'adapter' => Bitbucket::class, 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Bitbucket($clientId, $clientSecret, ''), diff --git a/app/init/constants.php b/app/init/constants.php index 61d861936b..96ded3139b 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -114,6 +114,7 @@ const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite'; const APP_VCS_GITEA_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits const APP_VCS_GITLAB_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits const APP_VCS_BITBUCKET_EMAIL = 'team@appwrite.io'; // Used to detect Appwrite's own commits +const APP_VCS_ORIGIN_EMAIL = 'vcs@utopia.dev'; // Used to detect commits the VCS adapter pushes over Git HTTPS const APP_BRANDED_EMAIL_BASE_TEMPLATE = 'email-base-styled'; // Embeddings diff --git a/composer.json b/composer.json index 8e5b888dbe..a75d860459 100644 --- a/composer.json +++ b/composer.json @@ -99,7 +99,7 @@ "utopia-php/telemetry": "^0.4", "utopia-php/user-agent": "0.1.*", "mustangostang/spyc": "0.6.*", - "utopia-php/vcs": "5.*.*", + "utopia-php/vcs": "dev-feat-origin", "utopia-php/websocket": "1.0.*", "dragonmantank/cron-expression": "3.4.*", "chillerlan/php-qrcode": "4.3.*", diff --git a/composer.lock b/composer.lock index e21c79dec2..e0708c1fb9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c7acee2d0fe6b2299e47df05257a7225", + "content-hash": "f22cd38378c2891b4656c4c756478e6c", "packages": [ { "name": "adhocore/jwt", @@ -5031,16 +5031,16 @@ }, { "name": "utopia-php/vcs", - "version": "5.1.2", + "version": "dev-feat-origin", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "abdb5763221d7e4900174c280244f83638e3133e" + "reference": "cbf57939aa7f0d40c5e6ab9120e13b3247bd767a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/abdb5763221d7e4900174c280244f83638e3133e", - "reference": "abdb5763221d7e4900174c280244f83638e3133e", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/cbf57939aa7f0d40c5e6ab9120e13b3247bd767a", + "reference": "cbf57939aa7f0d40c5e6ab9120e13b3247bd767a", "shasum": "" }, "require": { @@ -5091,10 +5091,10 @@ "vcs" ], "support": { - "source": "https://github.com/utopia-php/vcs/tree/5.1.2", + "source": "https://github.com/utopia-php/vcs/tree/feat-origin", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-07T12:32:15+00:00" + "time": "2026-08-18T12:28:48+00:00" }, { "name": "utopia-php/websocket", @@ -8401,7 +8401,8 @@ "minimum-stability": "dev", "stability-flags": { "utopia-php/http": 5, - "utopia-php/platform": 5 + "utopia-php/platform": 5, + "utopia-php/vcs": 20 }, "prefer-stable": true, "prefer-lowest": false, diff --git a/docs/references/vcs/create-origin-installation.md b/docs/references/vcs/create-origin-installation.md new file mode 100644 index 0000000000..264763aae1 --- /dev/null +++ b/docs/references/vcs/create-origin-installation.md @@ -0,0 +1 @@ +Begin Appwrite's Origin app installation to set up version control integration. This endpoint responds with a redirect URL to the Origin app's installation page on Cursor. The Origin app must be configured in your environment for this endpoint to work. diff --git a/src/Appwrite/Auth/OAuth2/Cursor.php b/src/Appwrite/Auth/OAuth2/Cursor.php index f1186ba57c..53b2232ad3 100644 --- a/src/Appwrite/Auth/OAuth2/Cursor.php +++ b/src/Appwrite/Auth/OAuth2/Cursor.php @@ -47,8 +47,6 @@ class Cursor extends OAuth2 public function getLoginURL(): string { - // The install page ignores redirect_uri and state parameters -- the - // callback URL is taken from the app's own configuration on Cursor. $params = [ 'client_id' => $this->appID, ]; @@ -62,6 +60,16 @@ class Cursor extends OAuth2 $params['scope'] = \implode(' ', $this->getScopes()); } + // The callback must exactly match a redirect URI registered on the + // app; Cursor echoes state back as the receipt's `state` claim. + if (!empty($this->callback)) { + $params['redirect_uri'] = $this->callback; + } + + if (!empty($this->state)) { + $params['state'] = \json_encode($this->state); + } + // The Origin console still lives under the /codebase path on cursor.com. return 'https://cursor.com/codebase/apps/install?' . \http_build_query($params); } diff --git a/src/Appwrite/Platform/Modules/Compute/Base.php b/src/Appwrite/Platform/Modules/Compute/Base.php index 77fae4af98..90b39bd812 100644 --- a/src/Appwrite/Platform/Modules/Compute/Base.php +++ b/src/Appwrite/Platform/Modules/Compute/Base.php @@ -11,6 +11,7 @@ use Appwrite\Platform\Action; use Appwrite\Platform\Modules\Compute\Validator\Specification as SpecificationValidator; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\SourceArchive; use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; @@ -172,12 +173,13 @@ class Base extends Action // git write and then hands the build to the jobs-service itself. if ($template->isEmpty()) { $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($vcs, $installation->getAttribute('providerInstallationId', ''), $owner, $repositoryName, $ref, $platform); $deployment = $deployments->createFromUrl( $function, $deployment, - $vcs->getRepositoryPresignedUrl($owner, $repositoryName, $ref), + $sourceUrl, $function->getAttribute('providerRootDirectory', ''), - $vcs->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, ); } else { $deployment = $dbForProject->createDocument('deployments', new Document([ @@ -394,12 +396,13 @@ class Base extends Action // pushes go through the Builds worker (same split as redeployVcsFunction). if ($template->isEmpty()) { $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($vcs, $installation->getAttribute('providerInstallationId', ''), $owner, $repositoryName, $ref, $platform); $deployment = $deployments->createFromUrl( $site, $deployment, - $vcs->getRepositoryPresignedUrl($owner, $repositoryName, $ref), + $sourceUrl, $site->getAttribute('providerRootDirectory', ''), - $vcs->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, ); } else { $publisherForBuilds->enqueue(new BuildMessage( diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php index 55616a18b7..3e23e1f1d7 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Duplicate/Create.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\SourceArchive; use Utopia\Database\Database; use Utopia\Database\Helpers\ID; use Utopia\Database\Validator\UID; @@ -67,6 +68,7 @@ class Create extends Action ->inject('deployments') ->inject('deviceForFunctions') ->inject('vcsFactory') + ->inject('platform') ->callback($this->action(...)); } @@ -81,6 +83,7 @@ class Create extends Action Deployments $deployments, Device $deviceForFunctions, VcsFactory $vcsFactory, + array $platform, ) { $function = $dbForProject->getDocument('functions', $functionId); @@ -157,12 +160,13 @@ class Create extends Action $github = $vcsFactory->fromInstallation($installation); $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($github, $installation->getAttribute('providerInstallationId', ''), $owner, $repository, $ref, $platform); $deployment = $deployments->createFromUrl( $function, $deployment, - $github->getRepositoryPresignedUrl($owner, $repository, $ref), + $sourceUrl, $deployment->getAttribute('providerRootDirectory', ''), - $github->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, ); } else { // Public template repo: providerBranch holds the resolved ref, diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 776f0adae6..62c6290ad0 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -9,6 +9,7 @@ use Appwrite\Event\Realtime; use Appwrite\Usage\Build as BuildUsage; use Appwrite\Usage\Context; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\SourceArchive; use Exception; use Utopia\Config\Config; use Utopia\Console; @@ -401,12 +402,13 @@ class Builds extends Action // jobs-service like any other VCS commit, via the same Deployments // service the HTTP endpoints use. $ref = $deployment->getAttribute('providerCommitHash') ?: $branchName; + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($providerAdapter, $providerInstallationId, $cloneOwner, $cloneRepository, $ref, $platform); $deployments->createFromUrl( $resource, $deployment, - $providerAdapter->getRepositoryPresignedUrl($cloneOwner, $cloneRepository, $ref), + $sourceUrl, $resource->getAttribute('providerRootDirectory', ''), - $providerAdapter->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, ); Console::execute('rm -rf ' . \escapeshellarg('/tmp/builds/' . $deploymentId), '', $stdout, $stderr); diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php index f0d5755c24..2f2663db2e 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Duplicate/Create.php @@ -10,6 +10,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; +use Appwrite\Vcs\SourceArchive; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; @@ -178,12 +179,13 @@ class Create extends Action $vcs = $vcsFactory->fromInstallation($installation); $ref = $deployment->getAttribute('providerCommitHash') ?: $deployment->getAttribute('providerBranch'); + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($vcs, $installation->getAttribute('providerInstallationId', ''), $owner, $repository, $ref, $platform); $deployment = $deployments->createFromUrl( $site, $deployment, - $vcs->getRepositoryPresignedUrl($owner, $repository, $ref), + $sourceUrl, $deployment->getAttribute('providerRootDirectory', ''), - $vcs->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, ); } else { // Public template repo: providerBranch holds the resolved ref, diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php new file mode 100644 index 0000000000..4951fa1a67 --- /dev/null +++ b/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php @@ -0,0 +1,114 @@ +setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) + ->setHttpPath('/v1/vcs/archives') + ->desc('Download repository archive') + ->groups(['api', 'vcs']) + ->label('scope', 'public') + ->param('provider', '', new Text(64, 0), 'VCS provider the repository lives on.') + ->param('installation', '', new Text(256, 0), 'Provider installation ID the credentials belong to.') + ->param('owner', '', new Text(256, 0), 'Owner of the repository.') + ->param('repository', '', new Text(256, 0), 'Name of the repository.') + ->param('ref', '', new Text(256, 0), 'Branch, tag or commit to archive.') + ->param('expires', 0, new Integer(loose: true, bits: 64), 'Unix timestamp the link stops working at.') + ->param('signature', '', new Text(128, 0), 'HMAC binding every other parameter.') + ->inject('response') + ->inject('vcsFactory') + ->callback($this->action(...)); + } + + /** + * Packages a repository the provider cannot archive itself. The link is + * minted by Appwrite\Vcs\SourceArchive for the jobs-service source fetch, + * and the HMAC is the whole authorization: it commits to every parameter + * and an expiry, and only Appwrite can produce it. + */ + public function action( + string $provider, + string $installation, + string $owner, + string $repository, + string $ref, + int $expires, + string $signature, + Response $response, + VcsFactory $vcsFactory, + ) { + $expected = SourceArchive::signature($provider, $installation, $owner, $repository, $ref, $expires); + + if (empty($signature) || !\hash_equals($expected, $signature)) { + throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid archive signature.'); + } + + if ($expires < \time()) { + throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'The archive link has expired.'); + } + + $vcs = $vcsFactory->fromInstallation(new Document([ + 'provider' => $provider, + 'providerInstallationId' => $installation, + ])); + + // Git-forge archives wrap the tree in a "{repo}-{ref}/" root the + // source fetch strips, so this one has to match that shape. + $directory = '/tmp/vcs-archives/' . \uniqid(); + $wrapper = $repository . '-' . \substr(\preg_replace('/[^A-Za-z0-9._-]/', '-', $ref) ?? '', 0, 64); + $archive = $directory . '/archive.tar.gz'; + + $cloneType = \preg_match('/^[0-9a-f]{40}([0-9a-f]{24})?$/i', $ref) ? Git::CLONE_TYPE_COMMIT : Git::CLONE_TYPE_BRANCH; + $cloneCommand = $vcs->generateCloneCommand($owner, $repository, $ref, $cloneType, $directory . '/' . $wrapper, '*'); + + try { + $stdout = ''; + $stderr = ''; + + if (Console::execute($cloneCommand, '', $stdout, $stderr) !== 0) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to download the repository.'); + } + + $packCommand = 'tar --exclude=' . \escapeshellarg($wrapper . '/.git') . ' -czf ' . \escapeshellarg($archive) . ' -C ' . \escapeshellarg($directory) . ' ' . \escapeshellarg($wrapper); + if (Console::execute($packCommand, '', $stdout, $stderr) !== 0) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to package the repository.'); + } + + $contents = \file_get_contents($archive); + if ($contents === false) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to read the repository archive.'); + } + + $response + ->setContentType('application/gzip') + ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') + ->addHeader('Content-Disposition', 'attachment; filename="' . $wrapper . '.tar.gz"') + ->send($contents); + } finally { + Console::execute('rm -rf ' . \escapeshellarg($directory), '', $stdout, $stderr); + } + } +} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index 535dffcb3f..742710ddb1 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -5,6 +5,7 @@ namespace Appwrite\Platform\Modules\VCS\Http\GitHub; use Appwrite\Extend\Exception; use Appwrite\Filter\BranchDomain as BranchDomainFilter; use Appwrite\Vcs\Comment; +use Appwrite\Vcs\SourceArchive; use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; @@ -391,13 +392,14 @@ trait Deployment // The Deployments service is built per repository: a webhook fans out to // many tenant projects, each with its own database. + [$sourceUrl, $sourceHeaders] = SourceArchive::presign($vcs, $providerInstallationId, $providerRepositoryOwner, $providerRepositoryName, $providerCommitHash, $platform); $deployment = $authorization->skip(fn () => $deploymentsFactory($dbForProject, $project) ->createFromUrl( $resource, $deployment, - $vcs->getRepositoryPresignedUrl($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash), + $sourceUrl, $resource->getAttribute('providerRootDirectory', ''), - $vcs->getRepositoryPresignedUrlHeaders(), + $sourceHeaders, )); if ($resource->getCollection() === 'sites') { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php index 804edd6e11..1df76d5481 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php @@ -11,7 +11,6 @@ use Appwrite\SDK\Method; use Appwrite\SDK\MethodType; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; -use Utopia\Console; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -20,6 +19,22 @@ class Get extends Action { use HTTP; + /** + * Everything the integration touches: cloning sources, pushing templates, + * deploying from pull requests, commenting on them, and reporting build + * checks. repository:metadata:read is granted implicitly. + */ + public const SCOPES = [ + 'repository:contents:read', + 'repository:contents:write', + 'repository:pull_requests:read', + 'repository:pull_requests:write', + 'repository:pull_requests:reviews:read', + 'repository:pull_requests:reviews:write', + 'repository:checks:read', + 'repository:checks:write', + ]; + public static function getName() { return 'getVCSOriginAuthorize'; @@ -38,7 +53,7 @@ class Get extends Action namespace: 'vcs', group: 'installations', name: 'createOriginInstallation', - description: 'Begin Appwrite\'s Origin app installation to set up version control integration. This endpoint responds with a redirect URL to the Origin app\'s installation page on Cursor. The Origin app must be configured in your environment for this endpoint to work.', + description: '/docs/references/vcs/create-origin-installation.md', auth: [AuthType::ADMIN], responses: [ new SDKResponse( @@ -56,6 +71,7 @@ class Get extends Action ->inject('response') ->inject('project') ->inject('platform') + ->inject('vcsConfigured') ->callback($this->action(...)); } @@ -64,25 +80,40 @@ class Get extends Action string $failure, Response $response, Document $project, - array $platform + array $platform, + callable $vcsConfigured, ) { - $clientId = System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID'); - - if (empty($clientId)) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Origin client ID is not configured. Please configure VCS (Version Control System) variables in .env file.'); + if (!$vcsConfigured('origin')) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'This endpoint is not implemented on this server. Please configure VCS (Version Control System) variables in .env file.'); } - // The Cursor adapter expects its secret as a JSON object. - $oauth2 = new OAuth2Cursor($clientId, \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), ''); - $url = $oauth2->getLoginURL(); + $projectId = $project->getId(); - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] Authorize for project "' . $project->getId() . '" (success: "' . $success . '", failure: "' . $failure . '")'); - Console::log('[ORIGIN DEBUG] Redirecting to install URL: ' . $url); + // The callback is public and Origin performs no token exchange, so the + // state carries an HMAC binding the redirect targets to this project. + $state = [ + 'projectId' => $projectId, + 'success' => $success, + 'failure' => $failure, + 'signature' => \hash_hmac('sha256', \json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + ]; + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $callback = $protocol . '://' . ($platform['consoleHostname'] ?? '') . '/v1/vcs/origin/callback'; + + // The Cursor adapter expects its secret as a JSON object; starting an + // install only needs the app ID, so the key may be left out here. + $oauth2 = new OAuth2Cursor( + System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), + \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), + $callback, + $state, + self::SCOPES + ); $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') - ->redirect($url); + ->redirect($oauth2->getLoginURL()); } } 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 437a47c0dd..17ae23e0eb 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php @@ -5,8 +5,8 @@ namespace Appwrite\Platform\Modules\VCS\Http\Origin\Callback; use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; 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\Console; use Utopia\Database\Database; use Utopia\Database\Document; @@ -37,12 +37,12 @@ class Get extends Action ->label('scope', 'public') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'Origin installation ID', true) - ->param('installation_receipt', '', new Text(4096, 0), 'Origin installation receipt JWT, signed by Cursor.', true) - ->param('state', '', new Text(2048), 'Origin state. Contains info sent when starting the installation flow.', true) - ->inject('request') + ->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) ->inject('response') ->inject('dbForPlatform') ->inject('platform') + ->inject('vcsFactory') ->callback($this->action(...)); } @@ -50,68 +50,57 @@ class Get extends Action string $providerInstallationId, string $installationReceipt, string $state, - Request $request, Response $response, Database $dbForPlatform, - array $platform + array $platform, + VcsFactory $vcsFactory, ) { - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - $params = $request->getParams(); - Console::log('[ORIGIN DEBUG] Callback received'); - Console::log('[ORIGIN DEBUG] Callback params: ' . \json_encode($params)); - Console::log('[ORIGIN DEBUG] Callback headers: ' . \json_encode($request->getHeaders())); - Console::log('[ORIGIN DEBUG] Callback installation_id: "' . $providerInstallationId . '"'); - - // Surface anything that looks like an OAuth2 token flow (e.g. a code - // exchange), since Origin is not expected to send one. - $oauthParams = \array_intersect_key($params, \array_flip([ - 'code', 'token', 'access_token', 'refresh_token', 'expires_in', 'token_type', 'scope', 'id_token', 'setup_action', 'authuser', 'session_state', - ])); - if (!empty($oauthParams)) { - Console::log('[ORIGIN DEBUG] Possible OAuth2 flow params detected: ' . \json_encode($oauthParams)); - } else { - Console::log('[ORIGIN DEBUG] No OAuth2 flow params detected'); + if (empty($installationReceipt)) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Missing installation receipt. Please restart the installation from the Appwrite Console.'); } - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - // Decode only -- the EdDSA signature is not verified, so the claims - // must not be trusted for authorization decisions yet. - if (!empty($installationReceipt)) { - $segments = \explode('.', $installationReceipt); - if (\count($segments) === 3) { - $receiptHeader = \json_decode(\base64_decode(\strtr($segments[0], '-_', '+/')), true) ?? []; - $receiptClaims = \json_decode(\base64_decode(\strtr($segments[1], '-_', '+/')), true) ?? []; - Console::log('[ORIGIN DEBUG] Receipt JWT header: ' . \json_encode($receiptHeader)); - Console::log('[ORIGIN DEBUG] Receipt JWT claims: ' . \json_encode($receiptClaims)); - } else { - Console::log('[ORIGIN DEBUG] installation_receipt is not a JWT: ' . $installationReceipt); - } - } else { - Console::log('[ORIGIN DEBUG] No installation_receipt received'); + // Authenticate the callback before trusting anything else on it: the + // endpoint is public and Origin performs no token exchange, so the + // EdDSA-signed receipt is the only proof the request came from Cursor. + // The Cursor adapter expects its secret as a JSON object. + $oauth2 = new OAuth2Cursor( + System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), + \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), + '' + ); + + try { + $receiptClaims = $oauth2->verifyReceipt($installationReceipt); + } catch (\Throwable $e) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Could not verify the Origin installation receipt. Please restart the installation from the Appwrite Console.'); } + // The receipt's subject is the authoritative installation id; a + // separately passed installation_id may only confirm it. + $receiptInstallationId = \strval($receiptClaims['sub'] ?? ''); + if (!empty($providerInstallationId) && $providerInstallationId !== $receiptInstallationId) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Receipt subject does not match the installation ID.'); + } + $providerInstallationId = $receiptInstallationId; + + // Cursor echoes the state given to the install URL as a receipt claim; + // accept a query parameter carrying the same value as a fallback. + $state = !empty($state) ? $state : \strval($receiptClaims['state'] ?? ''); 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'] ?? ''; + $redirectFailure = $state['failure'] ?? ''; - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] Decoded state: ' . \json_encode($state)); - - // This endpoint is public and Origin performs no token exchange -- - // without verifying the signature the Authorize action put in state, - // anyone could pass an arbitrary projectId here and attach an - // installation to another project. + // The receipt authenticates Cursor, and this HMAC authenticates the + // Appwrite Console that started the flow -- without it anyone could + // attach an installation to an arbitrary projectId. $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); if (!\hash_equals($signature, $state['signature'] ?? '')) { - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] State signature mismatch, rejecting callback'); throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } - Console::log('[ORIGIN DEBUG] State signature valid'); $project = $dbForPlatform->getDocument('projects', $projectId); @@ -138,36 +127,19 @@ class Get extends Action return; } - // Authenticate the callback by verifying the installation receipt - // against Cursor's published JWKS. This proves the request genuinely - // originates from Origin (the callback is public and has no token - // exchange) before any installation is created. - // The Cursor adapter expects its secret as a JSON object. - $oauth2 = new OAuth2Cursor( - System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), - \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), - '' - ); - + // The receipt names the workspace by its stable id; the owner slug the + // rest of the integration works with comes from the installation itself. + $organization = \strval($receiptClaims['namespace_id'] ?? ''); try { - $receiptClaims = $oauth2->verifyReceipt($installationReceipt); - - // Bind the receipt to the installation id in the callback so a - // valid receipt cannot be replayed to attach a different - // installation. - if (($receiptClaims['sub'] ?? '') !== $providerInstallationId) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Receipt subject does not match installation ID'); - } + $adapter = $vcsFactory->fromInstallation(new Document([ + 'provider' => 'origin', + 'providerInstallationId' => $providerInstallationId, + ])); + $organization = $adapter->getOwnerName($providerInstallationId); } catch (\Throwable $e) { - Console::log('[ORIGIN DEBUG] Receipt verification failed: ' . $e->getMessage()); - $this->failure($response, $redirectFailure, 'Could not verify the Origin installation receipt.'); - return; + Console::warning('Failed to resolve Origin installation owner: ' . $e->getMessage()); } - // The Cursor namespace (team/workspace) owning the installation. - $organization = $receiptClaims['namespace_id'] ?? ''; - Console::log('[ORIGIN DEBUG] Receipt verified, namespace_id: "' . $organization . '"'); - $projectInternalId = $project->getSequence(); $installation = $dbForPlatform->findOne('installations', [ @@ -189,19 +161,12 @@ class Get extends Action 'organization' => $organization, 'personal' => false, ])); - - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] Created installation "' . $installation->getId() . '" for project "' . $projectId . '"'); } else { $installation = $dbForPlatform->updateDocument('installations', $installation->getId(), new Document([ 'organization' => $organization, ])); - - Console::log('[ORIGIN DEBUG] Installation already exists: "' . $installation->getId() . '"'); } - Console::log('[ORIGIN DEBUG] Redirecting to success URL: ' . $redirectSuccess); - $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') @@ -213,9 +178,6 @@ class Get extends Action */ private function failure(Response $response, string $redirect, string $error, string $type = Exception::GENERAL_ARGUMENT_INVALID): void { - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] Callback failed: ' . $error . ' (redirect: "' . $redirect . '")'); - if (empty($redirect)) { throw new Exception($type, $error); } @@ -226,5 +188,4 @@ class Get extends Action ->addHeader('Pragma', 'no-cache') ->redirect($redirect . $separator . \http_build_query(['error' => $error])); } - } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index aac94ffa02..98ae1fa770 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -2,16 +2,36 @@ namespace Appwrite\Platform\Modules\VCS\Http\Origin\Events; +use Appwrite\Extend\Exception; use Appwrite\Platform\Action; +use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; +use Utopia\Config\Config; use Utopia\Console; +use Utopia\Database\Database; +use Utopia\Database\Document; +use Utopia\Database\Query; +use Utopia\Database\Validator\Authorization; +use Utopia\DSN\DSN; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; +use Utopia\System\System; +use Utopia\VCS\Adapter\Git\Origin; class Create extends Action { use HTTP; + use Deployment; + + /** + * Origin's active signing keys, cached for the worker's lifetime. The set + * rotates rarely and a delivery that fails on a stale set refetches once. + * + * @var array|null + */ + protected static ?array $signingKeys = null; public static function getName() { @@ -26,33 +46,339 @@ class Create extends Action ->desc('Create event') ->groups(['api', 'vcs']) ->label('scope', 'public') + ->inject('vcsFactory') ->inject('request') ->inject('response') + ->inject('dbForPlatform') + ->inject('authorization') + ->inject('getProjectDB') + ->inject('deploymentsFactory') + ->inject('platform') ->callback($this->action(...)); } public function action( + VcsFactory $vcsFactory, Request $request, - Response $response + Response $response, + Database $dbForPlatform, + Authorization $authorization, + callable $getProjectDB, + callable $deploymentsFactory, + array $platform ) { - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN DEBUG] Event received'); - Console::log('[ORIGIN DEBUG] Event headers: ' . \json_encode($request->getHeaders())); - Console::log('[ORIGIN DEBUG] Event query params: ' . \json_encode($request->getParams())); - Console::log('[ORIGIN DEBUG] Event raw payload: ' . $request->getRawPayload()); + $vcs = $vcsFactory->fromProvider('origin'); - $payload = \json_decode($request->getRawPayload(), true) ?? []; - - // The delivery header is not confirmed yet -- accept both the Origin - // name and the pre-rename Codebase one. - $event = $request->getHeaderLine('x-origin-event', '') - ?: $request->getHeaderLine('x-codebase-event', '') - ?: ($payload['event'] ?? ''); + $event = $request->getHeaderLine($vcs->getEventHeaderName(), ''); Span::add('vcs.origin.event.name', $event); - Console::log('[ORIGIN DEBUG] Event name resolved to: "' . $event . '"'); - // Origin does not deliver push or pull request events yet, so events - // are only acknowledged to prevent delivery retries. - $response->json(['success' => true]); + $payload = $request->getRawPayload(); + $signature = $request->getHeaderLine($vcs->getSignatureHeaderName(), ''); + $deliveryId = $request->getHeaderLine('webhook-id', ''); + $timestamp = $request->getHeaderLine('webhook-timestamp', ''); + + // Origin signs the SHA-256 of ".." + // with its own Ed25519 key, verified against its published JWKS rather + // than a shared secret. Stale timestamps are replays. + $valid = false; + if (!empty($deliveryId) && \ctype_digit($timestamp) && \abs(\time() - (int) $timestamp) <= 300) { + $signedContent = $deliveryId . '.' . $timestamp . '.' . $payload; + + foreach ($this->signingKeys() as $publicKey) { + if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) { + $valid = true; + break; + } + } + + // The key set may have rotated since it was cached. + if (!$valid) { + foreach ($this->signingKeys(refresh: true) as $publicKey) { + if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) { + $valid = true; + break; + } + } + } + } + + Span::add('vcs.origin.event.signature.valid', $valid); + + if (!$valid) { + throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. The delivery could not be verified against Origin\'s published signing keys.'); + } + + $parsedPayloads = $vcs->getEvents($event, $payload); + + foreach ($parsedPayloads as $parsedPayload) { + match (true) { + $event === Origin::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), + \str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), + \str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), + default => null, + }; + } + + $response->json(['events' => $parsedPayloads]); + } + + /** + * Origin's active Ed25519 signing keys as base64url raw key material, the + * shape the adapter's validateWebhookEvent() accepts. + * + * @return array + */ + protected function signingKeys(bool $refresh = false): array + { + if (!$refresh && self::$signingKeys !== null) { + return self::$signingKeys; + } + + $keys = []; + + try { + $ch = \curl_init('https://api.cursor.com/v1/origin/keys'); + \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + \curl_setopt($ch, CURLOPT_TIMEOUT, 15); + $body = \curl_exec($ch); + \curl_close($ch); + + $jwks = \is_string($body) ? \json_decode($body, true) : null; + foreach (\is_array($jwks['keys'] ?? null) ? $jwks['keys'] : [] as $key) { + if (($key['kty'] ?? '') === 'OKP' && ($key['crv'] ?? '') === 'Ed25519' && !empty($key['x'])) { + $keys[] = \strval($key['x']); + } + } + } catch (\Throwable $e) { + Console::warning('Failed to fetch Origin signing keys: ' . $e->getMessage()); + } + + // Never cache an empty set - a fetch hiccup would reject deliveries + // until the worker restarts. + if (!empty($keys)) { + self::$signingKeys = $keys; + } + + return self::$signingKeys ?? []; + } + + protected function handleInstallationEvent( + array $parsedPayload, + Database $dbForPlatform, + Authorization $authorization, + callable $getProjectDB, + ) { + if ($parsedPayload['action'] !== 'deleted') { + return; + } + + $providerInstallationId = $parsedPayload['installationId']; + + $installationCursor = null; + do { + $installationQueries = [ + Query::equal('providerInstallationId', [$providerInstallationId]), + Query::equal('provider', ['origin']), + Query::limit(1000), + ]; + if ($installationCursor !== null) { + $installationQueries[] = Query::cursorAfter($installationCursor); + } + $installations = $authorization->skip(fn () => $dbForPlatform->find('installations', $installationQueries)); + + foreach ($installations as $installation) { + $projectId = $installation->getAttribute('projectId', ''); + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId)); + + if (!$project->isEmpty() && $this->isProjectInCurrentRegion($project)) { + $dbForProject = $getProjectDB($project); + + foreach (['functions', 'sites'] as $collection) { + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $resources = $authorization->skip(fn () => $dbForProject->find($collection, $queries)); + + foreach ($resources as $resource) { + $authorization->skip(fn () => $dbForProject->updateDocument($collection, $resource->getId(), new Document([ + 'installationId' => '', + 'installationInternalId' => '', + 'providerRepositoryId' => '', + 'providerBranch' => '', + 'providerSilentMode' => false, + 'providerRootDirectory' => '', + 'repositoryId' => '', + 'repositoryInternalId' => '', + ]))); + } + + $cursor = count($resources) === 1000 ? $resources[array_key_last($resources)] : null; + } while ($cursor !== null); + } + } + + $cursor = null; + do { + $queries = [ + Query::equal('installationInternalId', [$installation->getSequence()]), + Query::limit(1000), + ]; + if ($cursor !== null) { + $queries[] = Query::cursorAfter($cursor); + } + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', $queries)); + + foreach ($repositories as $repository) { + $authorization->skip(fn () => $dbForPlatform->deleteDocument('repositories', $repository->getId())); + } + + $cursor = count($repositories) === 1000 ? $repositories[array_key_last($repositories)] : null; + } while ($cursor !== null); + + $authorization->skip(fn () => $dbForPlatform->deleteDocument('installations', $installation->getId())); + } + + $installationCursor = count($installations) === 1000 ? $installations[array_key_last($installations)] : null; + } while ($installationCursor !== null); + } + + private function isProjectInCurrentRegion(Document $project): bool + { + try { + $dsn = new DSN($project->getAttribute('database')); + $databaseName = $dsn->getHost(); + } catch (\InvalidArgumentException) { + $databaseName = $project->getAttribute('database'); + } + + $databases = Config::getParam('pools-database', []); + if (!\in_array($databaseName, $databases)) { + Console::warning("Skipping project {$project->getId()}: database '{$databaseName}' is not part of region " . System::getEnv('_APP_REGION')); + return false; + } + + return true; + } + + private function handlePushEvent( + array $parsedPayload, + VcsFactory $vcsFactory, + Database $dbForPlatform, + Authorization $authorization, + callable $getProjectDB, + array $platform, + callable $deploymentsFactory, + ) { + $providerBranchDeleted = $parsedPayload['branchDeleted'] ?? false; + $providerBranch = $parsedPayload['branch'] ?? ''; + $providerBranchUrl = $parsedPayload['branchUrl'] ?? ''; + $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; + $providerRepositoryName = $parsedPayload['repositoryName'] ?? ''; + $providerInstallationId = $parsedPayload['installationId'] ?? ''; + $providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? ''; + $providerCommitHash = $parsedPayload['commitHash'] ?? ''; + $providerRepositoryOwner = $parsedPayload['owner'] ?? ''; + $providerCommitAuthorName = $parsedPayload['headCommitAuthorName'] ?? ''; + $providerCommitAuthorEmail = $parsedPayload['headCommitAuthorEmail'] ?? ''; + $providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? ''; + $providerCommitMessage = $parsedPayload['headCommitMessage'] ?? ''; + $providerCommitUrl = $parsedPayload['headCommitUrl'] ?? ''; + + Span::add('vcs.origin.event.repo.id', $providerRepositoryId); + Span::add('vcs.origin.event.repo.name', $providerRepositoryName); + Span::add('vcs.origin.event.branch', $providerBranch); + Span::add('vcs.origin.event.installation.id', $providerInstallationId); + + $vcs = $vcsFactory->fromInstallation(new Document([ + 'provider' => 'origin', + 'providerInstallationId' => $providerInstallationId, + ])); + + // Find associated repositories + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::limit(100), + ])); + + // Create new deployment only on push (not committed by us) and not when branch is deleted + if (!\in_array($providerCommitAuthorEmail, [APP_VCS_GITHUB_EMAIL, APP_VCS_ORIGIN_EMAIL], true) && !$providerBranchDeleted) { + $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory); + } + } + + private function handlePullRequestEvent( + array $parsedPayload, + VcsFactory $vcsFactory, + Database $dbForPlatform, + Authorization $authorization, + callable $getProjectDB, + array $platform, + callable $deploymentsFactory, + ) { + $action = $parsedPayload['action'] ?? ''; + + if ($action == 'opened' || $action == 'reopened' || $action == 'synchronize') { + $providerBranch = $parsedPayload['branch'] ?? ''; + $providerBranchUrl = $parsedPayload['branchUrl'] ?? ''; + $providerRepositoryId = $parsedPayload['repositoryId'] ?? ''; + $providerRepositoryName = $parsedPayload['repositoryName'] ?? ''; + $providerInstallationId = $parsedPayload['installationId'] ?? ''; + $providerRepositoryUrl = $parsedPayload['repositoryUrl'] ?? ''; + $providerPullRequestId = $parsedPayload['pullRequestNumber'] ?? ''; + $providerCommitHash = $parsedPayload['commitHash'] ?? ''; + $providerRepositoryOwner = $parsedPayload['owner'] ?? ''; + $external = $parsedPayload['external'] ?? false; + $providerCommitUrl = $parsedPayload['headCommitUrl'] ?? ''; + $providerCommitAuthorUrl = $parsedPayload['authorUrl'] ?? ''; + + Span::add('vcs.origin.event.repo.id', $providerRepositoryId); + Span::add('vcs.origin.event.repo.name', $providerRepositoryName); + Span::add('vcs.origin.event.branch', $providerBranch); + Span::add('vcs.origin.event.installation.id', $providerInstallationId); + + // Ignore sync for non-external. We handle it in push webhook. + // Origin has no fork model, so every pull request is non-external. + if (!$external && $action == 'synchronize') { + return; + } + + $vcs = $vcsFactory->fromInstallation(new Document([ + 'provider' => 'origin', + 'providerInstallationId' => $providerInstallationId, + ])); + + try { + $commitDetails = $vcs->getCommit($providerRepositoryOwner, $providerRepositoryName, $providerCommitHash); + } catch (\Throwable $e) { + Console::warning("Failed to fetch commit '{$providerCommitHash}': " . $e->getMessage()); + $commitDetails = []; + } + $providerCommitAuthor = $commitDetails['commitAuthor'] ?? ''; + $providerCommitMessage = $commitDetails['commitMessage'] ?? ''; + + $prFiles = $vcs->getPullRequestFiles($providerRepositoryOwner, $providerRepositoryName, (int) $providerPullRequestId); + $providerAffectedFiles = [ + ...array_column($prFiles, 'filename'), + // Only renamed files carry a previous filename; skip missing values from other file changes. + ...array_filter(array_column($prFiles, 'previousFilename')) + ]; + + $repositories = $authorization->skip(fn () => $dbForPlatform->find('repositories', [ + Query::equal('providerRepositoryId', [$providerRepositoryId]), + Query::orderDesc('$createdAt') + ])); + + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, \strval($providerPullRequestId), $providerAffectedFiles, $external, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory); + } + + // No cleanup on close: Origin pull requests are never external, so no + // authorized-contributor entries accumulate on the repository. } } diff --git a/src/Appwrite/Platform/Modules/VCS/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index 71498db484..55e268980c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\VCS\Services; +use Appwrite\Platform\Modules\VCS\Http\Archives\Get as GetArchive; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Authorize\Get as GetBitbucketAuthorize; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Callback\Get as GetBitbucketCallback; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Events\Create as CreateBitbucketEvent; @@ -63,6 +64,9 @@ class Http extends Service $this->addAction(DeleteInstallation::getName(), new DeleteInstallation()); $this->addAction(ListNamespaces::getName(), new ListNamespaces()); + // Archives + $this->addAction(GetArchive::getName(), new GetArchive()); + // Repositories $this->addAction(CreateRepository::getName(), new CreateRepository()); $this->addAction(GetRepository::getName(), new GetRepository()); diff --git a/src/Appwrite/Vcs/SourceArchive.php b/src/Appwrite/Vcs/SourceArchive.php new file mode 100644 index 0000000000..3e1093fc4c --- /dev/null +++ b/src/Appwrite/Vcs/SourceArchive.php @@ -0,0 +1,66 @@ + $platform + * @return array{string, array} The source URL and the headers to fetch it with + */ + public static function presign(Git $vcs, string $providerInstallationId, string $owner, string $repository, string $ref, array $platform): array + { + if ($vcs->supportsRepositoryArchives()) { + return [ + $vcs->getRepositoryPresignedUrl($owner, $repository, $ref), + $vcs->getRepositoryPresignedUrlHeaders(), + ]; + } + + $expires = \time() + self::TTL; + + $params = [ + 'provider' => $vcs->getName(), + 'installation' => $providerInstallationId, + 'owner' => $owner, + 'repository' => $repository, + 'ref' => $ref, + 'expires' => $expires, + 'signature' => self::signature($vcs->getName(), $providerInstallationId, $owner, $repository, $ref, $expires), + ]; + + // The jobs-service reaches Appwrite over the internal Docker network + // when configured, the same way its presigned callback URLs do. + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $endpoint = \rtrim(System::getEnv('_APP_JOBS_ENDPOINT', $protocol . '://' . ($platform['apiHostname'] ?? '')), '/'); + + return [$endpoint . '/v1/vcs/archives?' . \http_build_query($params), []]; + } + + public static function signature(string $provider, string $providerInstallationId, string $owner, string $repository, string $ref, int $expires): string + { + return \hash_hmac( + 'sha256', + \implode("\n", [$provider, $providerInstallationId, $owner, $repository, $ref, $expires]), + System::getEnv('_APP_OPENSSL_KEY_V1', '') + ); + } +} From 6d54b49414024a8afa4b49d1a86a4ac53bff160d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 15:12:05 +0200 Subject: [PATCH 14/38] Rename env vars --- .env | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 49be7302b3..0be2ca8563 100644 --- a/.env +++ b/.env @@ -170,7 +170,7 @@ _APP_VCS_GITLAB_WEBHOOK_SECRET= _APP_VCS_BITBUCKET_CLIENT_ID= _APP_VCS_BITBUCKET_CLIENT_SECRET= _APP_VCS_BITBUCKET_WEBHOOK_SECRET= -_APP_VCS_CODEBASE_CLIENT_ID= -_APP_VCS_CODEBASE_PRIVATE_KEY="" +_APP_VCS_ORIGIN_CLIENT_ID= +_APP_VCS_ORIGIN_PRIVATE_KEY="" _APP_VCS_WEBHOOK_URL=http://appwrite/v1 _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 From 1844afdf94a86a49ee5a0db8256cbedbc0f7256c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 15:49:49 +0200 Subject: [PATCH 15/38] chore: bump utopia-php/vcs for the Origin pushed_at fix Co-Authored-By: Claude Fable 5 --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index e0708c1fb9..f003e55732 100644 --- a/composer.lock +++ b/composer.lock @@ -5035,12 +5035,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "cbf57939aa7f0d40c5e6ab9120e13b3247bd767a" + "reference": "f841df3867d6f98bfbab9faa5cd0b38a04674fb4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/cbf57939aa7f0d40c5e6ab9120e13b3247bd767a", - "reference": "cbf57939aa7f0d40c5e6ab9120e13b3247bd767a", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f841df3867d6f98bfbab9faa5cd0b38a04674fb4", + "reference": "f841df3867d6f98bfbab9faa5cd0b38a04674fb4", "shasum": "" }, "require": { @@ -5094,7 +5094,7 @@ "source": "https://github.com/utopia-php/vcs/tree/feat-origin", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-18T12:28:48+00:00" + "time": "2026-08-18T13:49:34+00:00" }, { "name": "utopia-php/websocket", From 7e1a2fd2133cb1e866a0a490cd37a3e0016742a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 15:52:29 +0200 Subject: [PATCH 16/38] chore: bump utopia-php/vcs for Origin error messages Co-Authored-By: Claude Fable 5 --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index f003e55732..654825ebd4 100644 --- a/composer.lock +++ b/composer.lock @@ -5035,12 +5035,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "f841df3867d6f98bfbab9faa5cd0b38a04674fb4" + "reference": "cb6f3c1d6d72487b264070e663ade8da57cd3a4a" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/f841df3867d6f98bfbab9faa5cd0b38a04674fb4", - "reference": "f841df3867d6f98bfbab9faa5cd0b38a04674fb4", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/cb6f3c1d6d72487b264070e663ade8da57cd3a4a", + "reference": "cb6f3c1d6d72487b264070e663ade8da57cd3a4a", "shasum": "" }, "require": { @@ -5094,7 +5094,7 @@ "source": "https://github.com/utopia-php/vcs/tree/feat-origin", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-18T13:49:34+00:00" + "time": "2026-08-18T13:52:12+00:00" }, { "name": "utopia-php/websocket", From 99d0243081f67250a16afffecae4bc5d15cb4953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 16:14:08 +0200 Subject: [PATCH 17/38] chore: bump utopia-php/vcs for slash-branch commit lookups Co-Authored-By: Claude Fable 5 --- composer.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/composer.lock b/composer.lock index 654825ebd4..3b863515d2 100644 --- a/composer.lock +++ b/composer.lock @@ -5035,12 +5035,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "cb6f3c1d6d72487b264070e663ade8da57cd3a4a" + "reference": "41b0ea07fd78081fb9a29f16d578b23840891803" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/cb6f3c1d6d72487b264070e663ade8da57cd3a4a", - "reference": "cb6f3c1d6d72487b264070e663ade8da57cd3a4a", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/41b0ea07fd78081fb9a29f16d578b23840891803", + "reference": "41b0ea07fd78081fb9a29f16d578b23840891803", "shasum": "" }, "require": { @@ -5094,7 +5094,7 @@ "source": "https://github.com/utopia-php/vcs/tree/feat-origin", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-18T13:52:12+00:00" + "time": "2026-08-18T14:13:55+00:00" }, { "name": "utopia-php/websocket", From f23b94b848ba5c9112894cf55e80c98b3aa6fe31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 16:42:48 +0200 Subject: [PATCH 18/38] chore: temporary debug logging on the Origin events endpoint Marked with TODOs; strip once the integration is verified end to end. Co-Authored-By: Claude Fable 5 --- .../Modules/VCS/Http/Origin/Events/Create.php | 33 ++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index 98ae1fa770..c84d12c41c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -77,6 +77,10 @@ class Create extends Action $deliveryId = $request->getHeaderLine('webhook-id', ''); $timestamp = $request->getHeaderLine('webhook-timestamp', ''); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN EVENT] delivery=' . $deliveryId . ' type=' . $event . ' timestamp=' . $timestamp . ' signature=' . (empty($signature) ? 'missing' : 'present')); + Console::log('[ORIGIN EVENT] payload: ' . \mb_strimwidth($payload, 0, 8000, '...[truncated]')); + // Origin signs the SHA-256 of ".." // with its own Ed25519 key, verified against its published JWKS rather // than a shared secret. Stale timestamps are replays. @@ -103,18 +107,28 @@ class Create extends Action } Span::add('vcs.origin.event.signature.valid', $valid); + Console::log('[ORIGIN EVENT] signature valid: ' . \var_export($valid, true)); if (!$valid) { throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. The delivery could not be verified against Origin\'s published signing keys.'); } $parsedPayloads = $vcs->getEvents($event, $payload); + Console::log('[ORIGIN EVENT] parsed ' . \count($parsedPayloads) . ' event(s): ' . \json_encode($parsedPayloads)); foreach ($parsedPayloads as $parsedPayload) { - match (true) { - $event === Origin::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), - \str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), - \str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), + $handler = match (true) { + $event === Origin::EVENT_PUSH => 'push', + \str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => 'pull_request', + \str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => 'installation', + default => 'none', + }; + Console::log('[ORIGIN EVENT] dispatching to handler: ' . $handler); + + match ($handler) { + 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), + 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), + 'installation' => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), default => null, }; } @@ -306,10 +320,21 @@ class Create extends Action Query::limit(100), ])); + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + Console::log('[ORIGIN EVENT] push: repo=' . $providerRepositoryId . ' branch=' . $providerBranch . ' commit=' . $providerCommitHash . ' authorEmail=' . $providerCommitAuthorEmail . ' deleted=' . \var_export($providerBranchDeleted, true)); + Console::log('[ORIGIN EVENT] push: matched ' . \count($repositories) . ' connected repositories document(s) for providerRepositoryId=' . $providerRepositoryId); + foreach ($repositories as $connected) { + Console::log('[ORIGIN EVENT] push: connected repo doc ' . $connected->getId() . ' project=' . $connected->getAttribute('projectId') . ' resource=' . $connected->getAttribute('resourceType') . '/' . $connected->getAttribute('resourceId')); + } + // Create new deployment only on push (not committed by us) and not when branch is deleted if (!\in_array($providerCommitAuthorEmail, [APP_VCS_GITHUB_EMAIL, APP_VCS_ORIGIN_EMAIL], true) && !$providerBranchDeleted) { $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; + Console::log('[ORIGIN EVENT] push: creating git deployments'); $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory); + Console::log('[ORIGIN EVENT] push: createGitDeployments finished'); + } else { + Console::log('[ORIGIN EVENT] push: skipped (self-commit or branch deletion)'); } } From fa5048c3dc757a5f770d7d267bc4171cf65f6693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:10:58 +0200 Subject: [PATCH 19/38] fix: deduplicate Origin webhook deliveries by webhook-id Origin delivers at least once and retries any delivery not acknowledged with a 2xx inside its sender timeout. The events endpoint processes deliveries synchronously and can outlast that window under load, so every retry of one push created another deployment - another build, more load, slower responses, more retries. Claim the delivery id atomically before processing and acknowledge late retries without action, as the Origin docs prescribe. Acknowledging first and processing asynchronously remains the proper fix and is marked as a TODO. Co-Authored-By: Claude Fable 5 --- .../Modules/VCS/Http/Origin/Events/Create.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index c84d12c41c..ada3fe27fd 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -12,6 +12,7 @@ use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\Document; +use Utopia\Database\Exception\Duplicate; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\DSN\DSN; @@ -113,6 +114,21 @@ class Create extends Action throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. The delivery could not be verified against Origin\'s published signing keys.'); } + // Origin delivers at least once and retries anything not acknowledged + // with a 2xx in time. Processing here is synchronous and can outlast + // that window, so claim the delivery id before doing the work - a + // retry of a delivery already being processed must not deploy again. + // TODO: acknowledge first and process asynchronously instead. + try { + $authorization->skip(fn () => $dbForPlatform->createDocument('vcsCommentLocks', new Document([ + '$id' => 'origin-delivery-' . $deliveryId, + ]))); + } catch (Duplicate) { + Console::log('[ORIGIN EVENT] duplicate delivery ' . $deliveryId . ' - already processed, acknowledging without action'); + $response->json(['events' => [], 'duplicate' => true]); + return; + } + $parsedPayloads = $vcs->getEvents($event, $payload); Console::log('[ORIGIN EVENT] parsed ' . \count($parsedPayloads) . ' event(s): ' . \json_encode($parsedPayloads)); From 895d99653dd4c0cc8ee8c7e7af9384cc8223aef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:14:56 +0200 Subject: [PATCH 20/38] chore: log outgoing PR comment image and link URLs Temporary, TODO-marked: compares the scheme we send against what the provider renders, while verifying the Origin integration. Co-Authored-By: Claude Fable 5 --- src/Appwrite/Vcs/Comment.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index ac0f6e1dae..6989992b1a 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -2,6 +2,7 @@ namespace Appwrite\Vcs; +use Utopia\Console; use Utopia\Database\Document; use Utopia\System\System; @@ -243,6 +244,15 @@ class Comment $text .= "\n
\n\n> [!TIP]\n> {$this->tip}\n\n"; + // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. + // Log every image and link URL the comment carries, to compare the + // scheme we send against the scheme the provider ends up rendering. + $urls = []; + if (\preg_match_all('/(?:src|srcset|href)="([^"]+)"|\]\((https?:[^)]+)\)/', $text, $matches)) { + $urls = \array_values(\array_filter(\array_merge($matches[1], $matches[2]))); + } + Console::log('[VCS COMMENT] outgoing image/link URLs: ' . \json_encode($urls)); + return $text; } From 3024ae14096c19618becb681c201d34df9a99735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:23:07 +0200 Subject: [PATCH 21/38] fix: surface swallowed VCS build-status reporting failures The gitAction call is best-effort by design, but a silent catch left the provider's check and PR comment frozen at their last state with nothing to debug from when reporting failed (seen with a dropped database connection). Log what was lost. Co-Authored-By: Claude Fable 5 --- src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 41c81ee3a5..3ee13ac603 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -18,6 +18,7 @@ use Appwrite\Usage\Context as UsageContext; use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Cache\Cache; +use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -514,8 +515,11 @@ class Jobs extends Action $dbForPlatform, $platform, ); - } catch (\Throwable) { - // Best-effort — never fails the build. + } catch (\Throwable $error) { + // Best-effort — never fails the build. But say what was lost: a + // swallowed failure here leaves the provider's check and comment + // frozen at their last state with nothing to debug from. + Console::warning('Failed to report build status to VCS provider for deployment ' . $deployment->getId() . ': ' . $error->getMessage()); } } From 28d3de729426a72a8d1b742b4aed25f68fbea13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:24:28 +0200 Subject: [PATCH 22/38] chore: temporarily hardcode cloud.appwrite.io for PR comment assets Comment images can never render on the provider's HTTPS page from a plain-HTTP localhost dev stack (browsers auto-upgrade mixed-content images), so point them at real HTTPS assets while verifying the Origin integration. TODO-marked; restore the env-driven protocol and platform hostname before merge. Co-Authored-By: Claude Fable 5 --- src/Appwrite/Vcs/Comment.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index 6989992b1a..8d72b7dd17 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -128,8 +128,11 @@ class Comment $i = 0; foreach ($projects as $projectId => $project) { - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; - $hostname = $this->platform['consoleHostname'] ?? ''; + // TODO: Temporarily hardcoded so comment images resolve over real + // HTTPS while verifying the Origin integration - restore the + // env-driven protocol and platform hostname afterwards. + $protocol = 'https'; + $hostname = 'cloud.appwrite.io'; $text .= "## {$project['name']}\n\n"; $text .= "Project ID: `{$projectId}`\n\n"; @@ -258,8 +261,11 @@ class Comment public function generatImage(string $pathLight, string $pathDark, string $alt, int $width): string { - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; - $hostname = $this->platform['consoleHostname'] ?? ''; + // TODO: Temporarily hardcoded so comment images resolve over real + // HTTPS while verifying the Origin integration - restore the + // env-driven protocol and platform hostname afterwards. + $protocol = 'https'; + $hostname = 'cloud.appwrite.io'; $imageLight = $protocol . '://' . $hostname . $pathLight; $imageDark = $protocol . '://' . $hostname . $pathDark; From d05e47c5e85d02214d795c2eacc0f526bdcbaddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:31:59 +0200 Subject: [PATCH 23/38] feat: text-only PR comments for providers without image support Reverts the temporary cloud.appwrite.io hostname hardcode and replaces it with a capability: adapters now report supportsCommentImages(), and the comment builder falls back to textual status cells and a plain QR link when the provider cannot render images. Origin renders comment markdown without an image proxy, so images on the consumer's own host can never display there; every other provider keeps the full visual comment. Co-Authored-By: Claude Fable 5 --- composer.lock | 8 +++--- src/Appwrite/Deployment/GitAction.php | 2 +- .../Modules/VCS/Http/GitHub/Deployment.php | 8 +++--- src/Appwrite/Vcs/Comment.php | 27 ++++++++++--------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/composer.lock b/composer.lock index 3b863515d2..75162b8e72 100644 --- a/composer.lock +++ b/composer.lock @@ -5035,12 +5035,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "41b0ea07fd78081fb9a29f16d578b23840891803" + "reference": "8b37a20f5ae04b1ab62e733da5267946d454aaa3" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/41b0ea07fd78081fb9a29f16d578b23840891803", - "reference": "41b0ea07fd78081fb9a29f16d578b23840891803", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/8b37a20f5ae04b1ab62e733da5267946d454aaa3", + "reference": "8b37a20f5ae04b1ab62e733da5267946d454aaa3", "shasum": "" }, "require": { @@ -5094,7 +5094,7 @@ "source": "https://github.com/utopia-php/vcs/tree/feat-origin", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-18T14:13:55+00:00" + "time": "2026-08-18T15:30:44+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Deployment/GitAction.php b/src/Appwrite/Deployment/GitAction.php index 03d679b2ba..8d709a0a2e 100644 --- a/src/Appwrite/Deployment/GitAction.php +++ b/src/Appwrite/Deployment/GitAction.php @@ -86,7 +86,7 @@ final class GitAction ]); $previewUrl = $isSite && !$rule->isEmpty() ? "{$protocol}://" . $rule->getAttribute('domain', '') : ''; - $comment = new Comment($platform); + $comment = new Comment($platform, $vcs->supportsCommentImages()); $comment->parseComment($vcs->getComment($owner, $repositoryName, $commentId)); $comment->addBuild($project, $resource, $isSite ? 'site' : 'function', $status, $deployment->getId(), ['type' => 'logs'], $previewUrl); $vcs->updateComment($owner, $repositoryName, $commentId, $comment->generateComment()); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php index 742710ddb1..08fbedaebc 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Deployment.php @@ -231,7 +231,7 @@ trait Deployment if ($lockAcquired) { // Wrap in try/finally to ensure lock file gets deleted try { - $comment = new Comment($platform); + $comment = new Comment($platform, $vcs->supportsCommentImages()); $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $commentPreviewUrl); @@ -243,7 +243,7 @@ trait Deployment } } } else { - $comment = new Comment($platform); + $comment = new Comment($platform, $vcs->supportsCommentImages()); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $commentPreviewUrl); $latestCommentId = \strval($vcs->createComment($owner, $repositoryName, $providerPullRequestId, $comment->generateComment())); @@ -304,7 +304,7 @@ trait Deployment if ($lockAcquired) { // Wrap in try/finally to ensure lock file gets deleted try { - $comment = new Comment($platform); + $comment = new Comment($platform, $vcs->supportsCommentImages()); $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, ''); @@ -531,7 +531,7 @@ trait Deployment $previewUrl = !$rule->isEmpty() ? ("{$protocol}://" . $rule->getAttribute('domain', '')) : ''; if (!empty($previewUrl)) { - $comment = new Comment($platform); + $comment = new Comment($platform, $vcs->supportsCommentImages()); $comment->parseComment($vcs->getComment($owner, $repositoryName, $latestCommentId)); $comment->addBuild($project, $resource, $resourceType, $commentStatus, $deploymentId, $action, $previewUrl); $vcs->updateComment($owner, $repositoryName, $latestCommentId, $comment->generateComment()); diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index 8d72b7dd17..9a6ca6ba5f 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -11,7 +11,8 @@ use Utopia\System\System; class Comment { public function __construct( - private array $platform + private array $platform, + private bool $withImages = true, ) { } @@ -128,11 +129,8 @@ class Comment $i = 0; foreach ($projects as $projectId => $project) { - // TODO: Temporarily hardcoded so comment images resolve over real - // HTTPS while verifying the Origin integration - restore the - // env-driven protocol and platform hostname afterwards. - $protocol = 'https'; - $hostname = 'cloud.appwrite.io'; + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $hostname = $this->platform['consoleHostname'] ?? ''; $text .= "## {$project['name']}\n\n"; $text .= "Project ID: `{$projectId}`\n\n"; @@ -177,7 +175,9 @@ class Comment $qrImagePathDark = '/images/vcs/qr-dark.svg'; $consoleUrl = $protocol . '://' . $hostname . '/v1/avatars/qr?text=' . \urlencode($site['previewUrl']); - $qr = '[' . $this->generatImage($qrImagePathLight, $qrImagePathDark, 'QR Code', 28) . '](' . $consoleUrl . ')'; + $qr = $this->withImages + ? '[' . $this->generatImage($qrImagePathLight, $qrImagePathDark, 'QR Code', 28) . '](' . $consoleUrl . ')' + : '[QR Code](' . $consoleUrl . ')'; $preview = '[Preview URL](' . $site['previewUrl'] . ')'; @@ -261,11 +261,14 @@ class Comment public function generatImage(string $pathLight, string $pathDark, string $alt, int $width): string { - // TODO: Temporarily hardcoded so comment images resolve over real - // HTTPS while verifying the Origin integration - restore the - // env-driven protocol and platform hostname afterwards. - $protocol = 'https'; - $hostname = 'cloud.appwrite.io'; + // Providers without comment image support get the textual cells only. + if (!$this->withImages) { + return ''; + } + + + $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; + $hostname = $this->platform['consoleHostname'] ?? ''; $imageLight = $protocol . '://' . $hostname . $pathLight; $imageDark = $protocol . '://' . $hostname . $pathDark; From 90d7591ec4d2a21d786a2766c463b47d05307597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 18 Aug 2026 17:33:05 +0200 Subject: [PATCH 24/38] chore: temporarily disable preview deployment authorization TODO-marked override while verifying the Origin integration; preview domains open without the auth cookie instead of redirecting to the console auth flow. Remove before merge. Co-Authored-By: Claude Fable 5 --- app/controllers/general.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/general.php b/app/controllers/general.php index ebf28a9162..d6f68a5217 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -246,6 +246,10 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S - If authorized, continue as if auth was not required */ $requirePreview = \is_null($apiKey) || !$apiKey->isPreviewAuthDisabled(); + // TODO: Temporarily disabled while verifying the Origin integration - + // preview deployments open without the auth cookie / console redirect. + // Remove this override to restore preview authorization. + $requirePreview = false; if ($isPreview && $requirePreview) { $cookie = $request->getCookie(COOKIE_NAME_PREVIEW, ''); $authorized = false; From c2dd4a9ad4e68b6575c4e1c17973c7e456e727cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 12:28:27 +0200 Subject: [PATCH 25/38] chore: bump utopia-php/vcs to 5.2.0 The feat-origin branch merged and was tagged, so the dev pin goes back to main's 5.*.* constraint. 5.2.0 also makes Origin's createRepository() throw instead of riding Cursor's undocumented web API, so the create endpoint asks supportsRepositoryCreation() first and answers with a clear client error. Co-Authored-By: Claude Fable 5 --- .../Modules/VCS/Http/Installations/Repositories/Create.php | 5 +++++ 1 file changed, 5 insertions(+) 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 d8dae58f3b..7f29f0b005 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Installations/Repositories/Create.php @@ -112,6 +112,11 @@ class Create extends Action } else { $providerInstallationId = $installation->getAttribute('providerInstallationId'); $vcs = $vcsFactory->fromInstallation($installation); + + if (!$vcs->supportsRepositoryCreation()) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'VCS provider does not support repository creation: ' . $provider); + } + $owner = !empty($providerNamespace) ? $providerNamespace : $vcs->getOwnerName($providerInstallationId); try { From 15415e95bf29b53e783c6c5e59412d8224f0f66c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 12:30:52 +0200 Subject: [PATCH 26/38] feat: report VCS provider capabilities to the console The console variables response gains a typed vcsProviders list: each configured provider with whether it supports repository creation and public repositories, read from the 5.2.0 adapter capability API. The _APP_VCS_PROVIDERS name list stays for released consoles. Co-Authored-By: Claude Fable 5 --- app/init/models.php | 2 + .../Modules/Console/Http/Variables/Get.php | 18 ++++++- src/Appwrite/Utopia/Response.php | 1 + .../Response/Model/ConsoleVariables.php | 7 +++ .../Utopia/Response/Model/VcsProvider.php | 52 +++++++++++++++++++ 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 src/Appwrite/Utopia/Response/Model/VcsProvider.php diff --git a/app/init/models.php b/app/init/models.php index 41651c9262..41163bec25 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -227,6 +227,7 @@ use Appwrite\Utopia\Response\Model\User; use Appwrite\Utopia\Response\Model\Variable; use Appwrite\Utopia\Response\Model\VcsContent; use Appwrite\Utopia\Response\Model\VcsNamespace; +use Appwrite\Utopia\Response\Model\VcsProvider; use Appwrite\Utopia\Response\Model\VectorsDBCollection; use Appwrite\Utopia\Response\Model\Webhook; @@ -264,6 +265,7 @@ Response::setModel(new ProviderRepositoryFrameworkList()); Response::setModel(new ProviderRepositoryRuntimeList()); Response::setModel(new VcsNamespace()); Response::setModel(new BaseList('VCS Namespaces List', Response::MODEL_VCS_NAMESPACE_LIST, 'namespaces', Response::MODEL_VCS_NAMESPACE)); +Response::setModel(new VcsProvider()); Response::setModel(new BaseList('Branches List', Response::MODEL_BRANCH_LIST, 'branches', Response::MODEL_BRANCH)); Response::setModel(new BaseList('Frameworks List', Response::MODEL_FRAMEWORK_LIST, 'frameworks', Response::MODEL_FRAMEWORK)); Response::setModel(new BaseList('Runtimes List', Response::MODEL_RUNTIME_LIST, 'runtimes', Response::MODEL_RUNTIME)); diff --git a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php index 9adf5ebf9a..92d4fa5466 100644 --- a/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php +++ b/src/Appwrite/Platform/Modules/Console/Http/Variables/Get.php @@ -7,6 +7,7 @@ use Appwrite\SDK\ContentType; use Appwrite\SDK\Method; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Domains\Domain; @@ -47,6 +48,7 @@ class Get extends Action contentType: ContentType::JSON )) ->inject('vcsProviders') + ->inject('vcsFactory') ->inject('response') ->inject('platform') ->inject('dbForProject') @@ -55,6 +57,7 @@ class Get extends Action public function action( callable $vcsProviders, + VcsFactory $vcsFactory, Response $response, array $platform, Database $dbForProject @@ -71,7 +74,17 @@ class Get extends Action $isDomainEnabled = $isAAAAValid || $isAValid || $isCNAMEValid; - $isVcsEnabled = !empty($vcsProviders()); + $providers = $vcsProviders(); + $isVcsEnabled = !empty($providers); + $providerCapabilities = \array_map(function (string $provider) use ($vcsFactory) { + $adapter = $vcsFactory->fromProvider($provider); + + return new Document([ + 'name' => $provider, + 'supportForRepositoryCreation' => $adapter->supportsRepositoryCreation(), + 'supportForPublicRepositories' => $adapter->supportsPublicRepositories(), + ]); + }, $providers); $isAssistantEnabled = !empty(System::getEnv('_APP_ASSISTANT_OPENAI_API_KEY', '')); @@ -87,7 +100,8 @@ class Get extends Action '_APP_COMPUTE_SIZE_LIMIT' => +System::getEnv('_APP_COMPUTE_SIZE_LIMIT'), '_APP_USAGE_STATS' => System::getEnv('_APP_USAGE_STATS'), '_APP_VCS_ENABLED' => $isVcsEnabled, - '_APP_VCS_PROVIDERS' => $vcsProviders(), + '_APP_VCS_PROVIDERS' => $providers, + 'vcsProviders' => $providerCapabilities, '_APP_DOMAIN_ENABLED' => $isDomainEnabled, '_APP_ASSISTANT_ENABLED' => $isAssistantEnabled, '_APP_DOMAIN_SITES' => $platform['sitesDomain'], diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 80dd69bd38..c7e8d1f82f 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -195,6 +195,7 @@ class Response extends SwooleResponse public const MODEL_PROVIDER_REPOSITORY_RUNTIME_LIST = 'providerRepositoryRuntimeList'; public const MODEL_VCS_NAMESPACE = 'vcsNamespace'; public const MODEL_VCS_NAMESPACE_LIST = 'vcsNamespaceList'; + public const MODEL_VCS_PROVIDER = 'vcsProvider'; public const MODEL_BRANCH = 'branch'; public const MODEL_BRANCH_LIST = 'branchList'; public const MODEL_DETECTION_FRAMEWORK = 'detectionFramework'; diff --git a/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php b/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php index ab179d04ee..4013d6bb71 100644 --- a/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php +++ b/src/Appwrite/Utopia/Response/Model/ConsoleVariables.php @@ -71,6 +71,13 @@ class ConsoleVariables extends Model 'example' => ['github'], 'array' => true, ]) + ->addRule('vcsProviders', [ + 'type' => Response::MODEL_VCS_PROVIDER, + 'description' => 'Configured VCS providers and their capabilities.', + 'default' => [], + 'example' => [], + 'array' => true, + ]) ->addRule('_APP_DOMAIN_ENABLED', [ 'type' => self::TYPE_BOOLEAN, 'description' => 'Defines if main domain is configured. If so, custom domains can be created.', diff --git a/src/Appwrite/Utopia/Response/Model/VcsProvider.php b/src/Appwrite/Utopia/Response/Model/VcsProvider.php new file mode 100644 index 0000000000..f7cd552603 --- /dev/null +++ b/src/Appwrite/Utopia/Response/Model/VcsProvider.php @@ -0,0 +1,52 @@ +addRule('name', [ + 'type' => self::TYPE_STRING, + 'description' => 'VCS (Version Control System) provider name.', + 'default' => '', + 'example' => 'github', + ]) + ->addRule('supportForRepositoryCreation', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Defines if the provider allows creating repositories. Some providers reserve repository creation for their own UI.', + 'default' => false, + 'example' => true, + ]) + ->addRule('supportForPublicRepositories', [ + 'type' => self::TYPE_BOOLEAN, + 'description' => 'Defines if the provider can host repositories that are readable without authentication.', + 'default' => false, + 'example' => true, + ]); + } + + /** + * Get Name + * + * @return string + */ + public function getName(): string + { + return 'VcsProvider'; + } + + /** + * Get Type + * + * @return string + */ + public function getType(): string + { + return Response::MODEL_VCS_PROVIDER; + } +} From 7d45b8a69454b13c40c79494f027b4d3e13e2c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 12:33:48 +0200 Subject: [PATCH 27/38] chore: drop the temporary QA overrides and debug logging Removes the preview-authorization bypass, the outgoing comment URL logging, and the Origin event tracing added while verifying the integration. Also threads the event bus through the Origin webhook handlers - main's createGitDeployments() gained a Bus parameter that the endpoint predates. Co-Authored-By: Claude Fable 5 --- app/controllers/general.php | 4 -- .../Modules/VCS/Http/Origin/Events/Create.php | 43 +++++-------------- src/Appwrite/Vcs/Comment.php | 11 ----- 3 files changed, 11 insertions(+), 47 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 8343f79d9b..280a0380e1 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -247,10 +247,6 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S - If authorized, continue as if auth was not required */ $requirePreview = \is_null($apiKey) || !$apiKey->isPreviewAuthDisabled(); - // TODO: Temporarily disabled while verifying the Origin integration - - // preview deployments open without the auth cookie / console redirect. - // Remove this override to restore preview authorization. - $requirePreview = false; if ($isPreview && $requirePreview) { $cookie = $request->getCookie(COOKIE_NAME_PREVIEW, ''); $authorized = false; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index ada3fe27fd..f71cf947bf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -8,6 +8,7 @@ use Appwrite\Platform\Modules\VCS\Http\GitHub\Deployment; use Appwrite\Utopia\Request; use Appwrite\Utopia\Response; use Appwrite\Vcs\Factory as VcsFactory; +use Utopia\Bus\Bus; use Utopia\Config\Config; use Utopia\Console; use Utopia\Database\Database; @@ -52,6 +53,7 @@ class Create extends Action ->inject('response') ->inject('dbForPlatform') ->inject('authorization') + ->inject('bus') ->inject('getProjectDB') ->inject('deploymentsFactory') ->inject('platform') @@ -64,6 +66,7 @@ class Create extends Action Response $response, Database $dbForPlatform, Authorization $authorization, + Bus $bus, callable $getProjectDB, callable $deploymentsFactory, array $platform @@ -78,10 +81,6 @@ class Create extends Action $deliveryId = $request->getHeaderLine('webhook-id', ''); $timestamp = $request->getHeaderLine('webhook-timestamp', ''); - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN EVENT] delivery=' . $deliveryId . ' type=' . $event . ' timestamp=' . $timestamp . ' signature=' . (empty($signature) ? 'missing' : 'present')); - Console::log('[ORIGIN EVENT] payload: ' . \mb_strimwidth($payload, 0, 8000, '...[truncated]')); - // Origin signs the SHA-256 of ".." // with its own Ed25519 key, verified against its published JWKS rather // than a shared secret. Stale timestamps are replays. @@ -108,7 +107,6 @@ class Create extends Action } Span::add('vcs.origin.event.signature.valid', $valid); - Console::log('[ORIGIN EVENT] signature valid: ' . \var_export($valid, true)); if (!$valid) { throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid webhook payload signature. The delivery could not be verified against Origin\'s published signing keys.'); @@ -124,27 +122,17 @@ class Create extends Action '$id' => 'origin-delivery-' . $deliveryId, ]))); } catch (Duplicate) { - Console::log('[ORIGIN EVENT] duplicate delivery ' . $deliveryId . ' - already processed, acknowledging without action'); $response->json(['events' => [], 'duplicate' => true]); return; } $parsedPayloads = $vcs->getEvents($event, $payload); - Console::log('[ORIGIN EVENT] parsed ' . \count($parsedPayloads) . ' event(s): ' . \json_encode($parsedPayloads)); foreach ($parsedPayloads as $parsedPayload) { - $handler = match (true) { - $event === Origin::EVENT_PUSH => 'push', - \str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => 'pull_request', - \str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => 'installation', - default => 'none', - }; - Console::log('[ORIGIN EVENT] dispatching to handler: ' . $handler); - - match ($handler) { - 'push' => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), - 'pull_request' => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory), - 'installation' => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), + match (true) { + $event === Origin::EVENT_PUSH => $this->handlePushEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory), + \str_starts_with($event, Origin::EVENT_PULL_REQUEST . '.') => $this->handlePullRequestEvent($parsedPayload, $vcsFactory, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory), + \str_starts_with($event, Origin::EVENT_INSTALLATION . '.') => $this->handleInstallationEvent($parsedPayload, $dbForPlatform, $authorization, $getProjectDB), default => null, }; } @@ -301,6 +289,7 @@ class Create extends Action VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, + Bus $bus, callable $getProjectDB, array $platform, callable $deploymentsFactory, @@ -336,21 +325,10 @@ class Create extends Action Query::limit(100), ])); - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - Console::log('[ORIGIN EVENT] push: repo=' . $providerRepositoryId . ' branch=' . $providerBranch . ' commit=' . $providerCommitHash . ' authorEmail=' . $providerCommitAuthorEmail . ' deleted=' . \var_export($providerBranchDeleted, true)); - Console::log('[ORIGIN EVENT] push: matched ' . \count($repositories) . ' connected repositories document(s) for providerRepositoryId=' . $providerRepositoryId); - foreach ($repositories as $connected) { - Console::log('[ORIGIN EVENT] push: connected repo doc ' . $connected->getId() . ' project=' . $connected->getAttribute('projectId') . ' resource=' . $connected->getAttribute('resourceType') . '/' . $connected->getAttribute('resourceId')); - } - // Create new deployment only on push (not committed by us) and not when branch is deleted if (!\in_array($providerCommitAuthorEmail, [APP_VCS_GITHUB_EMAIL, APP_VCS_ORIGIN_EMAIL], true) && !$providerBranchDeleted) { $providerAffectedFiles = $parsedPayload['affectedFiles'] ?? []; - Console::log('[ORIGIN EVENT] push: creating git deployments'); - $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory); - Console::log('[ORIGIN EVENT] push: createGitDeployments finished'); - } else { - Console::log('[ORIGIN EVENT] push: skipped (self-commit or branch deletion)'); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthorName, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, '', $providerAffectedFiles, false, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory); } } @@ -359,6 +337,7 @@ class Create extends Action VcsFactory $vcsFactory, Database $dbForPlatform, Authorization $authorization, + Bus $bus, callable $getProjectDB, array $platform, callable $deploymentsFactory, @@ -416,7 +395,7 @@ class Create extends Action Query::orderDesc('$createdAt') ])); - $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, \strval($providerPullRequestId), $providerAffectedFiles, $external, $dbForPlatform, $authorization, $getProjectDB, $platform, $deploymentsFactory); + $this->createGitDeployments($vcs, $providerInstallationId, $repositories, $providerBranch, $providerBranchUrl, $providerRepositoryName, $providerRepositoryUrl, $providerRepositoryOwner, $providerCommitHash, $providerCommitAuthor, $providerCommitAuthorUrl, $providerCommitMessage, $providerCommitUrl, \strval($providerPullRequestId), $providerAffectedFiles, $external, $dbForPlatform, $authorization, $bus, $getProjectDB, $platform, $deploymentsFactory); } // No cleanup on close: Origin pull requests are never external, so no diff --git a/src/Appwrite/Vcs/Comment.php b/src/Appwrite/Vcs/Comment.php index 9a6ca6ba5f..328dcaa19a 100644 --- a/src/Appwrite/Vcs/Comment.php +++ b/src/Appwrite/Vcs/Comment.php @@ -2,7 +2,6 @@ namespace Appwrite\Vcs; -use Utopia\Console; use Utopia\Database\Document; use Utopia\System\System; @@ -247,15 +246,6 @@ class Comment $text .= "\n
\n\n> [!TIP]\n> {$this->tip}\n\n"; - // TODO: Temporary debug logging while the Origin integration is verified -- remove afterwards. - // Log every image and link URL the comment carries, to compare the - // scheme we send against the scheme the provider ends up rendering. - $urls = []; - if (\preg_match_all('/(?:src|srcset|href)="([^"]+)"|\]\((https?:[^)]+)\)/', $text, $matches)) { - $urls = \array_values(\array_filter(\array_merge($matches[1], $matches[2]))); - } - Console::log('[VCS COMMENT] outgoing image/link URLs: ' . \json_encode($urls)); - return $text; } @@ -266,7 +256,6 @@ class Comment return ''; } - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = $this->platform['consoleHostname'] ?? ''; From 891d169e2ff5f165cc6a29034cfe1fd3cc495fe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 12:36:15 +0200 Subject: [PATCH 28/38] fix: provide Origin credentials to every VCS-touching worker The generic worker reports build status through the adapter and the builds worker mints installation tokens, so both need the Origin client ID next to the private key. Also drops an unrelated trailing-comma change in app/config/console.php. Co-Authored-By: Claude Fable 5 --- app/config/console.php | 2 +- docker-compose.yml | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/app/config/console.php b/app/config/console.php index 8a741030b6..bf1850182a 100644 --- a/app/config/console.php +++ b/app/config/console.php @@ -57,7 +57,7 @@ $console = [ 'oAuthProviders' => [ 'githubEnabled' => true, 'githubSecret' => System::getEnv('_APP_CONSOLE_GITHUB_SECRET', ''), - 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', ''), + 'githubAppid' => System::getEnv('_APP_CONSOLE_GITHUB_APP_ID', '') ], 'smtpBaseTemplate' => APP_BRANDED_EMAIL_BASE_TEMPLATE, ]; diff --git a/docker-compose.yml b/docker-compose.yml index c07d99a1c5..aa7ca310a4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -430,6 +430,8 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET + - _APP_VCS_ORIGIN_CLIENT_ID + - _APP_VCS_ORIGIN_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_BUILDS_VOLUME - _APP_JOBS_HOST @@ -710,6 +712,7 @@ services: - _APP_VCS_BITBUCKET_CLIENT_ID - _APP_VCS_BITBUCKET_CLIENT_SECRET - _APP_VCS_BITBUCKET_WEBHOOK_SECRET + - _APP_VCS_ORIGIN_CLIENT_ID - _APP_VCS_ORIGIN_PRIVATE_KEY - _APP_VCS_WEBHOOK_URL - _APP_FUNCTIONS_TIMEOUT From 2b6f07259885b8b2182abc33b3724a2037e2ba6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 12:37:14 +0200 Subject: [PATCH 29/38] style: match the sibling .env defaults for the Origin private key Co-Authored-By: Claude Fable 5 --- .env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env b/.env index 0be2ca8563..d6a78fc36b 100644 --- a/.env +++ b/.env @@ -171,6 +171,6 @@ _APP_VCS_BITBUCKET_CLIENT_ID= _APP_VCS_BITBUCKET_CLIENT_SECRET= _APP_VCS_BITBUCKET_WEBHOOK_SECRET= _APP_VCS_ORIGIN_CLIENT_ID= -_APP_VCS_ORIGIN_PRIVATE_KEY="" +_APP_VCS_ORIGIN_PRIVATE_KEY= _APP_VCS_WEBHOOK_URL=http://appwrite/v1 _APP_WEBHOOK_MAX_FAILED_ATTEMPTS=10 From 5021eff4a4b06f0b4f628104f940502f82e09b65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 15:36:10 +0200 Subject: [PATCH 30/38] fix: drop the source-archive plumbing superseded on main Main now sources archive-less providers through the orchestrator's clone artifact (Deployments::createFromVcs(), #13285), so the merge leaves this branch's precursors with no callers: the SourceArchive presigner, the GET /v1/vcs/archives endpoint, and the vcsProvider response model that the console variables' env-style capability lists replaced. Co-Authored-By: Claude Fable 5 --- app/init/models.php | 2 - .../Modules/VCS/Http/Archives/Get.php | 114 ------------------ .../Platform/Modules/VCS/Services/Http.php | 4 - src/Appwrite/Utopia/Response.php | 1 - .../Utopia/Response/Model/VcsProvider.php | 52 -------- src/Appwrite/Vcs/SourceArchive.php | 66 ---------- 6 files changed, 239 deletions(-) delete mode 100644 src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php delete mode 100644 src/Appwrite/Utopia/Response/Model/VcsProvider.php delete mode 100644 src/Appwrite/Vcs/SourceArchive.php diff --git a/app/init/models.php b/app/init/models.php index 5156881b26..ad147b9b2b 100644 --- a/app/init/models.php +++ b/app/init/models.php @@ -228,7 +228,6 @@ use Appwrite\Utopia\Response\Model\User; use Appwrite\Utopia\Response\Model\Variable; use Appwrite\Utopia\Response\Model\VcsContent; use Appwrite\Utopia\Response\Model\VcsNamespace; -use Appwrite\Utopia\Response\Model\VcsProvider; use Appwrite\Utopia\Response\Model\VectorsDBCollection; use Appwrite\Utopia\Response\Model\Webhook; @@ -266,7 +265,6 @@ Response::setModel(new ProviderRepositoryFrameworkList()); Response::setModel(new ProviderRepositoryRuntimeList()); Response::setModel(new VcsNamespace()); Response::setModel(new BaseList('VCS Namespaces List', Response::MODEL_VCS_NAMESPACE_LIST, 'namespaces', Response::MODEL_VCS_NAMESPACE)); -Response::setModel(new VcsProvider()); Response::setModel(new BaseList('Branches List', Response::MODEL_BRANCH_LIST, 'branches', Response::MODEL_BRANCH)); Response::setModel(new BaseList('Frameworks List', Response::MODEL_FRAMEWORK_LIST, 'frameworks', Response::MODEL_FRAMEWORK)); Response::setModel(new BaseList('Runtimes List', Response::MODEL_RUNTIME_LIST, 'runtimes', Response::MODEL_RUNTIME)); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php deleted file mode 100644 index 4951fa1a67..0000000000 --- a/src/Appwrite/Platform/Modules/VCS/Http/Archives/Get.php +++ /dev/null @@ -1,114 +0,0 @@ -setHttpMethod(Action::HTTP_REQUEST_METHOD_GET) - ->setHttpPath('/v1/vcs/archives') - ->desc('Download repository archive') - ->groups(['api', 'vcs']) - ->label('scope', 'public') - ->param('provider', '', new Text(64, 0), 'VCS provider the repository lives on.') - ->param('installation', '', new Text(256, 0), 'Provider installation ID the credentials belong to.') - ->param('owner', '', new Text(256, 0), 'Owner of the repository.') - ->param('repository', '', new Text(256, 0), 'Name of the repository.') - ->param('ref', '', new Text(256, 0), 'Branch, tag or commit to archive.') - ->param('expires', 0, new Integer(loose: true, bits: 64), 'Unix timestamp the link stops working at.') - ->param('signature', '', new Text(128, 0), 'HMAC binding every other parameter.') - ->inject('response') - ->inject('vcsFactory') - ->callback($this->action(...)); - } - - /** - * Packages a repository the provider cannot archive itself. The link is - * minted by Appwrite\Vcs\SourceArchive for the jobs-service source fetch, - * and the HMAC is the whole authorization: it commits to every parameter - * and an expiry, and only Appwrite can produce it. - */ - public function action( - string $provider, - string $installation, - string $owner, - string $repository, - string $ref, - int $expires, - string $signature, - Response $response, - VcsFactory $vcsFactory, - ) { - $expected = SourceArchive::signature($provider, $installation, $owner, $repository, $ref, $expires); - - if (empty($signature) || !\hash_equals($expected, $signature)) { - throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'Invalid archive signature.'); - } - - if ($expires < \time()) { - throw new Exception(Exception::GENERAL_ACCESS_FORBIDDEN, 'The archive link has expired.'); - } - - $vcs = $vcsFactory->fromInstallation(new Document([ - 'provider' => $provider, - 'providerInstallationId' => $installation, - ])); - - // Git-forge archives wrap the tree in a "{repo}-{ref}/" root the - // source fetch strips, so this one has to match that shape. - $directory = '/tmp/vcs-archives/' . \uniqid(); - $wrapper = $repository . '-' . \substr(\preg_replace('/[^A-Za-z0-9._-]/', '-', $ref) ?? '', 0, 64); - $archive = $directory . '/archive.tar.gz'; - - $cloneType = \preg_match('/^[0-9a-f]{40}([0-9a-f]{24})?$/i', $ref) ? Git::CLONE_TYPE_COMMIT : Git::CLONE_TYPE_BRANCH; - $cloneCommand = $vcs->generateCloneCommand($owner, $repository, $ref, $cloneType, $directory . '/' . $wrapper, '*'); - - try { - $stdout = ''; - $stderr = ''; - - if (Console::execute($cloneCommand, '', $stdout, $stderr) !== 0) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to download the repository.'); - } - - $packCommand = 'tar --exclude=' . \escapeshellarg($wrapper . '/.git') . ' -czf ' . \escapeshellarg($archive) . ' -C ' . \escapeshellarg($directory) . ' ' . \escapeshellarg($wrapper); - if (Console::execute($packCommand, '', $stdout, $stderr) !== 0) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to package the repository.'); - } - - $contents = \file_get_contents($archive); - if ($contents === false) { - throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to read the repository archive.'); - } - - $response - ->setContentType('application/gzip') - ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') - ->addHeader('Content-Disposition', 'attachment; filename="' . $wrapper . '.tar.gz"') - ->send($contents); - } finally { - Console::execute('rm -rf ' . \escapeshellarg($directory), '', $stdout, $stderr); - } - } -} diff --git a/src/Appwrite/Platform/Modules/VCS/Services/Http.php b/src/Appwrite/Platform/Modules/VCS/Services/Http.php index 55e268980c..71498db484 100644 --- a/src/Appwrite/Platform/Modules/VCS/Services/Http.php +++ b/src/Appwrite/Platform/Modules/VCS/Services/Http.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Services; -use Appwrite\Platform\Modules\VCS\Http\Archives\Get as GetArchive; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Authorize\Get as GetBitbucketAuthorize; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Callback\Get as GetBitbucketCallback; use Appwrite\Platform\Modules\VCS\Http\Bitbucket\Events\Create as CreateBitbucketEvent; @@ -64,9 +63,6 @@ class Http extends Service $this->addAction(DeleteInstallation::getName(), new DeleteInstallation()); $this->addAction(ListNamespaces::getName(), new ListNamespaces()); - // Archives - $this->addAction(GetArchive::getName(), new GetArchive()); - // Repositories $this->addAction(CreateRepository::getName(), new CreateRepository()); $this->addAction(GetRepository::getName(), new GetRepository()); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 4a2b7aaead..bfd02dafe1 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -195,7 +195,6 @@ class Response extends SwooleResponse public const MODEL_PROVIDER_REPOSITORY_RUNTIME_LIST = 'providerRepositoryRuntimeList'; public const MODEL_VCS_NAMESPACE = 'vcsNamespace'; public const MODEL_VCS_NAMESPACE_LIST = 'vcsNamespaceList'; - public const MODEL_VCS_PROVIDER = 'vcsProvider'; public const MODEL_BRANCH = 'branch'; public const MODEL_BRANCH_LIST = 'branchList'; public const MODEL_DETECTION_FRAMEWORK = 'detectionFramework'; diff --git a/src/Appwrite/Utopia/Response/Model/VcsProvider.php b/src/Appwrite/Utopia/Response/Model/VcsProvider.php deleted file mode 100644 index f7cd552603..0000000000 --- a/src/Appwrite/Utopia/Response/Model/VcsProvider.php +++ /dev/null @@ -1,52 +0,0 @@ -addRule('name', [ - 'type' => self::TYPE_STRING, - 'description' => 'VCS (Version Control System) provider name.', - 'default' => '', - 'example' => 'github', - ]) - ->addRule('supportForRepositoryCreation', [ - 'type' => self::TYPE_BOOLEAN, - 'description' => 'Defines if the provider allows creating repositories. Some providers reserve repository creation for their own UI.', - 'default' => false, - 'example' => true, - ]) - ->addRule('supportForPublicRepositories', [ - 'type' => self::TYPE_BOOLEAN, - 'description' => 'Defines if the provider can host repositories that are readable without authentication.', - 'default' => false, - 'example' => true, - ]); - } - - /** - * Get Name - * - * @return string - */ - public function getName(): string - { - return 'VcsProvider'; - } - - /** - * Get Type - * - * @return string - */ - public function getType(): string - { - return Response::MODEL_VCS_PROVIDER; - } -} diff --git a/src/Appwrite/Vcs/SourceArchive.php b/src/Appwrite/Vcs/SourceArchive.php deleted file mode 100644 index 3e1093fc4c..0000000000 --- a/src/Appwrite/Vcs/SourceArchive.php +++ /dev/null @@ -1,66 +0,0 @@ - $platform - * @return array{string, array} The source URL and the headers to fetch it with - */ - public static function presign(Git $vcs, string $providerInstallationId, string $owner, string $repository, string $ref, array $platform): array - { - if ($vcs->supportsRepositoryArchives()) { - return [ - $vcs->getRepositoryPresignedUrl($owner, $repository, $ref), - $vcs->getRepositoryPresignedUrlHeaders(), - ]; - } - - $expires = \time() + self::TTL; - - $params = [ - 'provider' => $vcs->getName(), - 'installation' => $providerInstallationId, - 'owner' => $owner, - 'repository' => $repository, - 'ref' => $ref, - 'expires' => $expires, - 'signature' => self::signature($vcs->getName(), $providerInstallationId, $owner, $repository, $ref, $expires), - ]; - - // The jobs-service reaches Appwrite over the internal Docker network - // when configured, the same way its presigned callback URLs do. - $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; - $endpoint = \rtrim(System::getEnv('_APP_JOBS_ENDPOINT', $protocol . '://' . ($platform['apiHostname'] ?? '')), '/'); - - return [$endpoint . '/v1/vcs/archives?' . \http_build_query($params), []]; - } - - public static function signature(string $provider, string $providerInstallationId, string $owner, string $repository, string $ref, int $expires): string - { - return \hash_hmac( - 'sha256', - \implode("\n", [$provider, $providerInstallationId, $owner, $repository, $ref, $expires]), - System::getEnv('_APP_OPENSSL_KEY_V1', '') - ); - } -} From d761597be18cd063f58730104c2d2ba4648ea691 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 15:40:43 +0200 Subject: [PATCH 31/38] refactor: drop Origin's duplicated OAuth2 registration The clientId/clientSecret entries and the oauth2 builder repeated appId and privateKey only so refreshForInstallation() could resolve an OAuth2 client it never uses: for a non-personal installation refresh() is a no-op, and every Origin installation is non-personal. The refresh now returns early for app installations - a GitHub App or Origin app authenticates per request with an app-signed JWT and has no personal tokens to refresh - so Origin registers only what the Factory reads, and oauth2FromProvider('origin') fails loudly instead of handing out a client with duplicated credentials. The install handshake keeps constructing its own Auth\OAuth2\Cursor, which needs the callback, state, and scopes the factory builder could never supply. Co-Authored-By: Claude Fable 5 --- app/config/vcs.php | 18 ++++++------------ src/Appwrite/Vcs/InstallationTokens.php | 7 +++++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/app/config/vcs.php b/app/config/vcs.php index 141b987b42..49f6d3b57e 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -5,7 +5,6 @@ */ use Appwrite\Auth\OAuth2\Bitbucket as OAuth2Bitbucket; -use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Auth\OAuth2\Gitea as OAuth2Gitea; use Appwrite\Auth\OAuth2\Github as OAuth2Github; use Appwrite\Auth\OAuth2\Gitlab as OAuth2Gitlab; @@ -66,20 +65,15 @@ return [ ], 'origin' => [ 'adapter' => Origin::class, - // Origin apps authenticate like GitHub Apps (an app-signed JWT minting - // installation tokens), so the same Ed25519 private key doubles as the - // OAuth2 secret. Auth\OAuth2\Cursor expects it JSON-wrapped, the way - // Apple does. - 'oauth2' => fn (string $clientId, string $clientSecret, string $endpoint) => new OAuth2Cursor($clientId, \json_encode([ - 'privateKey' => $clientSecret, - ]), ''), + // No 'oauth2': Origin has no OAuth2 user flow. Installs are approved + // on Cursor and confirmed by a signed receipt, and the dedicated + // Origin endpoints construct their own Auth\OAuth2\Cursor for that + // handshake. 'variables' => [ - 'clientId' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_CLIENT_ID'], - // Factory::fromInstallation() reads appId + privateKey; the app id - // and OAuth2 client id are the same value on Origin. + // Factory::fromInstallation() reads appId + privateKey; Origin's + // app id doubles as its client id. 'appId' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_CLIENT_ID'], 'privateKey' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_PRIVATE_KEY'], - 'clientSecret' => ['required' => true, 'envVariable' => '_APP_VCS_ORIGIN_PRIVATE_KEY'], // No webhookSecret: Origin signs deliveries with its own Ed25519 // key, verified against its published JWKS instead of a shared secret. ], diff --git a/src/Appwrite/Vcs/InstallationTokens.php b/src/Appwrite/Vcs/InstallationTokens.php index c192f6c07b..043ebdbb37 100644 --- a/src/Appwrite/Vcs/InstallationTokens.php +++ b/src/Appwrite/Vcs/InstallationTokens.php @@ -17,6 +17,13 @@ class InstallationTokens */ public function refreshForInstallation(Document $installation, Database $dbForPlatform, Factory $vcsFactory): Document { + // Only personal installations carry OAuth tokens to refresh. An app + // installation (a GitHub App, an Origin app) authenticates per request + // with an app-signed JWT and may have no OAuth2 client to resolve at all. + if (!$installation->getAttribute('personal', false)) { + return $installation; + } + $provider = $installation->getAttribute('provider', 'github'); return $this->refresh($installation, $dbForPlatform, $vcsFactory->oauth2FromProvider($provider)); From 38aa35774a85a4908211aedeaa2ed5d058efde72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 15:41:32 +0200 Subject: [PATCH 32/38] refactor: inline the Origin installation description The one-endpoint markdown file under docs/references gives way to the heredoc description the newer modules use. Co-Authored-By: Claude Fable 5 --- docs/references/vcs/create-origin-installation.md | 1 - .../Platform/Modules/VCS/Http/Origin/Authorize/Get.php | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) delete mode 100644 docs/references/vcs/create-origin-installation.md diff --git a/docs/references/vcs/create-origin-installation.md b/docs/references/vcs/create-origin-installation.md deleted file mode 100644 index 264763aae1..0000000000 --- a/docs/references/vcs/create-origin-installation.md +++ /dev/null @@ -1 +0,0 @@ -Begin Appwrite's Origin app installation to set up version control integration. This endpoint responds with a redirect URL to the Origin app's installation page on Cursor. The Origin app must be configured in your environment for this endpoint to work. diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php index 1df76d5481..ec491add54 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php @@ -53,7 +53,9 @@ class Get extends Action namespace: 'vcs', group: 'installations', name: 'createOriginInstallation', - description: '/docs/references/vcs/create-origin-installation.md', + description: << Date: Wed, 19 Aug 2026 15:41:38 +0200 Subject: [PATCH 33/38] revert: keep the build-status catch as main has it Co-Authored-By: Claude Fable 5 --- src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php index 6bf69c4aa0..bd40a7aa03 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Jobs.php @@ -20,7 +20,6 @@ use Appwrite\Utopia\Response\Model\Deployment; use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Bus\Bus; use Utopia\Cache\Cache; -use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -524,11 +523,8 @@ class Jobs extends Action $dbForPlatform, $platform, ); - } catch (\Throwable $error) { - // Best-effort — never fails the build. But say what was lost: a - // swallowed failure here leaves the provider's check and comment - // frozen at their last state with nothing to debug from. - Console::warning('Failed to report build status to VCS provider for deployment ' . $deployment->getId() . ': ' . $error->getMessage()); + } catch (\Throwable) { + // Best-effort — never fails the build. } } From bf77c10878c381905f78c3c001079107dfd439a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 15:41:53 +0200 Subject: [PATCH 34/38] fix: request repository:metadata:read explicitly The install scopes relied on Origin granting metadata reads implicitly; listing every scope the integration touches keeps the grant explicit. Co-Authored-By: Claude Fable 5 --- .../Platform/Modules/VCS/Http/Origin/Authorize/Get.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php index ec491add54..8b7020c8c4 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php @@ -20,12 +20,13 @@ class Get extends Action use HTTP; /** - * Everything the integration touches: cloning sources, pushing templates, - * deploying from pull requests, commenting on them, and reporting build - * checks. repository:metadata:read is granted implicitly. + * Everything the integration touches: reading repository metadata, + * cloning sources, pushing templates, deploying from pull requests, + * commenting on them, and reporting build checks. */ public const SCOPES = [ 'repository:contents:read', + 'repository:metadata:read', 'repository:contents:write', 'repository:pull_requests:read', 'repository:pull_requests:write', From d1cc63b39057bbcbb83499da3ef1ac790cc0c3c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 15:51:09 +0200 Subject: [PATCH 35/38] refactor: fetch Origin signing keys through Utopia\Fetch Raw curl gives way to the Fetch client the rest of the codebase uses for outgoing requests, keeping the 15-second timeout, the non-200 guard, and the warn-don't-fail error handling. Co-Authored-By: Claude Fable 5 --- .../Modules/VCS/Http/Origin/Events/Create.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index f71cf947bf..ced2605ecb 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -17,6 +17,7 @@ use Utopia\Database\Exception\Duplicate; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\DSN\DSN; +use Utopia\Fetch\Client as FetchClient; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; @@ -155,13 +156,11 @@ class Create extends Action $keys = []; try { - $ch = \curl_init('https://api.cursor.com/v1/origin/keys'); - \curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - \curl_setopt($ch, CURLOPT_TIMEOUT, 15); - $body = \curl_exec($ch); - \curl_close($ch); + $client = new FetchClient(); + $client->setTimeout(15000); + $response = $client->fetch('https://api.cursor.com/v1/origin/keys', FetchClient::METHOD_GET); - $jwks = \is_string($body) ? \json_decode($body, true) : null; + $jwks = $response->getStatusCode() === 200 ? $response->json() : null; foreach (\is_array($jwks['keys'] ?? null) ? $jwks['keys'] : [] as $key) { if (($key['kty'] ?? '') === 'OKP' && ($key['crv'] ?? '') === 'Ed25519' && !empty($key['x'])) { $keys[] = \strval($key['x']); From 5aee65e6d111075db963d51a2e08b6b04a1879c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 16:04:09 +0200 Subject: [PATCH 36/38] refactor: verify Origin installs through the adapter utopia-php/vcs 5.2.2 owns the install handshake, so the OAuth2-shaped Cursor client - Origin has no OAuth2 - goes away: Authorize builds the install URL with the adapter's getInstallUrl(), the callback verifies receipts with verifyReceipt(), and the events endpoint reads signing keys from getSigningKeys() instead of fetching the JWKS by hand. The worker-lifetime key cache stays, since the adapter memoizes only per instance. Co-Authored-By: Claude Fable 5 --- app/config/vcs.php | 5 +- composer.lock | 12 +- src/Appwrite/Auth/OAuth2/Cursor.php | 333 ------------------ .../Modules/VCS/Http/Origin/Authorize/Get.php | 17 +- .../Modules/VCS/Http/Origin/Callback/Get.php | 11 +- .../Modules/VCS/Http/Origin/Events/Create.php | 24 +- 6 files changed, 28 insertions(+), 374 deletions(-) delete mode 100644 src/Appwrite/Auth/OAuth2/Cursor.php diff --git a/app/config/vcs.php b/app/config/vcs.php index 49f6d3b57e..f21d54c803 100644 --- a/app/config/vcs.php +++ b/app/config/vcs.php @@ -66,9 +66,8 @@ return [ 'origin' => [ 'adapter' => Origin::class, // No 'oauth2': Origin has no OAuth2 user flow. Installs are approved - // on Cursor and confirmed by a signed receipt, and the dedicated - // Origin endpoints construct their own Auth\OAuth2\Cursor for that - // handshake. + // on Cursor and confirmed by a signed receipt, a handshake the + // adapter itself owns (getInstallUrl / verifyReceipt). 'variables' => [ // Factory::fromInstallation() reads appId + privateKey; Origin's // app id doubles as its client id. diff --git a/composer.lock b/composer.lock index fd8de8542f..2ab68f778c 100644 --- a/composer.lock +++ b/composer.lock @@ -5068,16 +5068,16 @@ }, { "name": "utopia-php/vcs", - "version": "5.2.1", + "version": "5.2.2", "source": { "type": "git", "url": "https://github.com/utopia-php/vcs.git", - "reference": "ee3928969d6c654fad44fad7f38cc96383b031fa" + "reference": "758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/vcs/zipball/ee3928969d6c654fad44fad7f38cc96383b031fa", - "reference": "ee3928969d6c654fad44fad7f38cc96383b031fa", + "url": "https://api.github.com/repos/utopia-php/vcs/zipball/758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e", + "reference": "758eb3ab3c32a607b5aca78b1c9bae4ab1ba9b6e", "shasum": "" }, "require": { @@ -5128,10 +5128,10 @@ "vcs" ], "support": { - "source": "https://github.com/utopia-php/vcs/tree/5.2.1", + "source": "https://github.com/utopia-php/vcs/tree/5.2.2", "issues": "https://github.com/utopia-php/vcs/issues" }, - "time": "2026-08-19T12:49:54+00:00" + "time": "2026-08-19T14:01:44+00:00" }, { "name": "utopia-php/websocket", diff --git a/src/Appwrite/Auth/OAuth2/Cursor.php b/src/Appwrite/Auth/OAuth2/Cursor.php deleted file mode 100644 index 53b2232ad3..0000000000 --- a/src/Appwrite/Auth/OAuth2/Cursor.php +++ /dev/null @@ -1,333 +0,0 @@ - $this->appID, - ]; - - if (empty($this->getScopes())) { - // Without an explicit scope list the install page rejects the - // request ("requested_scopes requires at least one scope") unless - // told to read the scopes from the app's registered metadata. - $params['source'] = 'app-metadata'; - } else { - $params['scope'] = \implode(' ', $this->getScopes()); - } - - // The callback must exactly match a redirect URI registered on the - // app; Cursor echoes state back as the receipt's `state` claim. - if (!empty($this->callback)) { - $params['redirect_uri'] = $this->callback; - } - - if (!empty($this->state)) { - $params['state'] = \json_encode($this->state); - } - - // The Origin console still lives under the /codebase path on cursor.com. - return 'https://cursor.com/codebase/apps/install?' . \http_build_query($params); - } - - /** - * Verifies an installation receipt JWT against Cursor's JWKS and returns - * its claims. The `sub` claim is the installation ID, `state` echoes the - * state given to the install URL, and `namespace_id` is the workspace. - * - * @return array - * @throws Exception - */ - public function verifyReceipt(string $receipt): array - { - $segments = \explode('.', $receipt); - - if (\count($segments) !== 3) { - throw new Exception('Installation receipt is not a JWT'); - } - - [$headerSegment, $claimsSegment, $signatureSegment] = $segments; - - $header = \json_decode($this->base64UrlDecode($headerSegment), true); - $claims = \json_decode($this->base64UrlDecode($claimsSegment), true); - $signature = $this->base64UrlDecode($signatureSegment); - - if (!\is_array($header) || !\is_array($claims)) { - throw new Exception('Installation receipt is malformed'); - } - - if (($header['alg'] ?? '') !== 'EdDSA') { - throw new Exception('Installation receipt has an unexpected algorithm'); - } - - // Receipts are documented to carry this media type; tolerate its - // absence, but never another Cursor-signed token kind in its place. - if (isset($header['typ']) && $header['typ'] !== 'origin-installation-receipt+jwt') { - throw new Exception('Installation receipt has an unexpected type'); - } - - $key = $this->getPublicKey($header['kid'] ?? ''); - - if ( - \strlen($signature) !== SODIUM_CRYPTO_SIGN_BYTES - || !\sodium_crypto_sign_verify_detached($signature, $headerSegment . '.' . $claimsSegment, $key) - ) { - throw new Exception('Installation receipt signature is invalid'); - } - - if (($claims['iss'] ?? '') !== $this->endpoint) { - throw new Exception('Installation receipt issuer is invalid'); - } - - if (($claims['aud'] ?? '') !== $this->appID) { - throw new Exception('Installation receipt audience does not match the configured client ID'); - } - - $now = \time(); - $leeway = 60; - - // Receipts are short-lived, single-use artifacts (about a five-minute - // window per Cursor's verification rules). - if (isset($claims['exp']) && $now >= (int)$claims['exp'] + $leeway) { - throw new Exception('Installation receipt has expired'); - } - - if (isset($claims['nbf']) && $now < (int)$claims['nbf'] - $leeway) { - throw new Exception('Installation receipt is not yet valid'); - } - - if (isset($claims['iat']) && $now < (int)$claims['iat'] - $leeway) { - throw new Exception('Installation receipt is issued in the future'); - } - - if (empty($claims['sub'])) { - throw new Exception('Installation receipt is missing the installation ID'); - } - - return $claims; - } - - /** - * App JWT authenticating this app to Cursor's API, signed with the app's - * Ed25519 private key. Used as a Bearer token for app-level endpoints, - * most importantly minting installation access tokens. - */ - public function getAppJWT(): string - { - $now = \time(); - - $headerSegment = $this->base64UrlEncode(\json_encode([ - 'alg' => 'EdDSA', - 'kid' => $this->appID, - 'typ' => 'JWT', - ])); - - $claimsSegment = $this->base64UrlEncode(\json_encode([ - 'iss' => $this->appID, - 'aud' => 'origin-apps', - 'iat' => $now, - 'exp' => $now + 300, - ])); - - $signature = \sodium_crypto_sign_detached($headerSegment . '.' . $claimsSegment, $this->getSigningKey()); - - return $headerSegment . '.' . $claimsSegment . '.' . $this->base64UrlEncode($signature); - } - - /** - * There is no authorization-code exchange: the "code" is the installation - * ID from the receipt's `sub` claim, exchanged for a short-lived (at most - * 15 minutes) installation access token. An empty or omitted scope list - * inherits the full installation grant. - * - * @param string $code - * - * @return array - */ - protected function getTokens(string $code): array - { - if (empty($this->tokens)) { - $payload = empty($this->getScopes()) - ? '{}' - : \json_encode(['scopes' => $this->getScopes()]); - - $response = \json_decode($this->request( - 'POST', - $this->endpoint . '/app/installations/' . \urlencode($code) . '/access_tokens', - [ - 'Authorization: Bearer ' . $this->getAppJWT(), - 'Content-Type: application/json', - ], - $payload - ), true); - - $this->tokens = [ - 'access_token' => $response['token'] ?? '', - 'expires_in' => isset($response['expiresAt']) - ? \max(0, (int)\strtotime($response['expiresAt']) - \time()) - : 0, - ]; - } - - return $this->tokens; - } - - /** - * Installation tokens cannot be refreshed -- a new one is minted from the - * installation ID with the app JWT. - * - * @param string $refreshToken - * - * @return array - */ - public function refreshTokens(string $refreshToken): array - { - $this->tokens = []; - - return $this->getTokens($refreshToken); - } - - /** - * Cursor Origin apps act as an installation, never as a user, so there is - * no user identity behind an access token. - */ - public function getUserID(string $accessToken): string - { - return ''; - } - - public function getUserEmail(string $accessToken): string - { - return ''; - } - - public function isEmailVerified(string $accessToken): bool - { - return false; - } - - public function getUserName(string $accessToken): string - { - return ''; - } - - /** - * Ed25519 public key for a JWKS key ID. - * - * @throws Exception - */ - protected function getPublicKey(string $kid): string - { - if (empty($this->jwks)) { - $this->jwks = \json_decode($this->request('GET', $this->endpoint . '/keys'), true)['keys'] ?? []; - } - - foreach ($this->jwks as $key) { - if ( - ($key['kid'] ?? '') === $kid - && ($key['kty'] ?? '') === 'OKP' - && ($key['crv'] ?? '') === 'Ed25519' - ) { - $publicKey = $this->base64UrlDecode($key['x'] ?? ''); - - if (\strlen($publicKey) === SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) { - return $publicKey; - } - } - } - - throw new Exception('Installation receipt is signed with an unknown key'); - } - - /** - * Ed25519 secret key derived from the PKCS#8 PEM in the secret's - * `privateKey` field. - * - * @throws Exception - */ - protected function getSigningKey(): string - { - $secret = \json_decode($this->appSecret, true); - $pem = \is_array($secret) ? ($secret['privateKey'] ?? '') : ''; - - if (empty($pem) || !\is_string($pem)) { - throw new Exception('Private key is not configured'); - } - - // Keys configured through environment variables carry literal \n - // sequences instead of newlines. - $pem = \str_replace(['\n', '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----'], '', $pem); - $der = \base64_decode(\preg_replace('/\s+/', '', $pem) ?? '', true); - - // RFC 8410 PKCS#8 wraps the 32-byte Ed25519 seed in a nested octet - // string tagged 0x04 0x22 0x04 0x20. - $offset = \is_string($der) ? \strpos($der, "\x04\x22\x04\x20") : false; - - if ($offset === false || \strlen($der) < $offset + 4 + SODIUM_CRYPTO_SIGN_SEEDBYTES) { - throw new Exception('Private key is not a PKCS#8 Ed25519 key'); - } - - $seed = \substr($der, $offset + 4, SODIUM_CRYPTO_SIGN_SEEDBYTES); - - return \sodium_crypto_sign_secretkey(\sodium_crypto_sign_seed_keypair($seed)); - } - - protected function base64UrlEncode(string $data): string - { - return \rtrim(\strtr(\base64_encode($data), '+/', '-_'), '='); - } - - protected function base64UrlDecode(string $data): string - { - $remainder = \strlen($data) % 4; - if ($remainder > 0) { - $data .= \str_repeat('=', 4 - $remainder); - } - - return (string)\base64_decode(\strtr($data, '-_', '+/'), true); - } -} diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php index 8b7020c8c4..58c12103ac 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Authorize/Get.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\Origin\Authorize; -use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Extend\Exception; use Appwrite\Platform\Action; use Appwrite\SDK\AuthType; @@ -11,6 +10,7 @@ use Appwrite\SDK\Method; use Appwrite\SDK\MethodType; use Appwrite\SDK\Response as SDKResponse; use Appwrite\Utopia\Response; +use Appwrite\Vcs\Factory as VcsFactory; use Utopia\Database\Document; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -75,6 +75,7 @@ class Get extends Action ->inject('project') ->inject('platform') ->inject('vcsConfigured') + ->inject('vcsFactory') ->callback($this->action(...)); } @@ -85,6 +86,7 @@ class Get extends Action Document $project, array $platform, callable $vcsConfigured, + VcsFactory $vcsFactory, ) { if (!$vcsConfigured('origin')) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'This endpoint is not implemented on this server. Please configure VCS (Version Control System) variables in .env file.'); @@ -104,19 +106,18 @@ class Get extends Action $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $callback = $protocol . '://' . ($platform['consoleHostname'] ?? '') . '/v1/vcs/origin/callback'; - // The Cursor adapter expects its secret as a JSON object; starting an - // install only needs the app ID, so the key may be left out here. - $oauth2 = new OAuth2Cursor( + /** @var \Utopia\VCS\Adapter\Git\Origin $vcs */ + $vcs = $vcsFactory->fromProvider('origin'); + $installUrl = $vcs->getInstallUrl( System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), - \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), + self::SCOPES, $callback, - $state, - self::SCOPES + \json_encode($state) ?: '' ); $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') ->addHeader('Pragma', 'no-cache') - ->redirect($oauth2->getLoginURL()); + ->redirect($installUrl); } } 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 17ae23e0eb..6c51fa124e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Callback/Get.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\VCS\Http\Origin\Callback; -use Appwrite\Auth\OAuth2\Cursor as OAuth2Cursor; use Appwrite\Extend\Exception; use Appwrite\Platform\Permission as AppwritePermission; use Appwrite\Utopia\Response; @@ -62,15 +61,11 @@ class Get extends Action // Authenticate the callback before trusting anything else on it: the // endpoint is public and Origin performs no token exchange, so the // EdDSA-signed receipt is the only proof the request came from Cursor. - // The Cursor adapter expects its secret as a JSON object. - $oauth2 = new OAuth2Cursor( - System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', ''), - \json_encode(['privateKey' => System::getEnv('_APP_VCS_ORIGIN_PRIVATE_KEY', '')]), - '' - ); + /** @var \Utopia\VCS\Adapter\Git\Origin $vcs */ + $vcs = $vcsFactory->fromProvider('origin'); try { - $receiptClaims = $oauth2->verifyReceipt($installationReceipt); + $receiptClaims = $vcs->verifyReceipt($installationReceipt, System::getEnv('_APP_VCS_ORIGIN_CLIENT_ID', '')); } catch (\Throwable $e) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Could not verify the Origin installation receipt. Please restart the installation from the Appwrite Console.'); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php index ced2605ecb..0c9b096e99 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Origin/Events/Create.php @@ -17,7 +17,6 @@ use Utopia\Database\Exception\Duplicate; use Utopia\Database\Query; use Utopia\Database\Validator\Authorization; use Utopia\DSN\DSN; -use Utopia\Fetch\Client as FetchClient; use Utopia\Platform\Scope\HTTP; use Utopia\Span\Span; use Utopia\System\System; @@ -72,6 +71,7 @@ class Create extends Action callable $deploymentsFactory, array $platform ) { + /** @var Origin $vcs */ $vcs = $vcsFactory->fromProvider('origin'); $event = $request->getHeaderLine($vcs->getEventHeaderName(), ''); @@ -89,7 +89,7 @@ class Create extends Action if (!empty($deliveryId) && \ctype_digit($timestamp) && \abs(\time() - (int) $timestamp) <= 300) { $signedContent = $deliveryId . '.' . $timestamp . '.' . $payload; - foreach ($this->signingKeys() as $publicKey) { + foreach ($this->signingKeys($vcs) as $publicKey) { if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) { $valid = true; break; @@ -98,7 +98,7 @@ class Create extends Action // The key set may have rotated since it was cached. if (!$valid) { - foreach ($this->signingKeys(refresh: true) as $publicKey) { + foreach ($this->signingKeys($vcs, refresh: true) as $publicKey) { if ($vcs->validateWebhookEvent($signedContent, $signature, $publicKey)) { $valid = true; break; @@ -142,12 +142,13 @@ class Create extends Action } /** - * Origin's active Ed25519 signing keys as base64url raw key material, the - * shape the adapter's validateWebhookEvent() accepts. + * Origin's active signing keys, from the adapter, cached for the worker's + * lifetime - the adapter memoizes only per instance, and a new adapter is + * built for every delivery. * * @return array */ - protected function signingKeys(bool $refresh = false): array + protected function signingKeys(Origin $vcs, bool $refresh = false): array { if (!$refresh && self::$signingKeys !== null) { return self::$signingKeys; @@ -156,16 +157,7 @@ class Create extends Action $keys = []; try { - $client = new FetchClient(); - $client->setTimeout(15000); - $response = $client->fetch('https://api.cursor.com/v1/origin/keys', FetchClient::METHOD_GET); - - $jwks = $response->getStatusCode() === 200 ? $response->json() : null; - foreach (\is_array($jwks['keys'] ?? null) ? $jwks['keys'] : [] as $key) { - if (($key['kty'] ?? '') === 'OKP' && ($key['crv'] ?? '') === 'Ed25519' && !empty($key['x'])) { - $keys[] = \strval($key['x']); - } - } + $keys = $vcs->getSigningKeys($refresh); } catch (\Throwable $e) { Console::warning('Failed to fetch Origin signing keys: ' . $e->getMessage()); } From 27d92edd34150ff3bbf4c65014505cebb13c65fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 16:17:55 +0200 Subject: [PATCH 37/38] Upgrade orchestrator to support clone artifact for cursor --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index aa7ca310a4..abb56e3c21 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1506,7 +1506,7 @@ services: options: max-file: "5" max-size: 10m - image: ghcr.io/open-runtimes/orchestrator/jobs-service:0.18.0 + image: ghcr.io/open-runtimes/orchestrator/jobs-service:1.3.0 restart: unless-stopped user: root networks: From a746d26d41d7c50d4610cf6940bb840173f34a40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Wed, 19 Aug 2026 16:21:21 +0200 Subject: [PATCH 38/38] test: a VCS registry entry owns an OAuth2 builder only when it has a flow The registry test required a callable builder on every provider, an invariant Origin deliberately broke: it has no OAuth2 user flow, so it registers none. The loop now checks the builder only where one exists, and a dedicated test pins Origin's absence - oauth2FromProvider('origin') must fail loudly rather than hand out a half-configured client. Co-Authored-By: Claude Fable 5 --- tests/unit/Vcs/FactoryTest.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tests/unit/Vcs/FactoryTest.php b/tests/unit/Vcs/FactoryTest.php index 9c758593bd..671aec8ff9 100644 --- a/tests/unit/Vcs/FactoryTest.php +++ b/tests/unit/Vcs/FactoryTest.php @@ -31,7 +31,11 @@ final class FactoryTest extends TestCase foreach ($registry as $key => $entry) { $this->assertTrue(\is_subclass_of($entry['adapter'], Git::class), "Adapter for '{$key}' must extend Git"); - $this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable"); + // A provider with an OAuth2 user flow registers a builder; an + // app-only provider (Origin) deliberately registers none. + if (isset($entry['oauth2'])) { + $this->assertIsCallable($entry['oauth2'], "OAuth2 builder for '{$key}' must be callable"); + } $this->assertNotEmpty($entry['variables'], "Variables missing for '{$key}'"); foreach ($entry['variables'] as $name => $variable) { $this->assertStringStartsWith('_APP_VCS_', $variable['envVariable'] ?? '', "Env variable for '{$name}' missing or invalid for '{$key}'"); @@ -39,6 +43,21 @@ final class FactoryTest extends TestCase } } + public function testOriginRegistersNoOAuth2(): void + { + // Origin has no OAuth2 user flow: installs are approved on Cursor and + // confirmed by a signed receipt the adapter verifies itself, so the + // registry deliberately carries no builder and oauth2FromProvider() + // fails loudly instead of handing out a half-configured client. + $registry = Config::getParam('vcs', []); + $this->assertArrayHasKey('origin', $registry); + $this->assertArrayNotHasKey('oauth2', $registry['origin']); + + $factory = new Factory($this->cache(), $registry); + $this->expectException(Exception::class); + $factory->oauth2FromProvider('origin'); + } + public function testOauth2FromProviderUnknownThrows(): void { $factory = new Factory($this->cache(), ['github' => $this->githubEntry()]);