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.
This commit is contained in:
harsh mahajan committed 2026-09-15 14:54:02 +05:30
1 parent 4ed17e6602
commit 8788b6b6da
1 file changed
+8 -4
+8 -4
View File
@@ -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();