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']); + } }