From b5d59c35f432c7c8850345985146f55d6779a829 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Sun, 24 May 2026 16:25:21 +0100 Subject: [PATCH 1/8] fix(migrations): validate source URL host before DB fast path --- src/Appwrite/Platform/Workers/Migrations.php | 50 +++++++++++++++++--- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 2dd59c7b4c..03d866bb48 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -49,6 +49,7 @@ use Utopia\Platform\Action; use Utopia\Queue\Message; use Utopia\Storage\Device; use Utopia\System\System; +use Utopia\Validator\Hostname; class Migrations extends Action { @@ -202,18 +203,55 @@ class Migrations extends Action } if (! empty($credentials['projectId'])) { + $isAppwriteSource = $source === SourceAppwrite::getName(); + $isAppwriteToAppwrite = $isAppwriteSource + && $destination === DestinationAppwrite::getName(); + $this->sourceProject = $this->dbForPlatform->getDocument('projects', $credentials['projectId']); - if ($this->sourceProject->isEmpty()) { - throw new Exception(Exception::MIGRATION_SOURCE_PROJECT_NOT_FOUND); + + // Same projectId may collide with an external Appwrite — trust the DB fast + // path only when the source URL targets this cluster's public or internal host. + $sourceHost = parse_url($credentials['endpoint'] ?? '', PHP_URL_HOST); + $rawDomain = System::getEnv('_APP_DOMAIN', ''); + $rawMigrationHost = System::getEnv('_APP_MIGRATION_HOST', ''); + $localDomain = $rawDomain !== '' ? (parse_url('http://' . $rawDomain, PHP_URL_HOST) ?: '') : ''; + $migrationHost = $rawMigrationHost !== '' ? (parse_url('http://' . $rawMigrationHost, PHP_URL_HOST) ?: '') : ''; + + $allowedHosts = array_filter([ + $localDomain, + $localDomain !== '' ? '*.' . $localDomain : null, + $migrationHost, + ]); + + // Include the source project's custom API domain so customers using + // a configured custom domain still get the DB fast path. + if (is_string($sourceHost) && !$this->sourceProject->isEmpty()) { + $rule = $this->dbForPlatform->findOne('rules', [ + Query::equal('domain', [$sourceHost]), + Query::equal('type', ['api']), + Query::equal('projectInternalId', [$this->sourceProject->getSequence()]), + ]); + if (!$rule->isEmpty()) { + $allowedHosts[] = $sourceHost; + } } + // Empty endpoint: processMigration defaults it to the internal host before reaching here. + $isLocalEndpoint = (is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost)) + || (empty($credentials['endpoint']) && $migrationHost !== ''); + $sourceRegion = $this->sourceProject->getAttribute('region', 'default'); $destinationRegion = $this->project->getAttribute('region', 'default'); - $useAppwriteApiSource = $source === SourceAppwrite::getName() - && $destination === DestinationAppwrite::getName() - && $sourceRegion !== $destinationRegion; - if (! $useAppwriteApiSource) { + + $isLocalSource = !$this->sourceProject->isEmpty() + && (!$isAppwriteSource || ($isLocalEndpoint && $sourceRegion === $destinationRegion)); + + if ($isLocalSource) { $projectDB = call_user_func($this->getProjectDB, $this->sourceProject); + } elseif ($isAppwriteToAppwrite) { + $useAppwriteApiSource = true; + } else { + throw new Exception(Exception::MIGRATION_SOURCE_PROJECT_NOT_FOUND); } } $getDatabasesDB = fn (Document $database): Database => From a75ed2d949973282bcb261794569369f32b0d60d Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Sun, 24 May 2026 16:43:01 +0100 Subject: [PATCH 2/8] Drop redundant empty-endpoint check from host validation --- src/Appwrite/Platform/Workers/Migrations.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 03d866bb48..1495eec5a1 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -236,9 +236,7 @@ class Migrations extends Action } } - // Empty endpoint: processMigration defaults it to the internal host before reaching here. - $isLocalEndpoint = (is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost)) - || (empty($credentials['endpoint']) && $migrationHost !== ''); + $isLocalEndpoint = is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost); $sourceRegion = $this->sourceProject->getAttribute('region', 'default'); $destinationRegion = $this->project->getAttribute('region', 'default'); From 735693572d1a63bbddb25e6ba9a9daf86895f0d1 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Sun, 24 May 2026 18:55:23 +0100 Subject: [PATCH 3/8] Scope region check to Appwrite-to-Appwrite only --- src/Appwrite/Platform/Workers/Migrations.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 1495eec5a1..eda00c6de7 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -242,7 +242,8 @@ class Migrations extends Action $destinationRegion = $this->project->getAttribute('region', 'default'); $isLocalSource = !$this->sourceProject->isEmpty() - && (!$isAppwriteSource || ($isLocalEndpoint && $sourceRegion === $destinationRegion)); + && (!$isAppwriteSource || $isLocalEndpoint) + && (!$isAppwriteToAppwrite || $sourceRegion === $destinationRegion); if ($isLocalSource) { $projectDB = call_user_func($this->getProjectDB, $this->sourceProject); From cd2506ada96063922b8c9b60b691b73dd7b447c7 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Sun, 24 May 2026 18:58:40 +0100 Subject: [PATCH 4/8] Restore empty-endpoint handling for internal-host fallback --- src/Appwrite/Platform/Workers/Migrations.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index eda00c6de7..542ed4d045 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -236,7 +236,10 @@ class Migrations extends Action } } - $isLocalEndpoint = is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost); + // Empty-string endpoint slips through processMigration's `??` defaulting (which only + // catches null/absent); treat it as targeting the internal migration host. + $isLocalEndpoint = (is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost)) + || (empty($credentials['endpoint']) && $migrationHost !== ''); $sourceRegion = $this->sourceProject->getAttribute('region', 'default'); $destinationRegion = $this->project->getAttribute('region', 'default'); From 3e03bd512a49b6bc1df798d60ec248ce60cac72f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Sun, 24 May 2026 19:11:14 +0100 Subject: [PATCH 5/8] Drop unreachable empty-endpoint branch from host check --- src/Appwrite/Platform/Workers/Migrations.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 542ed4d045..eda00c6de7 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -236,10 +236,7 @@ class Migrations extends Action } } - // Empty-string endpoint slips through processMigration's `??` defaulting (which only - // catches null/absent); treat it as targeting the internal migration host. - $isLocalEndpoint = (is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost)) - || (empty($credentials['endpoint']) && $migrationHost !== ''); + $isLocalEndpoint = is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost); $sourceRegion = $this->sourceProject->getAttribute('region', 'default'); $destinationRegion = $this->project->getAttribute('region', 'default'); From 852eb43585ca1b3949fa414d53884c3792dc8ea9 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 25 May 2026 08:01:14 +0100 Subject: [PATCH 6/8] Scope source-resolution block to Appwrite sources only --- src/Appwrite/Platform/Workers/Migrations.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index eda00c6de7..b5d0737281 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -198,15 +198,15 @@ class Migrations extends Action /** @var Database|null $projectDB */ $projectDB = null; $useAppwriteApiSource = false; - if ($source === SourceAppwrite::getName() && empty($credentials['projectId'])) { + $isAppwriteSource = $source === SourceAppwrite::getName(); + $isAppwriteToAppwrite = $isAppwriteSource + && $destination === DestinationAppwrite::getName(); + + if ($isAppwriteSource && empty($credentials['projectId'])) { throw new Exception(Exception::MIGRATION_SOURCE_PROJECT_ID_REQUIRED); } - if (! empty($credentials['projectId'])) { - $isAppwriteSource = $source === SourceAppwrite::getName(); - $isAppwriteToAppwrite = $isAppwriteSource - && $destination === DestinationAppwrite::getName(); - + if ($isAppwriteSource) { $this->sourceProject = $this->dbForPlatform->getDocument('projects', $credentials['projectId']); // Same projectId may collide with an external Appwrite — trust the DB fast @@ -242,7 +242,7 @@ class Migrations extends Action $destinationRegion = $this->project->getAttribute('region', 'default'); $isLocalSource = !$this->sourceProject->isEmpty() - && (!$isAppwriteSource || $isLocalEndpoint) + && $isLocalEndpoint && (!$isAppwriteToAppwrite || $sourceRegion === $destinationRegion); if ($isLocalSource) { From f380927061fe0b466e584f6a31e06abeab09145d Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 25 May 2026 08:51:20 +0100 Subject: [PATCH 7/8] Tighten env normalization and variable naming --- src/Appwrite/Platform/Workers/Migrations.php | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index b5d0737281..91a78dcda5 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -209,22 +209,18 @@ class Migrations extends Action if ($isAppwriteSource) { $this->sourceProject = $this->dbForPlatform->getDocument('projects', $credentials['projectId']); - // Same projectId may collide with an external Appwrite — trust the DB fast - // path only when the source URL targets this cluster's public or internal host. + // Trust DB fast path only when the source URL targets this cluster's host + // (env-configured or this project's verified custom API domain). $sourceHost = parse_url($credentials['endpoint'] ?? '', PHP_URL_HOST); - $rawDomain = System::getEnv('_APP_DOMAIN', ''); - $rawMigrationHost = System::getEnv('_APP_MIGRATION_HOST', ''); - $localDomain = $rawDomain !== '' ? (parse_url('http://' . $rawDomain, PHP_URL_HOST) ?: '') : ''; - $migrationHost = $rawMigrationHost !== '' ? (parse_url('http://' . $rawMigrationHost, PHP_URL_HOST) ?: '') : ''; + $publicDomain = parse_url('http://' . System::getEnv('_APP_DOMAIN', ''), PHP_URL_HOST) ?: ''; + $internalHost = parse_url('http://' . System::getEnv('_APP_MIGRATION_HOST', ''), PHP_URL_HOST) ?: ''; $allowedHosts = array_filter([ - $localDomain, - $localDomain !== '' ? '*.' . $localDomain : null, - $migrationHost, + $publicDomain, + $publicDomain !== '' ? '*.' . $publicDomain : null, + $internalHost, ]); - // Include the source project's custom API domain so customers using - // a configured custom domain still get the DB fast path. if (is_string($sourceHost) && !$this->sourceProject->isEmpty()) { $rule = $this->dbForPlatform->findOne('rules', [ Query::equal('domain', [$sourceHost]), @@ -236,7 +232,9 @@ class Migrations extends Action } } - $isLocalEndpoint = is_string($sourceHost) && !empty($allowedHosts) && (new Hostname($allowedHosts))->isValid($sourceHost); + $isLocalEndpoint = is_string($sourceHost) + && !empty($allowedHosts) + && (new Hostname($allowedHosts))->isValid($sourceHost); $sourceRegion = $this->sourceProject->getAttribute('region', 'default'); $destinationRegion = $this->project->getAttribute('region', 'default'); From bcbebe657c7edf17fd64970cda28cd5faa4d6a73 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 25 May 2026 09:14:13 +0100 Subject: [PATCH 8/8] Filter custom-domain rules by verified status --- src/Appwrite/Platform/Workers/Migrations.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 91a78dcda5..7e15775d9b 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -225,6 +225,7 @@ class Migrations extends Action $rule = $this->dbForPlatform->findOne('rules', [ Query::equal('domain', [$sourceHost]), Query::equal('type', ['api']), + Query::equal('status', [RULE_STATUS_VERIFIED]), Query::equal('projectInternalId', [$this->sourceProject->getSequence()]), ]); if (!$rule->isEmpty()) {