Skip to content

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.

fix(service): write large compose files through SSH stdin

Andras Bacsai236116148746
diff --git a/app/Helpers/SshMultiplexingHelper.php b/app/Helpers/SshMultiplexingHelper.php
index 2d7289d43a..f29d97058e 100644
--- a/app/Helpers/SshMultiplexingHelper.php
+++ b/app/Helpers/SshMultiplexingHelper.php
@@ -209,6 +209,30 @@ class SshMultiplexingHelper
}
public static function generateSshCommand(Server $server, string $command, bool $disableMultiplexing = false, ?int $commandTimeout = null): string
+ {
+ $sshCommand = self::sshCommandPrefix($server, $disableMultiplexing, $commandTimeout);
+
+ $delimiter = base64_encode(Hash::make($command));
+ $command = str_replace($delimiter, '', $command);
+ $remoteShellCommand = self::remoteShellCommand();
+
+ return $sshCommand.self::escapedUserAtHost($server)." '{$remoteShellCommand}' << \\$delimiter".PHP_EOL
+ .$command.PHP_EOL
+ .$delimiter;
+ }
+
+ /**
+ * Build an SSH command that runs one short remote command and leaves stdin free for data.
+ * Large content must go through stdin: Linux limits one exec argument to 128 KiB, and
+ * generateSshCommand() puts the whole script into one `sh -c` argument.
+ */
+ public static function generateSshStdinCommand(Server $server, string $remoteCommand, bool $disableMultiplexing = false, ?int $commandTimeout = null): string
+ {
+ return self::sshCommandPrefix($server, $disableMultiplexing, $commandTimeout)
+ .self::escapedUserAtHost($server).' '.escapeshellarg($remoteCommand);
+ }
+
+ private static function sshCommandPrefix(Server $server, bool $disableMultiplexing, ?int $commandTimeout): string
{
if ($server->settings->force_disabled) {
throw new \RuntimeException('Server is disabled.');
@@ -239,15 +263,7 @@ class SshMultiplexingHelper
$sshCommand .= "-o ProxyCommand='cloudflared access ssh --hostname %h' ";
}
- $sshCommand .= self::getCommonSshOptions($server, $sshKeyLocation, self::getConnectionTimeout($server), config('constants.ssh.server_interval'));
-
- $delimiter = base64_encode(Hash::make($command));
- $command = str_replace($delimiter, '', $command);
- $remoteShellCommand = self::remoteShellCommand();
-
- return $sshCommand.self::escapedUserAtHost($server)." '{$remoteShellCommand}' << \\$delimiter".PHP_EOL
- .$command.PHP_EOL
- .$delimiter;
+ return $sshCommand.self::getCommonSshOptions($server, $sshKeyLocation, self::getConnectionTimeout($server), config('constants.ssh.server_interval'));
}
/**
diff --git a/app/Models/Service.php b/app/Models/Service.php
index ad28ad8139..c5ddeb5941 100644
--- a/app/Models/Service.php
+++ b/app/Models/Service.php
@@ -1588,10 +1588,9 @@ class Service extends BaseModel
$workdir = $this->workdir();
// Absolute paths and tee, no cd or scp: a non-root SSH user cannot enter /data/coolify on the Coolify host.
- $commands = [
- "mkdir -p $workdir",
- "echo '".base64_encode($this->docker_compose)."' | base64 -d | tee $workdir/docker-compose.yml > /dev/null",
- ];
+ // File content goes over SSH stdin: inline in the command, a compose over ~96 KB exceeds the argument limit.
+ instant_remote_process(["mkdir -p $workdir"], $this->server);
+ instant_remote_write_file($this->server, "$workdir/docker-compose.yml", $this->docker_compose);
$environmentFile = "$workdir/".new_public_id().'.env.tmp';
$envs = collect([]);
@@ -1622,15 +1621,8 @@ class Service extends BaseModel
foreach ($sorted as $env) {
$envs->push("{$env->key}={$this->resolveSecretManagerDotenvValue($env)}");
}
- if ($envs->count() === 0) {
- $commands[] = "touch {$environmentFile}";
- } else {
- $envs_base64 = base64_encode($envs->implode("\n"));
- $commands[] = "echo '$envs_base64' | base64 -d | tee {$environmentFile} > /dev/null";
- }
- $commands[] = "mv {$environmentFile} $workdir/.env";
-
- instant_remote_process($commands, $this->server);
+ instant_remote_write_file($this->server, $environmentFile, $envs->implode("\n"));
+ instant_remote_process(["mv {$environmentFile} $workdir/.env"], $this->server);
}
public function parse(bool $isNew = false): Collection
diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php
index da3b6215ed..8f4eb84be6 100644
--- a/bootstrap/helpers/remoteProcess.php
+++ b/bootstrap/helpers/remoteProcess.php
@@ -137,6 +137,34 @@ function instant_scp_from_server(string $remoteSource, string $localDest, Server
);
}
+/**
+ * Write content to a file on a server through SSH stdin. The content never becomes part of a command
+ * line, so it is not limited by the 128 KiB per-argument limit of Linux. A non-root SSH user writes
+ * with `sudo tee` because Coolify directories such as /data/coolify are root-only.
+ */
+function instant_remote_write_file(Server $server, string $path, string $content, bool $throwError = true): void
+{
+ $remoteCommand = ($server->isNonRoot() ? 'sudo ' : '').'tee '.escapeshellarg($path).' > /dev/null';
+ $timeout = (int) config('constants.ssh.command_timeout');
+
+ SshRetryHandler::retry(
+ function () use ($server, $remoteCommand, $content, $timeout) {
+ $sshCommand = SshMultiplexingHelper::generateSshStdinCommand($server, $remoteCommand, commandTimeout: $timeout);
+ $process = Process::timeout($timeout)->input($content)->run($sshCommand);
+
+ if ($process->exitCode() !== 0) {
+ excludeCertainErrors($process->errorOutput(), $process->exitCode());
+ }
+ },
+ [
+ 'server' => $server->ip,
+ 'dest' => $path,
+ 'function' => 'instant_remote_write_file',
+ ],
+ $throwError
+ );
+}
+
function instant_remote_process_with_timeout(Collection|array $command, Server $server, bool $throwError = true, bool $no_sudo = false): ?string
{
$command = $command instanceof Collection ? $command->toArray() : $command;
diff --git a/tests/Feature/NonRootDataDirectoryCommandsTest.php b/tests/Feature/NonRootDataDirectoryCommandsTest.php
index 2849854e29..a155b2605d 100644
--- a/tests/Feature/NonRootDataDirectoryCommandsTest.php
+++ b/tests/Feature/NonRootDataDirectoryCommandsTest.php
@@ -93,7 +93,7 @@ it('writes service compose files without cd or scp on a non-root server', functi
$lines = nonRootDataDirectoryRemoteLines();
expectNoUnprivilegedDataDirectoryAccess($lines);
expect(implode("\n", $lines))
- ->toContain("| sudo tee {$workdir}/docker-compose.yml > /dev/null")
+ ->toContain(escapeshellarg("sudo tee '{$workdir}/docker-compose.yml' > /dev/null"))
->toMatch('/^sudo mv '.preg_quote($workdir, '/').'\/\S+\.env\.tmp '.preg_quote($workdir, '/').'\/\.env$/m');
});
diff --git a/tests/Feature/SecretManagers/RemoteSecretResourceLifecycleTest.php b/tests/Feature/SecretManagers/RemoteSecretResourceLifecycleTest.php
index 986bec92e9..1a8d128001 100644
--- a/tests/Feature/SecretManagers/RemoteSecretResourceLifecycleTest.php
+++ b/tests/Feature/SecretManagers/RemoteSecretResourceLifecycleTest.php
@@ -104,18 +104,19 @@ function remoteSecretLifecyclePostgres(): StandalonePostgresql
*/
function remoteSecretLifecycleWrittenEnvFile(Service $service): string
{
- $commands = collect();
- Process::fake(function ($process) use ($commands) {
- $commands->push($process->command);
+ $envFileContent = '';
+ Process::fake(function ($process) use (&$envFileContent) {
+ // The .env content goes to `tee <workdir>/<id>.env.tmp` over SSH stdin.
+ if (preg_match('/tee \S+\.env\.tmp/', $process->command)) {
+ $envFileContent = (string) $process->input;
+ }
return Process::result(output: '');
});
$service->saveComposeConfigs();
- preg_match("/echo '([A-Za-z0-9+\/=]+)' \| base64 -d \| tee [^ ]+\.env\.tmp/", $commands->implode("\n"), $matches);
-
- return base64_decode($matches[1] ?? '');
+ return $envFileContent;
}
/*
diff --git a/tests/Feature/ServiceComposeStdinWriteTest.php b/tests/Feature/ServiceComposeStdinWriteTest.php
new file mode 100644
index 0000000000..2f7844c05f
--- /dev/null
+++ b/tests/Feature/ServiceComposeStdinWriteTest.php
@@ -0,0 +1,80 @@
+<?php
+
+use App\Models\Environment;
+use App\Models\InstanceSettings;
+use App\Models\PrivateKey;
+use App\Models\Project;
+use App\Models\Server;
+use App\Models\Service;
+use App\Models\StandaloneDocker;
+use App\Models\Team;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Process\PendingProcess;
+use Illuminate\Support\Facades\Process;
+use Illuminate\Support\Facades\Storage;
+
+uses(RefreshDatabase::class);
+
+/**
+ * Linux limits one exec argument to 128 KiB (MAX_ARG_STRLEN). generateSshCommand() puts the whole
+ * remote script in one `sh -c` argument, so file content must reach the server over SSH stdin.
+ */
+beforeEach(function () {
+ Server::flushIdentityMap();
+ InstanceSettings::forceCreate(['id' => 0]);
+ Storage::fake('ssh-keys');
+ Storage::fake('ssh-mux');
+ config(['app.maintenance.store' => 'array', 'constants.ssh.mux_enabled' => false, 'constants.ssh.max_retries' => 1]);
+
+ $this->processes = collect();
+ Process::fake(function (PendingProcess $process) {
+ $this->processes->push(['command' => $process->command, 'input' => $process->input]);
+
+ return Process::result();
+ });
+
+ $team = Team::factory()->create();
+ $this->server = Server::factory()->create([
+ 'team_id' => $team->id,
+ 'private_key_id' => PrivateKey::factory()->create(['team_id' => $team->id])->id,
+ ]);
+ $this->server->settings->update(['is_reachable' => true, 'is_usable' => true, 'force_disabled' => false]);
+ $destination = StandaloneDocker::query()->where('server_id', $this->server->id)->firstOrFail();
+ $project = Project::factory()->create(['team_id' => $team->id]);
+ $environment = Environment::factory()->create(['project_id' => $project->id]);
+
+ $labels = collect(range(1, 4000))
+ ->map(fn (int $i) => " - coolify.test.label{$i}=".str_repeat('x', 40))
+ ->implode("\n");
+ $this->compose = "services:\n app:\n image: nginx\n labels:\n{$labels}\n";
+
+ $this->service = Service::factory()->create([
+ 'environment_id' => $environment->id,
+ 'server_id' => $this->server->id,
+ 'destination_id' => $destination->id,
+ 'destination_type' => $destination->getMorphClass(),
+ 'docker_compose' => $this->compose,
+ ]);
+});
+
+it('streams a large compose file over stdin instead of the command line', function (string $user, string $tee) {
+ $this->server->update(['user' => $user]);
+ $workdir = $this->service->workdir();
+ expect(strlen($this->compose))->toBeGreaterThan(200 * 1024);
+
+ $this->service->saveComposeConfigs();
+
+ $composeWrite = $this->processes->firstWhere('input', $this->compose);
+ expect($composeWrite)->not->toBeNull()
+ ->and($composeWrite['command'])->toEndWith(escapeshellarg("{$tee} '{$workdir}/docker-compose.yml' > /dev/null"));
+
+ $envWrite = $this->processes->first(fn (array $process) => $process['input'] === 'SERVICE_NAME_APP=app');
+ expect($envWrite)->not->toBeNull()
+ ->and($envWrite['command'])->toMatch('/'.preg_quote($tee, '/').' \S+\.env\.tmp/');
+
+ $this->processes->each(fn (array $process) => expect(strlen($process['command']))->toBeLessThan(8 * 1024));
+ expect($this->processes->last()['command'])->toMatch('/mv \S+\.env\.tmp '.preg_quote($workdir, '/').'\/\.env/');
+})->with([
+ 'root' => ['root', 'tee'],
+ 'non-root' => ['cooluser', 'sudo tee'],
+]);