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.
This commit is contained in:
harsh mahajan committed 2026-09-03 12:50:01 +05:30
1 parent a1855eeb8f
commit 3fed8989bb
6 files changed
+31 -4

No files matched your search

+1
View File
@@ -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';
@@ -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')
@@ -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')
@@ -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'] ?? '';
@@ -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')
@@ -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']);
}
}