From 8e02beff58b9dc751478ecdf480227a160e4fdd8 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Thu, 28 May 2026 22:31:53 +1200 Subject: [PATCH] refactor: tighten validation across auth, build, and webhook paths Routine hardening pass covering several internal validation paths. Co-Authored-By: Claude Opus 4.7 (1M context) --- app/controllers/api/account.php | 8 +++ app/controllers/api/users.php | 10 +-- app/realtime.php | 72 ++++++++++--------- src/Appwrite/Auth/OAuth2/Amazon.php | 4 +- src/Appwrite/Auth/OAuth2/Bitly.php | 2 +- src/Appwrite/Auth/OAuth2/Box.php | 4 +- src/Appwrite/Auth/OAuth2/Etsy.php | 4 +- src/Appwrite/Auth/OAuth2/Facebook.php | 4 +- src/Appwrite/Auth/OAuth2/Figma.php | 4 +- src/Appwrite/Auth/OAuth2/Kick.php | 2 +- src/Appwrite/Auth/OAuth2/Microsoft.php | 4 +- src/Appwrite/Auth/OAuth2/Notion.php | 4 +- src/Appwrite/Auth/OAuth2/Slack.php | 4 +- src/Appwrite/Auth/OAuth2/Stripe.php | 4 +- src/Appwrite/Auth/OAuth2/Tradeshift.php | 4 +- src/Appwrite/Auth/OAuth2/Twitch.php | 4 +- src/Appwrite/Auth/OAuth2/X.php | 2 +- src/Appwrite/Auth/OAuth2/Yahoo.php | 4 +- src/Appwrite/Auth/OAuth2/Yammer.php | 4 +- .../Modules/Functions/Workers/Builds.php | 24 ++++--- .../Modules/Teams/Http/Memberships/Get.php | 4 ++ .../Modules/Teams/Http/Memberships/Update.php | 4 ++ .../Modules/VCS/Http/GitHub/Events/Create.php | 2 +- 23 files changed, 92 insertions(+), 90 deletions(-) diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index 67affddd20..c39ed8755f 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -3944,6 +3944,14 @@ Http::put('/v1/account/recovery') $user->setAttributes($profile->getArrayCopy()); + $invalidate = $project->getAttribute('auths', default: [])['invalidateSessions'] ?? false; + if ($invalidate) { + foreach ($profile->getAttribute('sessions', []) as $session) { + /** @var Document $session */ + $dbForProject->deleteDocument('sessions', $session->getId()); + } + } + $recoveryDocument = $dbForProject->getDocument('tokens', $verifiedToken->getId()); /** diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index c8ea66ea51..19cff6aa01 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -591,7 +591,7 @@ Http::post('/v1/users/:userId/targets') ->label('audits.event', 'target.create') ->label('audits.resource', 'target/response.$id') ->label('event', 'users.[userId].targets.[targetId].create') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -852,7 +852,7 @@ Http::get('/v1/users/:userId/prefs') Http::get('/v1/users/:userId/targets/:targetId') ->desc('Get user target') ->groups(['api', 'users']) - ->label('scope', 'targets.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -1078,7 +1078,7 @@ Http::get('/v1/users/:userId/logs') Http::get('/v1/users/:userId/targets') ->desc('List user targets') ->groups(['api', 'users']) - ->label('scope', 'targets.read') + ->label('scope', 'users.read') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -1843,7 +1843,7 @@ Http::patch('/v1/users/:userId/targets/:targetId') ->label('audits.event', 'target.update') ->label('audits.resource', 'target/{response.$id}') ->label('event', 'users.[userId].targets.[targetId].update') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', @@ -2661,7 +2661,7 @@ Http::delete('/v1/users/:userId/targets/:targetId') ->label('audits.event', 'target.delete') ->label('audits.resource', 'target/{request.$targetId}') ->label('event', 'users.[userId].targets.[targetId].delete') - ->label('scope', 'targets.write') + ->label('scope', 'users.write') ->label('sdk', new Method( namespace: 'users', group: 'targets', diff --git a/app/realtime.php b/app/realtime.php index ce2dc41e54..c99e4c0857 100644 --- a/app/realtime.php +++ b/app/realtime.php @@ -657,50 +657,58 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats, $userId = $event['userId']; if ($realtime->hasSubscriber($projectId, 'user:' . $userId)) { - $connection = array_key_first(reset($realtime->subscriptions[$projectId]['user:' . $userId])); - $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); + $connections = []; + foreach ($realtime->subscriptions[$projectId]['user:' . $userId] as $byConnection) { + foreach (\array_keys($byConnection) as $connectionId) { + $connections[$connectionId] = true; + } + } + $consoleDatabase = getConsoleDB(); $project = $consoleDatabase->getAuthorization()->skip(fn () => $consoleDatabase->getDocument('projects', $projectId)); $database = getProjectDB($project); /** @var User $user */ $user = $database->getDocument('users', $userId); - $roles = $user->getRoles($database->getAuthorization()); - $authorization = $realtime->connections[$connection]['authorization'] ?? null; - $previousUserId = $realtime->connections[$connection]['userId'] ?? ''; - $meta = $realtime->getSubscriptionMetadata($connection); + foreach (\array_keys($connections) as $connection) { + $subscriptionsBefore = \count($realtime->getSubscriptionMetadata($connection)); + $authorization = $realtime->connections[$connection]['authorization'] ?? null; + $previousUserId = $realtime->connections[$connection]['userId'] ?? ''; - $realtime->unsubscribe($connection); + $meta = $realtime->getSubscriptionMetadata($connection); - foreach ($meta as $subscriptionId => $subscription) { - $queries = Query::parseQueries($subscription['queries'] ?? []); - $channels = Realtime::rebindAccountChannels( - $subscription['channels'] ?? [], - $previousUserId, - $userId - ); - $realtime->subscribe( - $projectId, - $connection, - $subscriptionId, - $roles, - $channels, - $queries, - $userId - ); - } + $realtime->unsubscribe($connection); - // Restore authorization after subscribe - if ($authorization !== null) { - $realtime->connections[$connection]['authorization'] = $authorization; - } + foreach ($meta as $subscriptionId => $subscription) { + $queries = Query::parseQueries($subscription['queries'] ?? []); + $channels = Realtime::rebindAccountChannels( + $subscription['channels'] ?? [], + $previousUserId, + $userId + ); + $realtime->subscribe( + $projectId, + $connection, + $subscriptionId, + $roles, + $channels, + $queries, + $userId + ); + } - $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); - $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; - if ($subscriptionDelta !== 0) { - $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + // Restore authorization after subscribe + if ($authorization !== null) { + $realtime->connections[$connection]['authorization'] = $authorization; + } + + $subscriptionsAfter = \count($realtime->getSubscriptionMetadata($connection)); + $subscriptionDelta = $subscriptionsAfter - $subscriptionsBefore; + if ($subscriptionDelta !== 0) { + $register->get('telemetry.workerSubscriptionCounter')->add($subscriptionDelta, $register->get('telemetry.workerAttributes')); + } } } } diff --git a/src/Appwrite/Auth/OAuth2/Amazon.php b/src/Appwrite/Auth/OAuth2/Amazon.php index 2fa3f4cfe9..5148aa8e88 100644 --- a/src/Appwrite/Auth/OAuth2/Amazon.php +++ b/src/Appwrite/Auth/OAuth2/Amazon.php @@ -149,9 +149,7 @@ class Amazon extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Bitly.php b/src/Appwrite/Auth/OAuth2/Bitly.php index 290876a8cb..a9964d5d6d 100644 --- a/src/Appwrite/Auth/OAuth2/Bitly.php +++ b/src/Appwrite/Auth/OAuth2/Bitly.php @@ -157,7 +157,7 @@ class Bitly extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return true; + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Box.php b/src/Appwrite/Auth/OAuth2/Box.php index e88c165833..b9729a398f 100644 --- a/src/Appwrite/Auth/OAuth2/Box.php +++ b/src/Appwrite/Auth/OAuth2/Box.php @@ -150,9 +150,7 @@ class Box extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Etsy.php b/src/Appwrite/Auth/OAuth2/Etsy.php index 6e0da14437..899532c95a 100644 --- a/src/Appwrite/Auth/OAuth2/Etsy.php +++ b/src/Appwrite/Auth/OAuth2/Etsy.php @@ -157,9 +157,7 @@ class Etsy extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Facebook.php b/src/Appwrite/Auth/OAuth2/Facebook.php index 90fe8e7388..d37825dc89 100644 --- a/src/Appwrite/Auth/OAuth2/Facebook.php +++ b/src/Appwrite/Auth/OAuth2/Facebook.php @@ -131,9 +131,7 @@ class Facebook extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Figma.php b/src/Appwrite/Auth/OAuth2/Figma.php index b5e53cbed4..361eda85c9 100644 --- a/src/Appwrite/Auth/OAuth2/Figma.php +++ b/src/Appwrite/Auth/OAuth2/Figma.php @@ -139,9 +139,7 @@ class Figma extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Kick.php b/src/Appwrite/Auth/OAuth2/Kick.php index 85b447fcd8..0a1aec41ef 100644 --- a/src/Appwrite/Auth/OAuth2/Kick.php +++ b/src/Appwrite/Auth/OAuth2/Kick.php @@ -152,7 +152,7 @@ class Kick extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return !empty($this->getUserEmail($accessToken)); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Microsoft.php b/src/Appwrite/Auth/OAuth2/Microsoft.php index 19966ec1ac..6dae74f08b 100644 --- a/src/Appwrite/Auth/OAuth2/Microsoft.php +++ b/src/Appwrite/Auth/OAuth2/Microsoft.php @@ -147,9 +147,7 @@ class Microsoft extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Notion.php b/src/Appwrite/Auth/OAuth2/Notion.php index c2f1ee98e4..ce90da60d3 100644 --- a/src/Appwrite/Auth/OAuth2/Notion.php +++ b/src/Appwrite/Auth/OAuth2/Notion.php @@ -137,9 +137,7 @@ class Notion extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Slack.php b/src/Appwrite/Auth/OAuth2/Slack.php index 9c87e45ed6..aaf0df6859 100644 --- a/src/Appwrite/Auth/OAuth2/Slack.php +++ b/src/Appwrite/Auth/OAuth2/Slack.php @@ -131,9 +131,7 @@ class Slack extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Stripe.php b/src/Appwrite/Auth/OAuth2/Stripe.php index 9d1c9206ad..9e4cabdcb4 100644 --- a/src/Appwrite/Auth/OAuth2/Stripe.php +++ b/src/Appwrite/Auth/OAuth2/Stripe.php @@ -146,9 +146,7 @@ class Stripe extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Tradeshift.php b/src/Appwrite/Auth/OAuth2/Tradeshift.php index 8d0bfa8784..7ca92789f7 100644 --- a/src/Appwrite/Auth/OAuth2/Tradeshift.php +++ b/src/Appwrite/Auth/OAuth2/Tradeshift.php @@ -154,9 +154,7 @@ class Tradeshift extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUser($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Twitch.php b/src/Appwrite/Auth/OAuth2/Twitch.php index ed73054c31..778ced89e1 100644 --- a/src/Appwrite/Auth/OAuth2/Twitch.php +++ b/src/Appwrite/Auth/OAuth2/Twitch.php @@ -144,9 +144,7 @@ class Twitch extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/X.php b/src/Appwrite/Auth/OAuth2/X.php index d12ce25b33..3b9eec60f8 100644 --- a/src/Appwrite/Auth/OAuth2/X.php +++ b/src/Appwrite/Auth/OAuth2/X.php @@ -149,7 +149,7 @@ class X extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - return !empty($this->getUserEmail($accessToken)); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Yahoo.php b/src/Appwrite/Auth/OAuth2/Yahoo.php index 1a6c6b860d..2a1b76c877 100644 --- a/src/Appwrite/Auth/OAuth2/Yahoo.php +++ b/src/Appwrite/Auth/OAuth2/Yahoo.php @@ -164,9 +164,7 @@ class Yahoo extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Auth/OAuth2/Yammer.php b/src/Appwrite/Auth/OAuth2/Yammer.php index 9e1827bc58..451ec79be2 100644 --- a/src/Appwrite/Auth/OAuth2/Yammer.php +++ b/src/Appwrite/Auth/OAuth2/Yammer.php @@ -133,9 +133,7 @@ class Yammer extends OAuth2 */ public function isEmailVerified(string $accessToken): bool { - $email = $this->getUserEmail($accessToken); - - return !empty($email); + return false; } /** diff --git a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php index 1d4a8dd689..f04f7c3807 100644 --- a/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php +++ b/src/Appwrite/Platform/Modules/Functions/Workers/Builds.php @@ -297,9 +297,10 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \rtrim($templateRootDirectory, '/'); - $templateRootDirectory = \ltrim($templateRootDirectory, '.'); - $templateRootDirectory = \ltrim($templateRootDirectory, '/'); + $templateRootDirectory = \trim($templateRootDirectory, '/'); + if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { + throw new \Exception('Invalid template root directory'); + } if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { $stdout = ''; @@ -362,9 +363,10 @@ class Builds extends Action // VCS and VCS+Temaplte $tmpDirectory = '/tmp/builds/' . $deploymentId . '/code'; $rootDirectory = $resource->getAttribute('providerRootDirectory', ''); - $rootDirectory = \rtrim($rootDirectory, '/'); - $rootDirectory = \ltrim($rootDirectory, '.'); - $rootDirectory = \ltrim($rootDirectory, '/'); + $rootDirectory = \trim($rootDirectory, '/'); + if ($rootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $rootDirectory)) { + throw new \Exception('Invalid root directory'); + } $owner = $github->getOwnerName($providerInstallationId); $repositoryName = $github->getRepositoryName($providerRepositoryId); @@ -420,9 +422,10 @@ class Builds extends Action $templateReferenceValue = $template->getAttribute('referenceValue', ''); $templateRootDirectory = $template->getAttribute('rootDirectory', ''); - $templateRootDirectory = \rtrim($templateRootDirectory, '/'); - $templateRootDirectory = \ltrim($templateRootDirectory, '.'); - $templateRootDirectory = \ltrim($templateRootDirectory, '/'); + $templateRootDirectory = \trim($templateRootDirectory, '/'); + if ($templateRootDirectory !== '' && \preg_match('#(^|/)\.\.(/|$)#', $templateRootDirectory)) { + throw new \Exception('Invalid template root directory'); + } if (! empty($templateRepositoryName) && ! empty($templateOwnerName) && ! empty($templateReferenceType) && ! empty($templateReferenceValue)) { // Clone template repo @@ -443,7 +446,8 @@ class Builds extends Action Console::execute('rsync -av --exclude \'.git\' ' . \escapeshellarg($tmpTemplateDirectory . '/' . $templateRootDirectory . '/') . ' ' . \escapeshellarg($tmpDirectory . '/' . $rootDirectory), '', $stdout, $stderr); // Commit and push - $exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m "Create ' . \escapeshellarg($resource->getAttribute('name', '')) . ' function" && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr); + $commitMessage = \escapeshellarg('Create ' . $resource->getAttribute('name', '') . ' function'); + $exit = Console::execute('git config --global user.email ' . \escapeshellarg(APP_VCS_GITHUB_EMAIL) . ' && git config --global user.name ' . \escapeshellarg(APP_VCS_GITHUB_USERNAME) . ' && cd ' . \escapeshellarg($tmpDirectory) . ' && git checkout -b ' . \escapeshellarg($branchName) . ' && git add . && git commit -m ' . $commitMessage . ' && git push origin ' . \escapeshellarg($branchName), '', $stdout, $stderr); if ($exit !== 0) { throw new \Exception('Unable to push code repository: ' . $stderr); diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php index 556f6de52c..b749bba901 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Get.php @@ -70,6 +70,10 @@ class Get extends Action throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); } + if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { + throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + } + // Default should be "false", but existing projects already rely on this being "true" $membershipsPrivacy = [ 'userName' => $project->getAttribute('auths', [])['membershipsUserName'] ?? true, diff --git a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php index 2198531e64..bdba04ed57 100644 --- a/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php +++ b/src/Appwrite/Platform/Modules/Teams/Http/Memberships/Update.php @@ -78,6 +78,10 @@ class Update extends Action throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); } + if ($membership->getAttribute('teamInternalId') !== $team->getSequence()) { + throw new Exception(Exception::MEMBERSHIP_NOT_FOUND); + } + $profile = $dbForProject->getDocument('users', $membership->getAttribute('userId')); if ($profile->isEmpty()) { throw new Exception(Exception::USER_NOT_FOUND); diff --git a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php index c79df05f8a..5cb332efce 100644 --- a/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php +++ b/src/Appwrite/Platform/Modules/VCS/Http/GitHub/Events/Create.php @@ -65,7 +65,7 @@ class Create extends Action $signature = $request->getHeader('x-hub-signature-256', ''); $secretKey = System::getEnv('_APP_VCS_GITHUB_WEBHOOK_SECRET', ''); - $valid = empty($secretKey) ? true : $github->validateWebhookEvent($payload, $signature, $secretKey); + $valid = !empty($secretKey) && $github->validateWebhookEvent($payload, $signature, $secretKey); Span::add('vcs.github.event.signature.valid', $valid); if (!$valid) {