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(database): restore custom PostgreSQL backup archives (#11481)

Andras Bacsaid9f0820a6f01
diff --git a/app/Livewire/Project/Database/ImportForm.php b/app/Livewire/Project/Database/ImportForm.php
index ccd3435106..5b8d90e2bb 100644
--- a/app/Livewire/Project/Database/ImportForm.php
+++ b/app/Livewire/Project/Database/ImportForm.php
@@ -796,6 +796,8 @@ EOD;
*
* Hardened against bypasses:
* - decompresses gzip backups before scanning,
+ * - converts custom-format (PGDMP) archives to SQL with pg_restore
+ * before scanning, and rejects archives that cannot be inspected,
* - strips `--` line comments and flattens newlines so multi-line and
* comment-separated payloads (e.g. `FROM/**​/PROGRAM`) are caught,
* - matches a literal `\!` shell escape and `\o|`/`\g|` pipe redirects.
@@ -817,8 +819,30 @@ EOD;
$escapedSqlPattern = escapeshellarg($sqlPattern);
$escapedPsqlPattern = escapeshellarg($psqlPattern);
$contents = "{ gunzip -cf {$escapedTmpPath} 2>/dev/null || cat {$escapedTmpPath}; }";
-
- return "header=\$({$contents} | head -c 5); if [ \"\$header\" = 'PGDMP' ]; then exit 0; fi; if {$contents} | sed 's/--.*//' | grep -Eiq {$escapedPsqlPattern} || {$contents} | sed 's/--.*//' | tr '\n\r\t' ' ' | grep -Eiq {$escapedSqlPattern}; then echo 'Blocked PostgreSQL restore: COPY ... PROGRAM and psql shell commands are not allowed.'; exit 1; fi";
+ $scan = static fn (string $source): string => "{$source} | sed 's/--.*//' | grep -Eiq {$escapedPsqlPattern} || {$source} | sed 's/--.*//' | tr '\\n\\r\\t' ' ' | grep -Eiq {$escapedSqlPattern}";
+ $customScan = $scan('pg_restore -f - "$inspect" 2>/dev/null');
+ $sqlScan = $scan($contents);
+ $blockedProgram = 'echo \'Blocked PostgreSQL restore: COPY ... PROGRAM and psql shell commands are not allowed.\'; exit 1';
+ $blockedInspect = 'echo \'Blocked PostgreSQL restore: unable to inspect custom archive.\'; exit 1';
+
+ return <<<SH
+header=\$({$contents} | head -c 5)
+if [ "\$header" = 'PGDMP' ]; then
+ inspect=\$(mktemp)
+ trap 'rm -f "\$inspect"' EXIT
+ if ! {$contents} > "\$inspect"; then
+ {$blockedInspect}
+ fi
+ if ! pg_restore -l "\$inspect" >/dev/null 2>&1; then
+ {$blockedInspect}
+ fi
+ if {$customScan}; then
+ {$blockedProgram}
+ fi
+elif {$sqlScan}; then
+ {$blockedProgram}
+fi
+SH;
}
private function addRestoreSafetyCheckCommand(array &$commands, string $tmpPath): void
@@ -883,7 +907,7 @@ EOD;
case 'postgresql':
$restoreCommand = $this->postgresqlRestoreCommand;
if ($this->dumpAll) {
- $restoreCommand .= " && (gunzip -cf {$escapedTmpPath} 2>/dev/null || cat {$escapedTmpPath}) | psql -U \${POSTGRES_USER} -d \${POSTGRES_DB:-\${POSTGRES_USER:-postgres}}";
+ $restoreCommand .= " && if [ \"\$({ gunzip -cf {$escapedTmpPath} 2>/dev/null || cat {$escapedTmpPath}; } | head -c 5)\" = 'PGDMP' ]; then pg_restore -U \${POSTGRES_USER} -d \${POSTGRES_DB:-\${POSTGRES_USER:-postgres}} {$escapedTmpPath}; else (gunzip -cf {$escapedTmpPath} 2>/dev/null || cat {$escapedTmpPath}) | psql -U \${POSTGRES_USER} -d \${POSTGRES_DB:-\${POSTGRES_USER:-postgres}}; fi";
} else {
$restoreCommand .= " {$escapedTmpPath}";
}
diff --git a/app/Support/DatabaseBackupFileValidator.php b/app/Support/DatabaseBackupFileValidator.php
index 2c1de948ba..84e629fe1a 100644
--- a/app/Support/DatabaseBackupFileValidator.php
+++ b/app/Support/DatabaseBackupFileValidator.php
@@ -90,8 +90,11 @@ class DatabaseBackupFileValidator
public static function containsPostgresqlProgramExecution(string $sql): bool
{
+ $requireStatementBoundary = true;
+
if (str_starts_with($sql, 'PGDMP')) {
- return false;
+ $sql = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F]+/', "\n", $sql) ?? $sql;
+ $requireStatementBoundary = false;
}
$withoutComments = self::stripSqlComments($sql);
@@ -100,7 +103,9 @@ class DatabaseBackupFileValidator
return true;
}
- return preg_match('/(?:^|;)\s*copy\b[^;]{0,2000}\b(?:from|to)\s+program\b/i', $withoutComments) === 1;
+ $copyPrefix = $requireStatementBoundary ? '(?:^|;)\s*' : '\b';
+
+ return preg_match('/'.$copyPrefix.'copy\b[^;]{0,2000}\b(?:from|to)\s+program\b/i', $withoutComments) === 1;
}
private static function extensionFor(string $name): ?string
diff --git a/tests/Feature/DatabaseBackupUploadValidationTest.php b/tests/Feature/DatabaseBackupUploadValidationTest.php
index 74835feefd..88978eeaba 100644
--- a/tests/Feature/DatabaseBackupUploadValidationTest.php
+++ b/tests/Feature/DatabaseBackupUploadValidationTest.php
@@ -18,10 +18,45 @@ function writeScanPayload(string $content, bool $gzip = false): string
/**
* Execute the real shell scanner snippet (as it runs inside the container)
* against a local payload file and return true when the restore is blocked.
+ *
+ * @param array<string, string> $env
*/
-function scannerBlocks(string $script): bool
+function scannerBlocks(string $script, array $env = []): bool
{
- return Process::run(['sh', '-c', $script])->exitCode() === 1;
+ $command = $script;
+
+ if ($env !== []) {
+ $exports = [];
+ foreach ($env as $name => $value) {
+ $exports[] = $name.'='.escapeshellarg($value);
+ }
+ $command = implode(' ', $exports).'; '.$script;
+ }
+
+ return Process::run(['sh', '-c', $command])->exitCode() === 1;
+}
+
+function fakePgRestorePath(string $sql, int $listExitCode = 0): string
+{
+ $dir = sys_get_temp_dir().'/coolify-fake-pg-'.uniqid();
+ mkdir($dir);
+
+ $sqlFile = $dir.'/archive.sql';
+ file_put_contents($sqlFile, $sql);
+
+ $escapedSqlFile = escapeshellarg($sqlFile);
+ file_put_contents($dir.'/pg_restore', <<<SH
+#!/bin/sh
+if [ "\$1" = '-l' ]; then exit {$listExitCode}; fi
+if [ "\$1" = '-f' ] && [ "\$2" = '-' ]; then
+ cat {$escapedSqlFile}
+ exit 0
+fi
+exit 1
+SH);
+ chmod($dir.'/pg_restore', 0755);
+
+ return $dir;
}
function invokeHasAllowedExtension(string $name): bool
@@ -185,15 +220,21 @@ test('file scanner allows ordinary gzipped dumps', function () {
expect(DatabaseBackupFileValidator::fileContainsPostgresqlProgramExecution($gzClean))->toBeFalse();
});
-test('file scanner allows PostgreSQL custom format archives', function () {
+test('file scanner detects program execution payloads inside custom format archives', function () {
$archive = writeScanPayload("PGDMP\0binary COPY records FROM PROGRAM payload");
- expect(DatabaseBackupFileValidator::fileContainsPostgresqlProgramExecution($archive))->toBeFalse();
+ expect(DatabaseBackupFileValidator::fileContainsPostgresqlProgramExecution($archive))->toBeTrue();
});
-test('file scanner allows gzipped PostgreSQL custom format archives', function () {
+test('file scanner detects program execution payloads inside gzipped custom format archives', function () {
$archive = writeScanPayload("PGDMP\0binary COPY records FROM PROGRAM payload", gzip: true);
+ expect(DatabaseBackupFileValidator::fileContainsPostgresqlProgramExecution($archive))->toBeTrue();
+});
+
+test('file scanner allows custom format archives without program execution', function () {
+ $archive = writeScanPayload("PGDMP\0binary archive without restore programs");
+
expect(DatabaseBackupFileValidator::fileContainsPostgresqlProgramExecution($archive))->toBeFalse();
});
@@ -226,6 +267,8 @@ test('remote postgresql scanner blocks bypass payloads', function (string $conte
'psql pipe redirect' => ["\\o | id\n", false],
'psql query pipe redirect' => ["\\g | id\n", false],
'gzipped comment bypass' => ["COPY x FROM/**/PROGRAM 'id';\n", true],
+ 'custom format archive' => ["PGDMP\0binary COPY records FROM PROGRAM payload", false],
+ 'custom format gzip archive' => ["PGDMP\0binary COPY records FROM PROGRAM payload", true],
]);
test('remote postgresql scanner allows legitimate restores', function (string $content, bool $gzip) {
@@ -241,11 +284,26 @@ test('remote postgresql scanner allows legitimate restores', function (string $c
'copy from stdin' => ["COPY users FROM stdin;\n1\tTaylor\n\\.\n", false],
'plain select' => ["SELECT * FROM users;\n", false],
'gzipped clean dump' => ["CREATE TABLE users (id int);\n", true],
- 'custom format archive' => ["PGDMP\0binary COPY records FROM PROGRAM payload", false],
- 'custom format gzip archive' => ["PGDMP\0binary COPY records FROM PROGRAM payload", true],
'copy words in table data' => ["COPY notes FROM stdin;\n1\tcopy files from program storage\n\\.\n", false],
]);
+test('remote postgresql scanner inspects custom archives instead of skipping them', function () {
+ $component = backupValidationImportFormWithResource(StandalonePostgresql::class);
+ $safeArchive = writeScanPayload("PGDMP\0binary archive");
+ $maliciousSql = "COPY x FROM PROGRAM 'id';\n";
+ $safeSql = "CREATE TABLE users (id integer);\nCOPY users FROM stdin;\n1\tTaylor\n\\.\n";
+
+ $maliciousPath = fakePgRestorePath($maliciousSql);
+ $safePath = fakePgRestorePath($safeSql);
+ $unreadablePath = fakePgRestorePath($safeSql, listExitCode: 1);
+ $path = getenv('PATH') ?: '/usr/bin:/bin';
+
+ expect(scannerBlocks($component->buildPostgresRestoreScanScript($safeArchive), ['PATH' => $maliciousPath.':'.$path]))->toBeTrue()
+ ->and(scannerBlocks($component->buildPostgresRestoreScanScript($safeArchive), ['PATH' => $safePath.':'.$path]))->toBeFalse()
+ ->and(scannerBlocks($component->buildPostgresRestoreScanScript($safeArchive), ['PATH' => $unreadablePath.':'.$path]))->toBeTrue()
+ ->and(scannerBlocks($component->buildPostgresRestoreScanScript($safeArchive)))->toBeTrue();
+});
+
test('MAX_BYTES constant is 10 GiB', function () {
$constant = (new ReflectionClass(UploadController::class))->getConstant('MAX_BYTES');
expect($constant)->toBe(10 * 1024 * 1024 * 1024);
diff --git a/tests/Unit/Livewire/Database/S3RestoreTest.php b/tests/Unit/Livewire/Database/S3RestoreTest.php
index 77695a8d76..e961f1317c 100644
--- a/tests/Unit/Livewire/Database/S3RestoreTest.php
+++ b/tests/Unit/Livewire/Database/S3RestoreTest.php
@@ -16,6 +16,76 @@ function importFormWithResource(string $modelClass): ImportForm
return $component;
}
+/**
+ * @return list<string>
+ */
+function invokedPostgresRestoreClients(string $contents, bool $gzip = false): array
+{
+ $binDir = sys_get_temp_dir().'/coolify-pg-restore-'.bin2hex(random_bytes(8));
+ mkdir($binDir);
+
+ try {
+ $logFile = $binDir.'/invoked.log';
+ file_put_contents($logFile, '');
+
+ foreach (['pg_restore', 'psql'] as $tool) {
+ $stub = <<<SH
+#!/bin/sh
+printf '%s\\n' '{$tool}' >> '{$logFile}'
+if [ ! -t 0 ]; then
+ cat >/dev/null
+fi
+exit 0
+SH;
+ $path = $binDir.'/'.$tool;
+ file_put_contents($path, $stub);
+ chmod($path, 0755);
+ }
+
+ $dumpPath = $binDir.'/backup.dump';
+ $payload = $gzip ? gzencode($contents) : $contents;
+ expect($payload)->not->toBeFalse();
+ file_put_contents($dumpPath, $payload);
+
+ $component = importFormWithResource('App\Models\StandalonePostgresql');
+ $component->dumpAll = true;
+ $component->postgresqlRestoreCommand = ':';
+
+ $process = proc_open(
+ ['sh', '-c', $component->buildRestoreCommand($dumpPath)],
+ [
+ 0 => ['pipe', 'r'],
+ 1 => ['pipe', 'w'],
+ 2 => ['pipe', 'w'],
+ ],
+ $pipes,
+ $binDir,
+ [
+ 'PATH' => $binDir.PATH_SEPARATOR.(getenv('PATH') ?: '/usr/bin:/bin'),
+ 'POSTGRES_USER' => 'postgres',
+ 'POSTGRES_DB' => 'app',
+ ]
+ );
+
+ expect($process)->not->toBeFalse();
+
+ fclose($pipes[0]);
+ stream_get_contents($pipes[1]);
+ stream_get_contents($pipes[2]);
+ fclose($pipes[1]);
+ fclose($pipes[2]);
+
+ expect(proc_close($process))->toBe(0);
+
+ return array_values(array_filter(explode("\n", trim((string) file_get_contents($logFile)))));
+ } finally {
+ foreach (glob($binDir.'/*') ?: [] as $file) {
+ unlink($file);
+ }
+ rmdir($binDir);
+ }
+}
+
test('buildRestoreCommand handles PostgreSQL without dumpAll', function () {
$component = importFormWithResource('App\Models\StandalonePostgresql');
$component->dumpAll = false;
@@ -38,6 +108,27 @@ test('buildRestoreCommand handles PostgreSQL with dumpAll', function () {
expect($result)->toContain('psql -U ${POSTGRES_USER} -d ${POSTGRES_DB:-${POSTGRES_USER:-postgres}}');
});
+test('buildRestoreCommand dump-all PostgreSQL restore is pg_restore for PGDMP otherwise psql', function () {
+ $component = importFormWithResource('App\Models\StandalonePostgresql');
+ $component->dumpAll = true;
+ $component->postgresqlRestoreCommand = 'psql -U ${POSTGRES_USER} -c "cleanup"';
+
+ $escapedTmpPath = escapeshellarg('/tmp/test.dump');
+
+ expect($component->buildRestoreCommand('/tmp/test.dump'))->toBe(
+ 'psql -U ${POSTGRES_USER} -c "cleanup" && if [ "$({ gunzip -cf '.$escapedTmpPath.' 2>/dev/null || cat '.$escapedTmpPath.'; } | head -c 5)" = \'PGDMP\' ]; then pg_restore -U ${POSTGRES_USER} -d ${POSTGRES_DB:-${POSTGRES_USER:-postgres}} '.$escapedTmpPath.'; else (gunzip -cf '.$escapedTmpPath.' 2>/dev/null || cat '.$escapedTmpPath.') | psql -U ${POSTGRES_USER} -d ${POSTGRES_DB:-${POSTGRES_USER:-postgres}}; fi'
+ );
+});
+
+test('dump-all PostgreSQL restore selects the client for the dump format', function (string $contents, bool $gzip, string $client) {
+ expect(invokedPostgresRestoreClients($contents, $gzip))->toBe([$client]);
+})->with([
+ 'custom archive' => ['PGDMP'.str_repeat("\0", 16), false, 'pg_restore'],
+ 'gzip custom archive' => ['PGDMP'.str_repeat("\0", 16), true, 'pg_restore'],
+ 'plain SQL' => ["-- PostgreSQL database dump\nSELECT 1;\n", false, 'psql'],
+ 'gzip SQL' => ["-- PostgreSQL database dump\nSELECT 1;\n", true, 'psql'],
+]);
+
test('buildRestoreCommand handles MySQL without dumpAll', function () {
$component = importFormWithResource('App\Models\StandaloneMysql');
$component->dumpAll = false;