From 40f4358f677969df2ff25fad0f70527f45d02a16 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 10:23:47 +0530 Subject: [PATCH 01/11] fix(vcs): sign the GitHub App installation state /v1/vcs/github/callback is public and took the projectId and both redirect URLs straight out of the state parameter, so anyone intercepting or forging the redirect could change which project an installation attaches to, or point the redirects anywhere. The OAuth2 providers already guard this: Authorize/Base signs [projectId, success, failure] with _APP_OPENSSL_KEY_V1 and Callback/Base fails closed on hash_equals. The GitHub App flow predates that and was never brought across. Mirror the same signature into the state Authorize builds and verify it before the projectId is read. A non-string signature value is treated as absent rather than reaching hash_equals, which throws on non-string input. The E2E suite performs the attacks directly: state with a tampered projectId, success or failure URL, a stripped signature, a signature replayed from another project, and a non-string signature are all rejected, while an untampered signed state passes through to the redirect. --- .../Modules/VCS/Http/GitHub/Authorize/Get.php | 3 + .../Modules/VCS/Http/GitHub/Callback/Get.php | 8 + .../VCSGitHubStateConsoleClientTest.php | 149 ++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index efa43f4837..63d28cf31c 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,10 +64,13 @@ class Get extends Action Document $project, array $platform ) { + // The callback endpoint is public, so it verifies this signature + // before trusting the projectId and redirect URLs in state. $state = \json_encode([ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), ]); $appName = System::getEnv('_APP_VCS_GITHUB_APP_NAME'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 76bcd4d8ea..84442978e2 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -68,6 +68,14 @@ class Get extends Action $redirectFailure = $state['failure'] ?? ''; $projectId = $state['projectId'] ?? ''; + // This endpoint is public -- without verifying the signature the + // Authorize action put in state, anyone could pass an arbitrary + // projectId here and attach an installation to another project. + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); + if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); + } + $project = $dbForPlatform->getDocument('projects', $projectId); if ($project->isEmpty()) { diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php new file mode 100644 index 0000000000..fe3192521c --- /dev/null +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php @@ -0,0 +1,149 @@ +getProject()['$id'] . '/settings/git-installations'; + } + + /** + * State signed the way the authorize endpoint signs it. + * + * @return array + */ + private function signState(string $projectId, string $success, string $failure): array + { + return [ + 'projectId' => $projectId, + 'success' => $success, + 'failure' => $failure, + 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + ]; + } + + /** + * @return array + */ + private function getCallbackHeaders(): array + { + return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); + } + + /** + * The signature check sits before everything else, so an untampered state + * passing through to the redirect is the proof it was accepted. Completing + * an installation is not possible here: without GitHub App credentials the + * flow cannot go further, so the redirect stands in for the happy path. + */ + public function testGetCallbackSignedState(): void + { + $redirect = $this->getRedirect(); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'request', + 'state' => json_encode($this->signState($this->getProject()['$id'], $redirect, $redirect)), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($redirect, (string) $response['headers']['location']); + } + + public function testGetCallbackUnsignedState(): void + { + $redirect = $this->getRedirect(); + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + unset($state['signature']); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); + } + + public function testGetCallbackTamperedState(): void + { + $redirect = $this->getRedirect(); + + // Every signed value changed after signing: the project an installation + // would attach to, and both redirect targets. + $tampered = [ + ['projectId' => 'victim-project'], + ['success' => 'https://evil.example/steal'], + ['failure' => 'https://evil.example/steal'], + ]; + + foreach ($tampered as $mutation) { + $state = array_merge($this->signState($this->getProject()['$id'], $redirect, $redirect), $mutation); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code'], json_encode($mutation)); + $this->assertStringContainsString('Invalid state parameter', (string) $response['body'], json_encode($mutation)); + } + } + + public function testGetCallbackReplayedSignature(): void + { + $redirect = $this->getRedirect(); + + // A signature captured from another project's authorize response must + // not validate someone else's state. + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + $state['signature'] = $this->signState('victim-project', $redirect, $redirect)['signature']; + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); + } + + public function testGetCallbackNonStringSignature(): void + { + $redirect = $this->getRedirect(); + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + + // hash_equals() throws on non-string input, so this must reject as an + // invalid state rather than surface a 500. + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode(array_merge($state, ['signature' => 1234])), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); + } +} From 449ba8e336d5f6e6d73721b0ae836644dba01a9f Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 11:23:00 +0530 Subject: [PATCH 02/11] fix(vcs): give the callback state validator headroom for the signature The success and failure URLs carried in state are not length-limited, so a state the authorize endpoint builds can already approach the callback's 2048 cap; the signature adds another ~80 bytes on top. Accept 4096 so the callback never rejects a state this server produced, and allow the empty value like the sibling params do. --- src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 84442978e2..82de4499b3 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -38,7 +38,7 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'GitHub installation ID', true) ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) - ->param('state', '', new Text(2048), 'GitHub state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(4096, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) ->inject('vcsFactory') ->inject('project') From 4562d4e73263921dca8f4994257370186eac5d43 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 12:23:20 +0530 Subject: [PATCH 03/11] Trim test comments to the non-obvious --- .../VCSGitHubStateConsoleClientTest.php | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php index fe3192521c..25eda690e2 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php @@ -11,11 +11,9 @@ use Tests\E2E\Scopes\SideConsole; use Utopia\System\System; /** - * Signature verification on the GitHub App installation state. The callback is - * public, so a state whose projectId or redirect URLs were changed in transit - * has to be rejected before anything is trusted. Deliberately does not use - * VCSGitHubBase: nothing here reaches GitHub, so it must also run on - * installations that have no GitHub App configured. + * Signature verification on the GitHub App installation state. Deliberately + * does not use VCSGitHubBase: nothing here reaches GitHub, so it must also run + * on installations that have no GitHub App configured. */ final class VCSGitHubStateConsoleClientTest extends Scope { @@ -51,10 +49,8 @@ final class VCSGitHubStateConsoleClientTest extends Scope } /** - * The signature check sits before everything else, so an untampered state - * passing through to the redirect is the proof it was accepted. Completing - * an installation is not possible here: without GitHub App credentials the - * flow cannot go further, so the redirect stands in for the happy path. + * Without GitHub App credentials the flow cannot go past the callback, so + * an untampered state reaching the redirect stands in for the happy path. */ public function testGetCallbackSignedState(): void { @@ -89,8 +85,6 @@ final class VCSGitHubStateConsoleClientTest extends Scope { $redirect = $this->getRedirect(); - // Every signed value changed after signing: the project an installation - // would attach to, and both redirect targets. $tampered = [ ['projectId' => 'victim-project'], ['success' => 'https://evil.example/steal'], @@ -115,8 +109,6 @@ final class VCSGitHubStateConsoleClientTest extends Scope { $redirect = $this->getRedirect(); - // A signature captured from another project's authorize response must - // not validate someone else's state. $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); $state['signature'] = $this->signState('victim-project', $redirect, $redirect)['signature']; From bea9e47a965e742cf0440182ebb7b2515e891a16 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Wed, 2 Sep 2026 19:25:37 +0530 Subject: [PATCH 04/11] Assert status codes only in the state signature suite Error prose is not a contract; a rejected state is a 400 whatever the wording. --- .../Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php index 25eda690e2..2d2b1d5688 100644 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php @@ -78,7 +78,6 @@ final class VCSGitHubStateConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } public function testGetCallbackTamperedState(): void @@ -101,7 +100,6 @@ final class VCSGitHubStateConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code'], json_encode($mutation)); - $this->assertStringContainsString('Invalid state parameter', (string) $response['body'], json_encode($mutation)); } } @@ -119,7 +117,6 @@ final class VCSGitHubStateConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } public function testGetCallbackNonStringSignature(): void @@ -136,6 +133,5 @@ final class VCSGitHubStateConsoleClientTest extends Scope ], followRedirects: false); $this->assertEquals(400, $response['headers']['status-code']); - $this->assertStringContainsString('Invalid state parameter', (string) $response['body']); } } From 1236842ef5c50accfc46983233ce3d03f4d4cdb8 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 11:21:59 +0530 Subject: [PATCH 05/11] Guard the shared callback's signature check and test the attacks on Gitea The OAuth2 callback shared by Gitea, GitLab and Bitbucket had the same non-string signature crash the GitHub callback gets fixed here: hash_equals() throws a TypeError, surfacing as a 500 instead of a rejected state. The attack cases move to the Gitea suite, which drives the shared callback end to end without provider credentials: stripped signature, tampered success and failure redirects, a signature replayed from another project, and a non-string signature are rejected; the existing tampered-project and signed-state cases already cover the rest. --- .../Modules/VCS/Http/Callback/Base.php | 2 +- .../VCSGitHubStateConsoleClientTest.php | 137 ------------------ .../VCSGitea/VCSGiteaConsoleClientTest.php | 56 +++++++ 3 files changed, 57 insertions(+), 138 deletions(-) delete mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index 81161ecf0f..ce6733af68 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -84,7 +84,7 @@ abstract class Base extends Action // authorization code could pass an arbitrary projectId here and // attach their VCS account as an installation on another project. $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); - if (!\hash_equals($signature, $state['signature'] ?? '')) { + if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php deleted file mode 100644 index 2d2b1d5688..0000000000 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php +++ /dev/null @@ -1,137 +0,0 @@ -getProject()['$id'] . '/settings/git-installations'; - } - - /** - * State signed the way the authorize endpoint signs it. - * - * @return array - */ - private function signState(string $projectId, string $success, string $failure): array - { - return [ - 'projectId' => $projectId, - 'success' => $success, - 'failure' => $failure, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), - ]; - } - - /** - * @return array - */ - private function getCallbackHeaders(): array - { - return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); - } - - /** - * Without GitHub App credentials the flow cannot go past the callback, so - * an untampered state reaching the redirect stands in for the happy path. - */ - public function testGetCallbackSignedState(): void - { - $redirect = $this->getRedirect(); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'request', - 'state' => json_encode($this->signState($this->getProject()['$id'], $redirect, $redirect)), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($redirect, (string) $response['headers']['location']); - } - - public function testGetCallbackUnsignedState(): void - { - $redirect = $this->getRedirect(); - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - unset($state['signature']); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } - - public function testGetCallbackTamperedState(): void - { - $redirect = $this->getRedirect(); - - $tampered = [ - ['projectId' => 'victim-project'], - ['success' => 'https://evil.example/steal'], - ['failure' => 'https://evil.example/steal'], - ]; - - foreach ($tampered as $mutation) { - $state = array_merge($this->signState($this->getProject()['$id'], $redirect, $redirect), $mutation); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code'], json_encode($mutation)); - } - } - - public function testGetCallbackReplayedSignature(): void - { - $redirect = $this->getRedirect(); - - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - $state['signature'] = $this->signState('victim-project', $redirect, $redirect)['signature']; - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } - - public function testGetCallbackNonStringSignature(): void - { - $redirect = $this->getRedirect(); - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - - // hash_equals() throws on non-string input, so this must reject as an - // invalid state rather than surface a 500. - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode(array_merge($state, ['signature' => 1234])), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } -} diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index ac0a58fa59..75779a6a0d 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -726,4 +726,60 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(404, $stillForeign['headers']['status-code']); $this->assertEquals('installation_not_found', $stillForeign['body']['type']); } + + public function testCreateInstallationWithUnsignedState(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); + unset($state['signature']); + + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => (string) \json_encode($state)]); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithTamperedRedirects(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + foreach (['success', 'failure'] as $field) { + $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); + $state[$field] = 'https://evil.example/steal'; + + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => (string) \json_encode($state)]); + + $this->assertEquals(400, $response['headers']['status-code'], $field); + } + } + + public function testCreateInstallationWithReplayedSignature(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); + $state['signature'] = \json_decode($this->buildGiteaState('victim-project', $consoleUrl, $consoleUrl), true)['signature']; + + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => (string) \json_encode($state)]); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testCreateInstallationWithNonStringSignature(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + $state = \json_decode($this->buildGiteaState($projectId, $consoleUrl, $consoleUrl), true); + $state['signature'] = 1234; + + // hash_equals() throws on non-string input, so this must reject as an + // invalid state rather than surface a 500. + $response = $this->callGiteaCallbackHelper(['code' => 'unused', 'state' => (string) \json_encode($state)]); + + $this->assertEquals(400, $response['headers']['status-code']); + } } From 7ae9891bb686c95a7489c59c32f7e7b16d17fcf3 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 3 Sep 2026 12:22:32 +0545 Subject: [PATCH 06/11] fix(usage): tag build and execution usage with the resource id Build and execution usage rows carried a populated resourceInternalId but an empty resourceId, so the console's GB-hours breakdown - which groups by resourceId - collapsed all build compute into a single "Unknown" row. CLO-4383 required emitting the public id alongside the internal one, but it was only applied to the Cloud-side emitters. Set it on the CE emitters too: Usage\Build (shared by the Builds and Jobs workers, covering functions and sites) and the execution listener used by self-hosted. Also drops the claim that the id is "resolved to resourceType + resourceId in the usage pipeline" - no such resolution exists. --- src/Appwrite/Bus/Listeners/Usage.php | 1 + src/Appwrite/Usage/Build.php | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Appwrite/Bus/Listeners/Usage.php b/src/Appwrite/Bus/Listeners/Usage.php index 1d675682bb..f673314cec 100644 --- a/src/Appwrite/Bus/Listeners/Usage.php +++ b/src/Appwrite/Bus/Listeners/Usage.php @@ -65,6 +65,7 @@ class Usage extends Listener $context = (new Context()) ->setResource(rtrim($resourceType, 's')) + ->setResourceId((string) $resource->getId()) ->setResourceInternalId((string) $resourceInternalId) ->addMetric(METRIC_EXECUTIONS, 1) ->addMetric(str_replace(['{resourceType}'], [$resourceType], METRIC_RESOURCE_TYPE_EXECUTIONS), 1) diff --git a/src/Appwrite/Usage/Build.php b/src/Appwrite/Usage/Build.php index a961c49281..fb2fdf6bd5 100644 --- a/src/Appwrite/Usage/Build.php +++ b/src/Appwrite/Usage/Build.php @@ -39,11 +39,11 @@ final class Build $mbSeconds = 0; } - // Per-resource breakdown now travels as resource dimensions on the - // Context (resolved to resourceType + resourceId in the usage pipeline) + // Per-resource breakdown travels as resource dimensions on the Context // instead of per-{resourceInternalId} metric-name templates. $usage ->setResource(rtrim($resourceType, 's')) + ->setResourceId($resource->getId()) ->setResourceInternalId((string) $resource->getSequence()); switch ($deployment->getAttribute('status')) { From 38028788229f7bf4de604eef1edc7fb572bec944 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 12:32:51 +0530 Subject: [PATCH 07/11] Refuse to sign or verify installation state with an empty key An empty _APP_OPENSSL_KEY_V1 would make the HMAC trivially forgeable, so authorize and callback fail closed instead, the way the module already does for a missing provider configuration. A weak but present key remains a deployment concern surfaced by the doctor task. --- src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php | 8 +++++++- src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php | 8 +++++++- .../Platform/Modules/VCS/Http/GitHub/Authorize/Get.php | 8 +++++++- .../Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 8 +++++++- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php index e28a96434e..7a526f288a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -97,13 +97,19 @@ abstract class Base extends Action $hostname = $platform['consoleHostname'] ?? ''; $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; + $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($key)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); + } + // The callback endpoint is public, so it verifies this signature // before trusting the projectId and redirect URLs in state. $oauth2 = $this->createOAuth2($callback, [ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, - 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), ]); $response diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index ce6733af68..043a1c05e4 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -83,7 +83,13 @@ abstract class Base extends Action // Authorize action put in state, anyone with their own valid // authorization code could pass an arbitrary projectId here and // attach their VCS account as an installation on another project. - $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); + $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($key)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); + } + + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $key); if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 63d28cf31c..9abd01391a 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,13 +64,19 @@ class Get extends Action Document $project, array $platform ) { + $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($key)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); + } + // The callback endpoint is public, so it 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', '')), + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), ]); $appName = System::getEnv('_APP_VCS_GITHUB_APP_NAME'); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 65d34af8a8..228bbb06ee 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -70,7 +70,13 @@ class Get extends Action // This endpoint is public -- without verifying the signature the // Authorize action put in state, anyone could pass an arbitrary // projectId here and attach an installation to another project. - $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')); + $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($key)) { + throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); + } + + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $key); if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } From bdfbcc8a284cebd6e5811af93fad82ef33006cc7 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 12:37:09 +0530 Subject: [PATCH 08/11] Keep the GitHub state suite alongside the Gitea cases The Gitea suite proves the shared mechanism, but the GitHub callback is a separate implementation; this suite is the only test that runs the signing this change adds to it, and it needs no GitHub credentials. --- .../VCSGitHubStateConsoleClientTest.php | 137 ++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php new file mode 100644 index 0000000000..2d2b1d5688 --- /dev/null +++ b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php @@ -0,0 +1,137 @@ +getProject()['$id'] . '/settings/git-installations'; + } + + /** + * State signed the way the authorize endpoint signs it. + * + * @return array + */ + private function signState(string $projectId, string $success, string $failure): array + { + return [ + 'projectId' => $projectId, + 'success' => $success, + 'failure' => $failure, + 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), + ]; + } + + /** + * @return array + */ + private function getCallbackHeaders(): array + { + return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); + } + + /** + * Without GitHub App credentials the flow cannot go past the callback, so + * an untampered state reaching the redirect stands in for the happy path. + */ + public function testGetCallbackSignedState(): void + { + $redirect = $this->getRedirect(); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'request', + 'state' => json_encode($this->signState($this->getProject()['$id'], $redirect, $redirect)), + ], followRedirects: false); + + $this->assertEquals(301, $response['headers']['status-code']); + $this->assertStringStartsWith($redirect, (string) $response['headers']['location']); + } + + public function testGetCallbackUnsignedState(): void + { + $redirect = $this->getRedirect(); + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + unset($state['signature']); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testGetCallbackTamperedState(): void + { + $redirect = $this->getRedirect(); + + $tampered = [ + ['projectId' => 'victim-project'], + ['success' => 'https://evil.example/steal'], + ['failure' => 'https://evil.example/steal'], + ]; + + foreach ($tampered as $mutation) { + $state = array_merge($this->signState($this->getProject()['$id'], $redirect, $redirect), $mutation); + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code'], json_encode($mutation)); + } + } + + public function testGetCallbackReplayedSignature(): void + { + $redirect = $this->getRedirect(); + + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + $state['signature'] = $this->signState('victim-project', $redirect, $redirect)['signature']; + + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode($state), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + } + + public function testGetCallbackNonStringSignature(): void + { + $redirect = $this->getRedirect(); + $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); + + // hash_equals() throws on non-string input, so this must reject as an + // invalid state rather than surface a 500. + $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ + 'setup_action' => 'install', + 'installation_id' => '1234567', + 'state' => json_encode(array_merge($state, ['signature' => 1234])), + ], followRedirects: false); + + $this->assertEquals(400, $response['headers']['status-code']); + } +} From a1855eeb8fd05c9c795baa05d83d371b4bcf53aa Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 12:40:20 +0530 Subject: [PATCH 09/11] Test the signature on Gitea only The Gitea suite drives the shared callback end to end; GitHub-specific suites are not kept alongside it. --- .../VCSGitHubStateConsoleClientTest.php | 137 ------------------ 1 file changed, 137 deletions(-) delete mode 100644 tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php diff --git a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php b/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php deleted file mode 100644 index 2d2b1d5688..0000000000 --- a/tests/e2e/Services/VCSGitHub/VCSGitHubStateConsoleClientTest.php +++ /dev/null @@ -1,137 +0,0 @@ -getProject()['$id'] . '/settings/git-installations'; - } - - /** - * State signed the way the authorize endpoint signs it. - * - * @return array - */ - private function signState(string $projectId, string $success, string $failure): array - { - return [ - 'projectId' => $projectId, - 'success' => $success, - 'failure' => $failure, - 'signature' => hash_hmac('sha256', json_encode([$projectId, $success, $failure]), System::getEnv('_APP_OPENSSL_KEY_V1', '')), - ]; - } - - /** - * @return array - */ - private function getCallbackHeaders(): array - { - return array_merge(['x-appwrite-project' => $this->getProject()['$id']], $this->getHeaders()); - } - - /** - * Without GitHub App credentials the flow cannot go past the callback, so - * an untampered state reaching the redirect stands in for the happy path. - */ - public function testGetCallbackSignedState(): void - { - $redirect = $this->getRedirect(); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'request', - 'state' => json_encode($this->signState($this->getProject()['$id'], $redirect, $redirect)), - ], followRedirects: false); - - $this->assertEquals(301, $response['headers']['status-code']); - $this->assertStringStartsWith($redirect, (string) $response['headers']['location']); - } - - public function testGetCallbackUnsignedState(): void - { - $redirect = $this->getRedirect(); - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - unset($state['signature']); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } - - public function testGetCallbackTamperedState(): void - { - $redirect = $this->getRedirect(); - - $tampered = [ - ['projectId' => 'victim-project'], - ['success' => 'https://evil.example/steal'], - ['failure' => 'https://evil.example/steal'], - ]; - - foreach ($tampered as $mutation) { - $state = array_merge($this->signState($this->getProject()['$id'], $redirect, $redirect), $mutation); - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code'], json_encode($mutation)); - } - } - - public function testGetCallbackReplayedSignature(): void - { - $redirect = $this->getRedirect(); - - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - $state['signature'] = $this->signState('victim-project', $redirect, $redirect)['signature']; - - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode($state), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } - - public function testGetCallbackNonStringSignature(): void - { - $redirect = $this->getRedirect(); - $state = $this->signState($this->getProject()['$id'], $redirect, $redirect); - - // hash_equals() throws on non-string input, so this must reject as an - // invalid state rather than surface a 500. - $response = $this->client->call(Client::METHOD_GET, '/vcs/github/callback', $this->getCallbackHeaders(), [ - 'setup_action' => 'install', - 'installation_id' => '1234567', - 'state' => json_encode(array_merge($state, ['signature' => 1234])), - ], followRedirects: false); - - $this->assertEquals(400, $response['headers']['status-code']); - } -} From 3fed8989bb0b3d198d70b53c6eeb20cfd138f62c Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 12:50:01 +0530 Subject: [PATCH 10/11] Refuse redirect URLs that would outgrow the callback's state limit Nothing bounded the success and failure URLs, so authorize could hand a provider a state longer than the callback accepts and the installation failed on return. Both authorize endpoints now reject that up front, and one constant carries the limit so the two sides cannot drift. --- app/init/constants.php | 1 + .../Platform/Modules/VCS/Http/Authorize/Base.php | 10 ++++++++-- .../Platform/Modules/VCS/Http/Callback/Base.php | 2 +- .../Modules/VCS/Http/GitHub/Authorize/Get.php | 4 ++++ .../Modules/VCS/Http/GitHub/Callback/Get.php | 2 +- .../VCSGitea/VCSGiteaConsoleClientTest.php | 16 ++++++++++++++++ 6 files changed, 31 insertions(+), 4 deletions(-) diff --git a/app/init/constants.php b/app/init/constants.php index f6e0abae84..050a20b97f 100644 --- a/app/init/constants.php +++ b/app/init/constants.php @@ -153,6 +153,7 @@ const APP_SDK_PLATFORM_SERVER = 'server'; const APP_SDK_PLATFORM_CLIENT = 'client'; const APP_SDK_PLATFORM_CONSOLE = 'console'; const APP_SDK_PLATFORM_STATIC = 'static'; +const APP_LIMIT_VCS_STATE = 4096; // Maximum length of the state the VCS authorize endpoints hand to a provider const APP_VCS_GITHUB_USERNAME = 'Appwrite'; const APP_VCS_GITHUB_EMAIL = 'team@appwrite.io'; const APP_VCS_GITHUB_URL = 'https://github.com/TeamAppwrite'; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php index 7a526f288a..b46898db9d 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -105,12 +105,18 @@ abstract class Base extends Action // The callback endpoint is public, so it verifies this signature // before trusting the projectId and redirect URLs in state. - $oauth2 = $this->createOAuth2($callback, [ + $state = [ 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), - ]); + ]; + + if (\strlen((string) \json_encode($state)) > APP_LIMIT_VCS_STATE) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Redirect URLs are too long to complete the installation. Please use shorter success and failure URLs.'); + } + + $oauth2 = $this->createOAuth2($callback, $state); $response ->addHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0') diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index 043a1c05e4..88647da603 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -55,7 +55,7 @@ abstract class Base extends Action ->label('scope', 'public') ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) - ->param('state', '', new Text(4096, 0), 'OAuth2 state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(APP_LIMIT_VCS_STATE, 0), 'OAuth2 state. Contains info sent when starting authorization flow.', true) ->inject('response') ->inject('dbForPlatform') ->inject('platform') diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index 9abd01391a..f1d48b8618 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -79,6 +79,10 @@ class Get extends Action 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), ]); + if (\strlen($state) > APP_LIMIT_VCS_STATE) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Redirect URLs are too long to complete the installation. Please use shorter success and failure URLs.'); + } + $appName = System::getEnv('_APP_VCS_GITHUB_APP_NAME'); $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $hostname = $platform['consoleHostname'] ?? ''; diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index 228bbb06ee..a76ef8d4d4 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -38,7 +38,7 @@ class Get extends Action ->label('error', APP_VIEWS_DIR . '/general/error.phtml') ->param('installation_id', '', new Text(256, 0), 'GitHub installation ID', true) ->param('setup_action', '', new Text(256, 0), 'GitHub setup action type', true) - ->param('state', '', new Text(4096, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) + ->param('state', '', new Text(APP_LIMIT_VCS_STATE, 0), 'GitHub state. Contains info sent when starting authorization flow.', true) ->param('code', '', new Text(2048, 0), 'OAuth2 code. This is a temporary code that the will be later exchanged for an access token.', true) ->inject('vcsFactory') ->inject('project') diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index 75779a6a0d..91c218408e 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -782,4 +782,20 @@ final class VCSGiteaConsoleClientTest extends Scope $this->assertEquals(400, $response['headers']['status-code']); } + + public function testCreateInstallationWithOversizedRedirects(): void + { + $projectId = $this->getProject()['$id']; + $consoleUrl = 'http://localhost/console/project-default-' . $projectId . '/settings/git-installations'; + + // Authorize must refuse rather than mint a state its own callback would reject. + $authorize = $this->client->call(Client::METHOD_GET, '/vcs/gitea/authorize', \array_merge([ + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()), [ + 'success' => $consoleUrl, + 'failure' => $consoleUrl . '?pad=' . \str_repeat('a', APP_LIMIT_VCS_STATE), + ], true, false); + + $this->assertEquals(400, $authorize['headers']['status-code']); + } } From 6a316aada74c8db5a98a891c003a4dc3bdbdec0e Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Thu, 3 Sep 2026 12:51:35 +0530 Subject: [PATCH 11/11] Name the signing key apart from the provider key Both base actions already bind $key to the provider and use it for the callback URL and the installation lookup, so reusing the name for the OpenSSL key sent every Gitea, GitLab and Bitbucket callback looking for a provider named after the key. --- src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php | 6 +++--- src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php | 6 +++--- .../Platform/Modules/VCS/Http/GitHub/Authorize/Get.php | 6 +++--- .../Platform/Modules/VCS/Http/GitHub/Callback/Get.php | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php index b46898db9d..b99867477e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -97,9 +97,9 @@ abstract class Base extends Action $hostname = $platform['consoleHostname'] ?? ''; $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; - $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); - if (empty($key)) { + if (empty($signingKey)) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); } @@ -109,7 +109,7 @@ abstract class Base extends Action 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, - 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $signingKey), ]; if (\strlen((string) \json_encode($state)) > APP_LIMIT_VCS_STATE) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php index 88647da603..55f9ee1d2f 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Callback/Base.php @@ -83,13 +83,13 @@ abstract class Base extends Action // Authorize action put in state, anyone with their own valid // authorization code could pass an arbitrary projectId here and // attach their VCS account as an installation on another project. - $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); - if (empty($key)) { + if (empty($signingKey)) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); } - $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $key); + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $signingKey); if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); } diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php index f1d48b8618..fb5a28cfaf 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Authorize/Get.php @@ -64,9 +64,9 @@ class Get extends Action Document $project, array $platform ) { - $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); - if (empty($key)) { + if (empty($signingKey)) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); } @@ -76,7 +76,7 @@ class Get extends Action 'projectId' => $project->getId(), 'success' => $success, 'failure' => $failure, - 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $key), + 'signature' => \hash_hmac('sha256', \json_encode([$project->getId(), $success, $failure]), $signingKey), ]); if (\strlen($state) > APP_LIMIT_VCS_STATE) { diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php index a76ef8d4d4..eb53da9857 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Callback/Get.php @@ -70,13 +70,13 @@ class Get extends Action // This endpoint is public -- without verifying the signature the // Authorize action put in state, anyone could pass an arbitrary // projectId here and attach an installation to another project. - $key = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); - if (empty($key)) { + if (empty($signingKey)) { throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Signing key is not configured. Please configure _APP_OPENSSL_KEY_V1 in .env file.'); } - $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $key); + $signature = \hash_hmac('sha256', \json_encode([$projectId, $state['success'] ?? '', $redirectFailure]), $signingKey); if (!\hash_equals($signature, \is_string($state['signature'] ?? null) ? $state['signature'] : '')) { throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Invalid state parameter. Please restart the installation from the Appwrite Console.'); }