From 8788b6b6da9c31f56bb9c28e70eae317850bbb28 Mon Sep 17 00:00:00 2001 From: harsh mahajan Date: Tue, 15 Sep 2026 14:53:04 +0530 Subject: [PATCH] fix(account): hand OAuth2 session params only to the console's own relay The default success and failure relays were matched by path alone, so any allowed host serving /auth/oauth2/success received the session key and secret. Match the console host from platform consoleUrl as well. --- app/controllers/api/account.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 5c8d7ff94e..5fede6a7fd 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1508,7 +1508,8 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') ->inject('domainVerification') ->inject('cookieDomain') ->inject('authorization') - ->action(function (string $provider, string $code, string $state, string $error, string $error_description, Request $request, Response $response, Document $project, Validator $redirectValidator, Document $devKey, User $user, Database $dbForProject, Geo $geo, Database $dbForPlatform, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, array $plan, bool $domainVerification, ?string $cookieDomain, Authorization $authorization) use ($oauthDefaultSuccess, $oauthDefaultFailure) { + ->inject('platform') + ->action(function (string $provider, string $code, string $state, string $error, string $error_description, Request $request, Response $response, Document $project, Validator $redirectValidator, Document $devKey, User $user, Database $dbForProject, Geo $geo, Database $dbForPlatform, Event $queueForEvents, Store $store, ProofsPassword $proofForPassword, ProofsToken $proofForToken, array $plan, bool $domainVerification, ?string $cookieDomain, Authorization $authorization, array $platform) use ($oauthDefaultSuccess, $oauthDefaultFailure) { $protocol = System::getEnv('_APP_OPTIONS_FORCE_HTTPS') === 'disabled' ? 'http' : 'https'; $port = $request->getPort(); $callbackBase = $protocol . '://' . $request->getHostname(); @@ -1576,12 +1577,15 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') if ($devKey->isEmpty() && !empty($state['failure']) && !$redirectValidator->isValid($state['failure'])) { throw new Exception(Exception::PROJECT_INVALID_FAILURE_URL); } + // The default relays live on the console host; the same path on any other allowed host is a customer page + $consoleHostname = \parse_url($platform['consoleUrl'] ?? '', PHP_URL_HOST); + $failure = []; if (!empty($state['failure'])) { $failure = URLParser::parse($state['failure']); } - $failureRedirect = (function (string $type, ?string $message = null, ?int $code = null, ?\Throwable $previous = null, array $params = []) use ($failure, $response, $project, $oauthDefaultFailure) { + $failureRedirect = (function (string $type, ?string $message = null, ?int $code = null, ?\Throwable $previous = null, array $params = []) use ($failure, $response, $project, $oauthDefaultFailure, $consoleHostname) { $exception = new Exception($type, $message, $code, $previous, params: $params); if (!empty($failure)) { $query = URLParser::parseQuery($failure['query']); @@ -1592,7 +1596,7 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') ]); // Mirror success path: default OAuth failure relay needs project to deep-link // back into the native app via appwrite-callback-{project}:// - if (($failure['path'] ?? '') === $oauthDefaultFailure) { + if ($failure['host'] === $consoleHostname && $failure['path'] === $oauthDefaultFailure) { $query['project'] = $project->getId(); } $failure['query'] = URLParser::unparseQuery($query); @@ -2175,7 +2179,7 @@ Http::get('/v1/account/sessions/oauth2/:provider/redirect') ; // TODO: Remove this deprecated workaround - support only token - if ($state['success']['path'] == $oauthDefaultSuccess) { + if ($state['success']['host'] === $consoleHostname && $state['success']['path'] === $oauthDefaultSuccess) { $query['project'] = $project->getId(); $query['domain'] = $cookieDomain; $query['key'] = $store->getKey();