diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 2dd59c7b4c..7e15775d9b 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 { @@ -197,23 +198,58 @@ 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'])) { + if ($isAppwriteSource) { $this->sourceProject = $this->dbForPlatform->getDocument('projects', $credentials['projectId']); - if ($this->sourceProject->isEmpty()) { - throw new Exception(Exception::MIGRATION_SOURCE_PROJECT_NOT_FOUND); + + // 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); + $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([ + $publicDomain, + $publicDomain !== '' ? '*.' . $publicDomain : null, + $internalHost, + ]); + + if (is_string($sourceHost) && !$this->sourceProject->isEmpty()) { + $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()) { + $allowedHosts[] = $sourceHost; + } } + $isLocalEndpoint = is_string($sourceHost) + && !empty($allowedHosts) + && (new Hostname($allowedHosts))->isValid($sourceHost); + $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() + && $isLocalEndpoint + && (!$isAppwriteToAppwrite || $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 =>