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(runners): create the runner secret file readable only by its owner

Andras Bacsai8c5fe1c85b9a
diff --git a/app/Services/GithubRunner/GithubRunnerContainer.php b/app/Services/GithubRunner/GithubRunnerContainer.php
index d7815f5769..c970490be7 100644
--- a/app/Services/GithubRunner/GithubRunnerContainer.php
+++ b/app/Services/GithubRunner/GithubRunnerContainer.php
@@ -111,8 +111,8 @@ class GithubRunnerContainer
$commands = [
'mkdir -p '.self::SECRETS_DIRECTORY,
- "echo {$environment} | base64 -d | tee {$envFile} > /dev/null",
- "chmod 600 {$envFile}",
+ // umask 077 creates the file as 0600; rm drops a world-readable file left by an interrupted start.
+ "sh -c 'umask 077 && rm -f {$envFile} && echo {$environment} | base64 -d > {$envFile}'",
];
$runnerFlags = "--name {$name} --network {$name} {$this->labelFlags()} {$this->limitFlags()} --env-file {$envFile}";
diff --git a/tests/Unit/GithubRunnerContainerTest.php b/tests/Unit/GithubRunnerContainerTest.php
index 48017decdf..c843007359 100644
--- a/tests/Unit/GithubRunnerContainerTest.php
+++ b/tests/Unit/GithubRunnerContainerTest.php
@@ -44,7 +44,7 @@ it('passes the JIT config through a root-only env file and never mounts the host
expect($all)->not->toContain('SECRET')
->toContain(base64_encode("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n"))
->not->toContain('docker.sock:/var/run/docker.sock')
- ->and($commands[2])->toBe('chmod 600 /data/coolify/github-runners/abc123.env')
+ ->and($commands[1])->toBe("sh -c 'umask 077 && rm -f /data/coolify/github-runners/abc123.env && echo ".base64_encode("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n")." | base64 -d > /data/coolify/github-runners/abc123.env'")
->and(collect($commands)->first(fn (string $command) => str_starts_with($command, 'docker run -d --name coolify-runner-abc123 ')))
->toStartWith('docker run -d --name coolify-runner-abc123 --network coolify-runner-abc123')
->toContain('--env-file /data/coolify/github-runners/abc123.env ')
@@ -113,8 +113,7 @@ it('keeps every command valid for servers with a non-root user', function () {
expect($parsed)
->toContain('sudo docker pull '."'".config('constants.github_runner.image')."'".' || sudo docker image inspect '."'".config('constants.github_runner.image')."'".' > /dev/null')
- ->toContain('echo '.base64_encode("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n").' | sudo base64 -d | sudo tee /data/coolify/github-runners/abc123.env > /dev/null')
- ->toContain('sudo chmod 600 /data/coolify/github-runners/abc123.env')
+ ->toContain("sudo sh -c 'umask 077 && rm -f /data/coolify/github-runners/abc123.env && echo ".base64_encode("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n")." | base64 -d > /data/coolify/github-runners/abc123.env'")
->toContain('echo '.base64_encode(GithubRunnerContainer::pullRequestHookScript()).' | sudo base64 -d | sudo tee /data/coolify/github-runners/abc123-job-started.sh > /dev/null')
->toContain('sudo chmod 644 /data/coolify/github-runners/abc123-job-started.sh')
->toContain('sudo docker rm -f -v coolify-runner-abc123 coolify-runner-abc123-dind > /dev/null 2>&1 || sudo true')
diff --git a/tests/Unit/GithubRunnerSecretFileModeTest.php b/tests/Unit/GithubRunnerSecretFileModeTest.php
new file mode 100644
index 0000000000..182251ba05
--- /dev/null
+++ b/tests/Unit/GithubRunnerSecretFileModeTest.php
@@ -0,0 +1,77 @@
+<?php
+
+use App\Models\GithubRunnerConfig;
+use App\Models\GithubRunnerExecution;
+use App\Models\Server;
+use App\Services\GithubRunner\GithubRunnerContainer;
+use Symfony\Component\Process\Process;
+use Tests\TestCase;
+
+uses(TestCase::class);
+
+beforeEach(function () {
+ $this->secretFileExecution = (new GithubRunnerExecution)->forceFill(['uuid' => 'abc123']);
+ $this->secretFileConfig = (new GithubRunnerConfig)->forceFill(['docker_mode' => 'none', 'allow_pull_requests' => true]);
+ $this->secretFileDirectory = sys_get_temp_dir().'/runner-secret-'.bin2hex(random_bytes(4));
+ mkdir($this->secretFileDirectory);
+});
+
+afterEach(function () {
+ array_map('unlink', glob($this->secretFileDirectory.'/*') ?: []);
+ rmdir($this->secretFileDirectory);
+ Mockery::close();
+});
+
+/**
+ * Runs the start commands up to the docker run line in a temp directory under umask 022 and
+ * returns the mode of the env file right after the command that wrote the secret into it.
+ */
+function runSecretFileCommandsAndReadMode(array $commands, string $directory): int
+{
+ $envFile = GithubRunnerContainer::SECRETS_DIRECTORY.'/abc123.env';
+ foreach ($commands as $command) {
+ if (str_starts_with($command, 'docker ')) {
+ break;
+ }
+ $process = Process::fromShellCommandline('umask 022; '.str_replace(GithubRunnerContainer::SECRETS_DIRECTORY, $directory, $command));
+ $process->mustRun();
+
+ clearstatcache();
+ $localEnvFile = str_replace(GithubRunnerContainer::SECRETS_DIRECTORY, $directory, $envFile);
+ if (file_exists($localEnvFile) && str_contains((string) file_get_contents($localEnvFile), 'SECRET')) {
+ return fileperms($localEnvFile) & 0777;
+ }
+ }
+
+ throw new RuntimeException('No command wrote the env file.');
+}
+
+it('creates the JIT secret file readable only by root as soon as the secret is written', function () {
+ $commands = (new GithubRunnerContainer($this->secretFileExecution, $this->secretFileConfig))->startCommands('SECRET');
+
+ expect(runSecretFileCommandsAndReadMode($commands, $this->secretFileDirectory))->toBe(0600)
+ ->and(file_get_contents($this->secretFileDirectory.'/abc123.env'))->toBe("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n");
+});
+
+it('replaces a world-readable secret file left by an interrupted start instead of writing into it', function () {
+ file_put_contents($this->secretFileDirectory.'/abc123.env', 'OLD');
+ chmod($this->secretFileDirectory.'/abc123.env', 0644);
+ $commands = (new GithubRunnerContainer($this->secretFileExecution, $this->secretFileConfig))->startCommands('SECRET');
+
+ expect(runSecretFileCommandsAndReadMode($commands, $this->secretFileDirectory))->toBe(0600)
+ ->and(file_get_contents($this->secretFileDirectory.'/abc123.env'))->toBe("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n");
+});
+
+it('writes the secret file as root in one shell script for a non-root SSH user', function () {
+ $server = Mockery::mock(Server::class)->makePartial();
+ $server->shouldReceive('getAttribute')->with('user')->andReturn('ubuntu');
+ $commands = (new GithubRunnerContainer($this->secretFileExecution, $this->secretFileConfig))->startCommands('SECRET');
+
+ $parsed = collect(parseCommandsByLineForSudo(collect($commands), $server));
+ $writeLine = $parsed->first(fn (string $line) => str_contains($line, base64_encode("ACTIONS_RUNNER_INPUT_JITCONFIG=SECRET\n")));
+
+ expect($writeLine)->toStartWith("sudo sh -c 'umask 077 && ")
+ ->and(isSingleSudoShellScript($writeLine))->toBeTrue()
+ ->and($parsed->implode("\n"))->not->toContain('SECRET')
+ ->not->toContain('tee /data/coolify/github-runners/abc123.env');
+});