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 e28a96434e..b99867477e 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/Authorize/Base.php @@ -97,14 +97,26 @@ abstract class Base extends Action $hostname = $platform['consoleHostname'] ?? ''; $callback = $protocol . '://' . $hostname . '/v1/vcs/' . $key . '/callback'; + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($signingKey)) { + 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, [ + $state = [ '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]), $signingKey), + ]; + + 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 81161ecf0f..55f9ee1d2f 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') @@ -83,8 +83,14 @@ 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', '')); - if (!\hash_equals($signature, $state['signature'] ?? '')) { + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + 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]), $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 efa43f4837..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,12 +64,25 @@ class Get extends Action Document $project, array $platform ) { + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + if (empty($signingKey)) { + 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]), $signingKey), ]); + 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 ce92a37f48..eb53da9857 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') @@ -67,6 +67,20 @@ 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. + $signingKey = System::getEnv('_APP_OPENSSL_KEY_V1', ''); + + 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]), $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.'); + } + $project = $dbForPlatform->getDocument('projects', $projectId); if ($project->isEmpty()) { diff --git a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php index ac0a58fa59..91c218408e 100644 --- a/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php +++ b/tests/e2e/Services/VCSGitea/VCSGiteaConsoleClientTest.php @@ -726,4 +726,76 @@ 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']); + } + + 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']); + } }