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: measure log size before ANSI strip; harden tasks and volumes

Andras Bacsai60129c2c4701
diff --git a/app/Jobs/ScheduledTaskJob.php b/app/Jobs/ScheduledTaskJob.php
index 02f3daa3b4..d24f36350b 100644
--- a/app/Jobs/ScheduledTaskJob.php
+++ b/app/Jobs/ScheduledTaskJob.php
@@ -150,11 +150,12 @@ class ScheduledTaskJob implements ShouldBeEncrypted, ShouldQueue
foreach ($this->containers as $containerName) {
if (count($this->containers) == 1 || str_starts_with($containerName, $this->task->container.'-'.$this->resource->uuid)) {
$cmd = "sh -c '".str_replace("'", "'\''", $this->task->command)."'";
- $execCommand = "docker exec {$containerName} {$cmd}";
+ $dockerCommand = $this->server->isNonRoot() ? 'sudo docker' : 'docker';
+ $execCommand = "{$dockerCommand} exec {$containerName} {$cmd}";
$exec = $this->boundedTaskCommand($execCommand);
// Disable SSH multiplexing to prevent race conditions when multiple tasks run concurrently
// See: https://github.com/coollabsio/coolify/issues/6736
- $this->task_output = instant_remote_process([$exec], $this->server, true, false, $this->timeout, disableMultiplexing: true);
+ $this->task_output = instant_remote_process([$exec], $this->server, throwError: true, no_sudo: true, timeout: $this->timeout, disableMultiplexing: true);
$this->task_log->update([
'status' => 'success',
'message' => $this->task_output,
diff --git a/app/Livewire/Project/Shared/GetLogs.php b/app/Livewire/Project/Shared/GetLogs.php
index fa575073f7..67a040ef77 100644
--- a/app/Livewire/Project/Shared/GetLogs.php
+++ b/app/Livewire/Project/Shared/GetLogs.php
@@ -192,17 +192,17 @@ class GetLogs extends Component
return;
}
- $output = removeAnsiColors($output);
$remainingBytes = self::MAX_DISPLAY_SIZE_BYTES - $accumulatedBytes;
- if (strlen($output) > $remainingBytes) {
- $logChunks[] = substr($output, 0, max(0, $remainingBytes));
+ $outputBytes = strlen($output);
+ if ($outputBytes > $remainingBytes) {
+ $logChunks[] = removeAnsiColors(substr($output, 0, max(0, $remainingBytes)));
$truncated = true;
return;
}
- $logChunks[] = $output;
- $accumulatedBytes += strlen($output);
+ $logChunks[] = removeAnsiColors($output);
+ $accumulatedBytes += $outputBytes;
});
$newOutputs = implode('', $logChunks);
@@ -274,20 +274,19 @@ class GetLogs extends Component
return;
}
- $output = removeAnsiColors($output);
$outputBytes = strlen($output);
if ($accumulatedBytes + $outputBytes > self::MAX_DOWNLOAD_SIZE_BYTES) {
$remaining = self::MAX_DOWNLOAD_SIZE_BYTES - $accumulatedBytes;
if ($remaining > 0) {
- $logChunks[] = substr($output, 0, $remaining);
+ $logChunks[] = removeAnsiColors(substr($output, 0, $remaining));
}
$truncated = true;
return;
}
- $logChunks[] = $output;
+ $logChunks[] = removeAnsiColors($output);
$accumulatedBytes += $outputBytes;
});
diff --git a/bootstrap/helpers/services.php b/bootstrap/helpers/services.php
index e46769a15c..05430930b7 100644
--- a/bootstrap/helpers/services.php
+++ b/bootstrap/helpers/services.php
@@ -167,11 +167,10 @@ function getFilesystemVolumesFromServer(ServiceApplication|ServiceDatabase|Appli
$isDir = instant_remote_process(["test -d $fileLocation && echo OK || echo NOK"], $server);
if ($isFile === 'OK') {
+ $fileVolume->is_directory = false;
+ $fileVolume->save();
if ($fileVolume->is_based_on_git) {
$fileVolume->loadStorageOnServer();
- } else {
- $fileVolume->is_directory = false;
- $fileVolume->save();
}
} elseif ($isDir === 'OK') {
// If its a directory & exists
diff --git a/tests/Feature/GetLogsCommandInjectionTest.php b/tests/Feature/GetLogsCommandInjectionTest.php
index db75f7b757..c920b94ede 100644
--- a/tests/Feature/GetLogsCommandInjectionTest.php
+++ b/tests/Feature/GetLogsCommandInjectionTest.php
@@ -3,6 +3,7 @@
use App\Livewire\Project\Shared\GetLogs;
use App\Models\Application;
use App\Models\Environment;
+use App\Models\PrivateKey;
use App\Models\Project;
use App\Models\Server;
use App\Models\StandaloneDocker;
@@ -10,6 +11,8 @@ use App\Models\Team;
use App\Models\User;
use App\Support\ValidationPatterns;
use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Process\FakeProcessResult;
+use Illuminate\Support\Facades\Process;
use Livewire\Attributes\Locked;
use Livewire\Livewire;
@@ -20,7 +23,11 @@ beforeEach(function () {
$this->team = Team::factory()->create();
$this->user->teams()->attach($this->team, ['role' => 'owner']);
- $this->server = Server::factory()->create(['team_id' => $this->team->id]);
+ $privateKey = PrivateKey::factory()->create(['team_id' => $this->team->id]);
+ $this->server = Server::factory()->create([
+ 'team_id' => $this->team->id,
+ 'private_key_id' => $privateKey->id,
+ ]);
// Server::created auto-creates a StandaloneDocker, reuse it
$this->destination = StandaloneDocker::where('server_id', $this->server->id)->first();
$this->project = Project::factory()->create(['team_id' => $this->team->id]);
@@ -67,6 +74,38 @@ describe('GetLogs locked properties', function () {
});
describe('GetLogs Livewire action validation', function () {
+ test('getLogs marks ANSI-colored output truncated based on raw bytes', function () {
+ $this->server->settings->fill([
+ 'is_reachable' => true,
+ 'is_usable' => true,
+ 'force_disabled' => false,
+ ])->save();
+ $server = Server::with('settings')->find($this->server->id);
+ $output = "\e[31m".str_repeat('a', GetLogs::MAX_DISPLAY_SIZE_BYTES - 4);
+
+ expect(strlen($output))->toBe(GetLogs::MAX_DISPLAY_SIZE_BYTES + 1);
+
+ Process::shouldReceive('timeout')->once()->andReturnSelf();
+ Process::shouldReceive('run')->andReturnUsing(function (string $command, ?callable $callback = null) use ($output): FakeProcessResult {
+ if ($callback) {
+ $callback('out', $output);
+ }
+
+ return new FakeProcessResult(command: $command);
+ });
+
+ $component = new GetLogs;
+ $component->server = $server;
+ $component->resource = $this->application;
+ $component->container = 'test-container';
+ $component->showTimeStamps = false;
+ $component->getLogs(true);
+
+ expect($component->outputs)
+ ->toContain('[... Output truncated at 5MB limit ...]')
+ ->not->toContain("\e[31m");
+ });
+
test('getLogs rejects invalid container name', function () {
// Make server functional by setting settings directly
$this->server->settings->fill([
diff --git a/tests/Unit/LocalFileVolumeContentSizeTest.php b/tests/Unit/LocalFileVolumeContentSizeTest.php
index 0ad69e28aa..1954539785 100644
--- a/tests/Unit/LocalFileVolumeContentSizeTest.php
+++ b/tests/Unit/LocalFileVolumeContentSizeTest.php
@@ -77,3 +77,14 @@ it('does not read regular bind-mounted file contents while loading service setti
->toContain('if ($fileVolume->is_based_on_git)')
->toContain('$fileVolume->loadStorageOnServer();');
});
+
+it('marks git-based file volumes as files before refreshing their content', function () {
+ $helpers = file_get_contents(base_path('bootstrap/helpers/services.php'));
+ $fileBranch = str($helpers)
+ ->after("if (\$isFile === 'OK') {")
+ ->before("} elseif (\$isDir === 'OK') {");
+
+ expect($fileBranch->value())->toMatch(
+ '/\$fileVolume->is_directory = false;\s+\$fileVolume->save\(\);\s+if \(\$fileVolume->is_based_on_git\) \{/'
+ );
+});
diff --git a/tests/Unit/RemoteOutputSizeLimitsTest.php b/tests/Unit/RemoteOutputSizeLimitsTest.php
index e010970bba..b8341dd73f 100644
--- a/tests/Unit/RemoteOutputSizeLimitsTest.php
+++ b/tests/Unit/RemoteOutputSizeLimitsTest.php
@@ -5,6 +5,7 @@ use App\Jobs\ScheduledTaskJob;
use App\Livewire\Project\Shared\GetLogs;
use App\Livewire\Server\Proxy\DynamicConfigurations;
use App\Models\Application;
+use App\Models\Server;
function remoteOutputSource(string $path): string
{
@@ -77,6 +78,25 @@ it('bounds scheduled task output before storing or notifying', function () {
->and(implode("\n", $failureOutput))->toBe('failure');
});
+it('does not pass the scheduled task output wrapper through the sudo rewriter', function () {
+ $source = remoteOutputSource('app/Jobs/ScheduledTaskJob.php');
+ $reflection = new ReflectionClass(ScheduledTaskJob::class);
+ $method = $reflection->getMethod('boundedTaskCommand');
+ $command = $method->invoke($reflection->newInstanceWithoutConstructor(), 'sudo docker exec example true');
+ $server = Mockery::mock(Server::class)->makePartial();
+ $server->shouldReceive('getAttribute')->with('user')->andReturn('ubuntu');
+ $rewrittenCommand = parseCommandsByLineForSudo(collect([$command]), $server)[0];
+
+ exec('bash -n -c '.escapeshellarg($command), $output, $exitCode);
+ exec('bash -n -c '.escapeshellarg($rewrittenCommand).' 2>/dev/null', $rewrittenOutput, $rewrittenExitCode);
+
+ expect($source)
+ ->toContain("\$dockerCommand = \$this->server->isNonRoot() ? 'sudo docker' : 'docker'")
+ ->toContain('instant_remote_process([$exec], $this->server, throwError: true, no_sudo: true')
+ ->and($exitCode)->toBe(0)
+ ->and($rewrittenExitCode)->not->toBe(0);
+});
+
it('bounds proxy configuration backfill before storing it', function () {
$source = remoteOutputSource('app/Actions/Proxy/GetProxyConfiguration.php');