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 compose .env files atomically via temp rename

Andras Bacsai0134401f7cfc
diff --git a/app/Models/Service.php b/app/Models/Service.php
index 3b80a19412..e963571cb4 100644
--- a/app/Models/Service.php
+++ b/app/Models/Service.php
@@ -1587,7 +1587,7 @@ class Service extends BaseModel
Storage::disk('local')->delete("tmp/{$filename}");
$commands[] = "cd $workdir";
- $commands[] = 'rm -f .env || true';
+ $environmentFilename = new_public_id().'.env.tmp';
$envs = collect([]);
@@ -1618,10 +1618,10 @@ class Service extends BaseModel
$envs->push("{$env->key}={$env->real_value}");
}
if ($envs->count() === 0) {
- $commands[] = 'touch .env';
+ $commands[] = "touch {$environmentFilename} && mv {$environmentFilename} .env";
} else {
$envs_base64 = base64_encode($envs->implode("\n"));
- $commands[] = "echo '$envs_base64' | base64 -d | tee .env > /dev/null";
+ $commands[] = "echo '$envs_base64' | base64 -d | tee {$environmentFilename} > /dev/null && mv {$environmentFilename} .env";
}
instant_remote_process($commands, $this->server);
diff --git a/tests/Unit/ServiceComposeConfigAtomicWriteTest.php b/tests/Unit/ServiceComposeConfigAtomicWriteTest.php
new file mode 100644
index 0000000000..ebe5d7b719
--- /dev/null
+++ b/tests/Unit/ServiceComposeConfigAtomicWriteTest.php
@@ -0,0 +1,14 @@
+<?php
+
+it('publishes the service environment file atomically', function () {
+ $source = file_get_contents(__DIR__.'/../../app/Models/Service.php');
+ $methodStart = strpos($source, 'public function saveComposeConfigs()');
+ $methodEnd = strpos($source, 'public function parse(', $methodStart);
+ $method = substr($source, $methodStart, $methodEnd - $methodStart);
+
+ expect($method)
+ ->not->toContain("'rm -f .env || true'")
+ ->toContain("new_public_id().'.env.tmp'")
+ ->toContain('tee {$environmentFilename} > /dev/null && mv {$environmentFilename} .env')
+ ->toContain('touch {$environmentFilename} && mv {$environmentFilename} .env');
+});