An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
fix(git): ensure ssh credentials are propagated to submodule operations
Andras Bacsaidcd976ae065e
diff --git a/app/Models/Application.php b/app/Models/Application.phpindex 34ab4141ea..e622c9d541 100644--- a/app/Models/Application.php+++ b/app/Models/Application.php@@ -1087,12 +1087,14 @@ class Application extends BaseModelreturn application_configuration_dir()."/{$this->uuid}";}- public function setGitImportSettings(string $deployment_uuid, string $git_clone_command, bool $public = false, ?string $commit = null)+ public function setGitImportSettings(string $deployment_uuid, string $git_clone_command, bool $public = false, ?string $commit = null, ?string $gitSshCommand = null){$baseDir = $this->generateBaseDir($deployment_uuid);$escapedBaseDir = escapeshellarg($baseDir);$isShallowCloneEnabled = $this->settings?->is_git_shallow_clone_enabled ?? false;+ $sshCommand = $gitSshCommand ?? 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';+// Use the explicitly passed commit (e.g. from rollback), falling back to the application's git_commit_sha.// Invalid refs will cause the git checkout/fetch command to fail on the remote server.$commitToUse = $commit ?? $this->git_commit_sha;@@ -1102,9 +1104,9 @@ class Application extends BaseModel// If shallow clone is enabled and we need a specific commit,// we need to fetch that specific commit with depth=1if ($isShallowCloneEnabled) {- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git fetch --depth=1 origin {$escapedCommit} && git -c advice.detachedHead=false checkout {$escapedCommit} >/dev/null 2>&1";+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$sshCommand}\" git fetch --depth=1 origin {$escapedCommit} && git -c advice.detachedHead=false checkout {$escapedCommit} >/dev/null 2>&1";} else {- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git -c advice.detachedHead=false checkout {$escapedCommit} >/dev/null 2>&1";+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$sshCommand}\" git -c advice.detachedHead=false checkout {$escapedCommit} >/dev/null 2>&1";}}if ($this->settings->is_git_submodules_enabled) {@@ -1115,10 +1117,10 @@ class Application extends BaseModel}// Add shallow submodules flag if shallow clone is enabled$submoduleFlags = $isShallowCloneEnabled ? '--depth=1' : '';- $git_clone_command = "{$git_clone_command} git submodule sync && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git submodule update --init --recursive {$submoduleFlags}; fi";+ $git_clone_command = "{$git_clone_command} git submodule sync && GIT_SSH_COMMAND=\"{$sshCommand}\" git submodule update --init --recursive {$submoduleFlags}; fi";}if ($this->settings->is_git_lfs_enabled) {- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\" git lfs pull";+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$sshCommand}\" git lfs pull";}return $git_clone_command;@@ -1301,12 +1303,18 @@ class Application extends BaseModel}} else {$github_access_token = generateGithubInstallationToken($this->source);+ // Configure git to rewrite URLs with the token so submodules on the same host authenticate correctly+ $escapedTokenUrl = escapeshellarg("{$source_html_url_scheme}://x-access-token:{$github_access_token}@{$source_html_url_host}/");+ $escapedHostUrl = escapeshellarg("{$source_html_url_scheme}://{$source_html_url_host}/");+ $gitConfigCommand = "git config --global url.{$escapedTokenUrl}.insteadOf {$escapedHostUrl}";if ($exec_in_docker) {+ $commands->push(executeInDocker($deployment_uuid, $gitConfigCommand));$repoUrl = "$source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$customRepository}.git";$escapedRepoUrl = escapeshellarg($repoUrl);$git_clone_command = "{$git_clone_command} {$escapedRepoUrl} {$escapedBaseDir}";$fullRepoUrl = $repoUrl;} else {+ $commands->push($gitConfigCommand);$repoUrl = "$source_html_url_scheme://x-access-token:$github_access_token@$source_html_url_host/{$customRepository}";$escapedRepoUrl = escapeshellarg($repoUrl);$git_clone_command = "{$git_clone_command} {$escapedRepoUrl} {$escapedBaseDir}";@@ -1348,11 +1356,12 @@ class Application extends BaseModel}$private_key = base64_encode($private_key);$escapedCustomRepository = escapeshellarg($customRepository);- $git_clone_command_base = "GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" {$git_clone_command} {$escapedCustomRepository} {$escapedBaseDir}";+ $deployKeySshCommand = "ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa";+ $git_clone_command_base = "GIT_SSH_COMMAND=\"{$deployKeySshCommand}\" {$git_clone_command} {$escapedCustomRepository} {$escapedBaseDir}";if ($only_checkout) {$git_clone_command = $git_clone_command_base;} else {- $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command_base, commit: $commit);+ $git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command_base, commit: $commit, gitSshCommand: $deployKeySshCommand);}if ($exec_in_docker) {$commands = collect([@@ -1375,7 +1384,7 @@ class Application extends BaseModel} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$deployKeySshCommand}\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name, $deployKeySshCommand);} elseif ($git_type === 'github' || $git_type === 'gitea') {$branch = "pull/{$pull_request_id}/head:$pr_branch_name";if ($exec_in_docker) {@@ -1383,14 +1392,14 @@ class Application extends BaseModel} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$deployKeySshCommand}\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name, $deployKeySshCommand);} elseif ($git_type === 'bitbucket') {if ($exec_in_docker) {$commands->push(executeInDocker($deployment_uuid, "echo 'Checking out $branch'"));} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" ".$this->buildGitCheckoutCommand($commit);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$deployKeySshCommand}\" ".$this->buildGitCheckoutCommand($commit, $deployKeySshCommand);}}@@ -1411,6 +1420,7 @@ class Application extends BaseModel$escapedCustomRepository = escapeshellarg($customRepository);$git_clone_command = "{$git_clone_command} {$escapedCustomRepository} {$escapedBaseDir}";$git_clone_command = $this->setGitImportSettings($deployment_uuid, $git_clone_command, public: true, commit: $commit);+ $otherSshCommand = "ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa";if ($pull_request_id !== 0) {if ($git_type === 'gitlab') {@@ -1420,7 +1430,7 @@ class Application extends BaseModel} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$otherSshCommand}\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name, $otherSshCommand);} elseif ($git_type === 'github' || $git_type === 'gitea') {$branch = "pull/{$pull_request_id}/head:$pr_branch_name";if ($exec_in_docker) {@@ -1428,14 +1438,14 @@ class Application extends BaseModel} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$otherSshCommand}\" git fetch origin $branch && ".$this->buildGitCheckoutCommand($pr_branch_name, $otherSshCommand);} elseif ($git_type === 'bitbucket') {if ($exec_in_docker) {$commands->push(executeInDocker($deployment_uuid, "echo 'Checking out $branch'"));} else {$commands->push("echo 'Checking out $branch'");}- $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"ssh -o ConnectTimeout=30 -p {$customPort} -o Port={$customPort} -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa\" ".$this->buildGitCheckoutCommand($commit);+ $git_clone_command = "{$git_clone_command} && cd {$escapedBaseDir} && GIT_SSH_COMMAND=\"{$otherSshCommand}\" ".$this->buildGitCheckoutCommand($commit, $otherSshCommand);}}@@ -1684,13 +1694,14 @@ class Application extends BaseModel);}- protected function buildGitCheckoutCommand($target): string+ protected function buildGitCheckoutCommand($target, ?string $gitSshCommand = null): string{$escapedTarget = escapeshellarg($target);$command = "git checkout {$escapedTarget}";if ($this->settings->is_git_submodules_enabled) {- $command .= ' && git submodule update --init --recursive';+ $sshCommand = $gitSshCommand ?? 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';+ $command .= " && GIT_SSH_COMMAND=\"{$sshCommand}\" git submodule update --init --recursive";}return $command;diff --git a/openapi.json b/openapi.jsonindex 69f5ef53d0..849dee3631 100644--- a/openapi.json+++ b/openapi.json@@ -3339,6 +3339,15 @@"schema": {"type": "string"}+ },+ {+ "name": "docker_cleanup",+ "in": "query",+ "description": "Perform docker cleanup (prune networks, volumes, etc.).",+ "schema": {+ "type": "boolean",+ "default": true+ }}],"responses": {@@ -5864,6 +5873,15 @@"schema": {"type": "string"}+ },+ {+ "name": "docker_cleanup",+ "in": "query",+ "description": "Perform docker cleanup (prune networks, volumes, etc.).",+ "schema": {+ "type": "boolean",+ "default": true+ }}],"responses": {@@ -10561,6 +10579,15 @@"schema": {"type": "string"}+ },+ {+ "name": "docker_cleanup",+ "in": "query",+ "description": "Perform docker cleanup (prune networks, volumes, etc.).",+ "schema": {+ "type": "boolean",+ "default": true+ }}],"responses": {diff --git a/openapi.yaml b/openapi.yamlindex fab3df54e6..226295cdba 100644--- a/openapi.yaml+++ b/openapi.yaml@@ -2111,6 +2111,13 @@ paths:required: trueschema:type: string+ -+ name: docker_cleanup+ in: query+ description: 'Perform docker cleanup (prune networks, volumes, etc.).'+ schema:+ type: boolean+ default: trueresponses:'200':description: 'Stop application.'@@ -3806,6 +3813,13 @@ paths:required: trueschema:type: string+ -+ name: docker_cleanup+ in: query+ description: 'Perform docker cleanup (prune networks, volumes, etc.).'+ schema:+ type: boolean+ default: trueresponses:'200':description: 'Stop database.'@@ -6645,6 +6659,13 @@ paths:required: trueschema:type: string+ -+ name: docker_cleanup+ in: query+ description: 'Perform docker cleanup (prune networks, volumes, etc.).'+ schema:+ type: boolean+ default: trueresponses:'200':description: 'Stop service.'diff --git a/tests/Unit/GitSubmoduleCredentialTest.php b/tests/Unit/GitSubmoduleCredentialTest.phpnew file mode 100644index 0000000000..1adaf735fa--- /dev/null+++ b/tests/Unit/GitSubmoduleCredentialTest.php@@ -0,0 +1,122 @@+<?php++use App\Models\Application;+use App\Models\ApplicationSetting;++describe('Git submodule credential propagation', function () {+ beforeEach(function () {+ $this->application = new Application;+ $this->application->forceFill([+ 'uuid' => 'test-app-uuid',+ 'git_commit_sha' => 'HEAD',+ ]);++ $settings = new ApplicationSetting;+ $settings->is_git_shallow_clone_enabled = false;+ $settings->is_git_submodules_enabled = true;+ $settings->is_git_lfs_enabled = false;+ $this->application->setRelation('settings', $settings);+ });++ test('setGitImportSettings uses provided gitSshCommand for submodule update', function () {+ $sshCommand = 'ssh -o ConnectTimeout=30 -p 22 -o Port=22 -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa';++ $result = $this->application->setGitImportSettings(+ deployment_uuid: 'test-uuid',+ git_clone_command: 'git clone',+ public: false,+ gitSshCommand: $sshCommand+ );++ expect($result)+ ->toContain('GIT_SSH_COMMAND="'.$sshCommand.'" git submodule update --init --recursive')+ ->toContain('git submodule sync');+ });++ test('setGitImportSettings uses default ssh command when no gitSshCommand provided', function () {+ $result = $this->application->setGitImportSettings(+ deployment_uuid: 'test-uuid',+ git_clone_command: 'git clone',+ public: false,+ );++ expect($result)+ ->toContain('GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" git submodule update --init --recursive');+ });++ test('setGitImportSettings uses provided gitSshCommand for fetch and checkout', function () {+ $this->application->git_commit_sha = 'abc123def456';+ $sshCommand = 'ssh -o ConnectTimeout=30 -p 22 -o Port=22 -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa';++ $result = $this->application->setGitImportSettings(+ deployment_uuid: 'test-uuid',+ git_clone_command: 'git clone',+ public: false,+ gitSshCommand: $sshCommand+ );++ expect($result)+ ->toContain('GIT_SSH_COMMAND="'.$sshCommand.'" git -c advice.detachedHead=false checkout');+ });++ test('setGitImportSettings uses provided gitSshCommand for shallow fetch', function () {+ $this->application->git_commit_sha = 'abc123def456';+ $this->application->settings->is_git_shallow_clone_enabled = true;+ $sshCommand = 'ssh -o ConnectTimeout=30 -p 22 -o Port=22 -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i /root/.ssh/id_rsa';++ $result = $this->application->setGitImportSettings(+ deployment_uuid: 'test-uuid',+ git_clone_command: 'git clone',+ public: false,+ gitSshCommand: $sshCommand+ );++ expect($result)+ ->toContain('GIT_SSH_COMMAND="'.$sshCommand.'" git fetch --depth=1 origin');+ });++ test('setGitImportSettings uses provided gitSshCommand for lfs pull', function () {+ $this->application->settings->is_git_lfs_enabled = true;+ $sshCommand = 'ssh -o ConnectTimeout=30 -p 22 -i /root/.ssh/id_rsa';++ $result = $this->application->setGitImportSettings(+ deployment_uuid: 'test-uuid',+ git_clone_command: 'git clone',+ public: false,+ gitSshCommand: $sshCommand+ );++ expect($result)+ ->toContain('GIT_SSH_COMMAND="'.$sshCommand.'" git lfs pull');+ });++ test('buildGitCheckoutCommand includes GIT_SSH_COMMAND for submodule update when provided', function () {+ $sshCommand = 'ssh -o ConnectTimeout=30 -p 22 -i /root/.ssh/id_rsa';++ $method = new ReflectionMethod($this->application, 'buildGitCheckoutCommand');+ $result = $method->invoke($this->application, 'main', $sshCommand);++ expect($result)+ ->toContain("git checkout 'main'")+ ->toContain('GIT_SSH_COMMAND="'.$sshCommand.'" git submodule update --init --recursive');+ });++ test('buildGitCheckoutCommand uses default ssh command for submodule update when none provided', function () {+ $method = new ReflectionMethod($this->application, 'buildGitCheckoutCommand');+ $result = $method->invoke($this->application, 'main');++ expect($result)+ ->toContain('GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" git submodule update --init --recursive');+ });++ test('buildGitCheckoutCommand omits submodule update when submodules disabled', function () {+ $this->application->settings->is_git_submodules_enabled = false;++ $method = new ReflectionMethod($this->application, 'buildGitCheckoutCommand');+ $result = $method->invoke($this->application, 'main');++ expect($result)+ ->toContain("git checkout 'main'")+ ->not->toContain('submodule');+ });+});