mirror of
https://github.com/coollabsio/coolify.git
synced 2026-10-07 14:36:44 -04:00
Caddy 2.7.6, shipped in the caddy-docker-proxy 2.8 image, rejects the whole Caddyfile when it contains log_append. Add Server::caddySupportsLogAppend(), which reads the image from the applied proxy configuration. Traffic analytics labels now add log_append only when the server runs 2.9 or newer and has no pending proxy change. - Change the default Caddy proxy image from 2.8-alpine to 2.13-alpine - ProxyPortParser now validates Docker Compose port ranges and random host ports. It returns only fixed host ports for the availability check and has a clearer validation message - After mkdir, chown only root-owned files and remove other-user access from the top directory only. Files owned by container users and the modes of mounted files no longer change - Add tests for log_append support and the new parser/sudo behaviour - Note in the lessons file that tests must flush the Server identity map between dataset cases
171 lines
5.8 KiB
PHP
171 lines
5.8 KiB
PHP
<?php
|
|
|
|
use App\Models\Server;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Str;
|
|
|
|
function shouldChangeOwnership(string $path): bool
|
|
{
|
|
$path = trim($path);
|
|
|
|
$systemPaths = ['/var', '/etc', '/usr', '/opt', '/sys', '/proc', '/dev', '/bin', '/sbin', '/lib', '/lib64', '/boot', '/root', '/home', '/media', '/mnt', '/srv', '/run'];
|
|
|
|
foreach ($systemPaths as $systemPath) {
|
|
if ($path === $systemPath || Str::startsWith($path, $systemPath.'/')) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
$isCoolifyPath = Str::startsWith($path, '/data/coolify') || Str::startsWith($path, '/tmp/coolify');
|
|
|
|
return $isCoolifyPath;
|
|
}
|
|
/**
|
|
* Gives the SSH user the Coolify-created (root-owned) files in a directory and closes the
|
|
* directory to other users. Files that belong to container users (such as a database data
|
|
* folder) and the modes of mounted files stay as they are, so containers can still read them.
|
|
*/
|
|
function ownershipCommand(string $path, Server $server): string
|
|
{
|
|
return "find $path -user root -exec chown $server->user:$server->user {} + && chmod o-rwx $path";
|
|
}
|
|
|
|
function parseCommandsByLineForSudo(Collection $commands, Server $server): array
|
|
{
|
|
$commands = $commands->map(function ($line) {
|
|
$trimmedLine = trim($line);
|
|
|
|
// All bash keywords that should not receive sudo prefix
|
|
// Using word boundary matching to avoid prefix collisions (e.g., 'do' vs 'docker', 'if' vs 'ifconfig', 'fi' vs 'find')
|
|
$bashKeywords = [
|
|
'cd',
|
|
'command',
|
|
'declare',
|
|
'echo',
|
|
'export',
|
|
'local',
|
|
'readonly',
|
|
'return',
|
|
'true',
|
|
'if',
|
|
'fi',
|
|
'for',
|
|
'done',
|
|
'while',
|
|
'until',
|
|
'case',
|
|
'esac',
|
|
'select',
|
|
'then',
|
|
'else',
|
|
'elif',
|
|
'break',
|
|
'continue',
|
|
'do',
|
|
];
|
|
|
|
// Special case: comments (no collision risk with '#')
|
|
if (str_starts_with($trimmedLine, '#')) {
|
|
return $line;
|
|
}
|
|
|
|
// Negation belongs to the shell, before the elevated command.
|
|
if (preg_match('/^\s*!\s+/', $line)) {
|
|
return preg_replace('/^(\s*(?:!\s+)+)/', '$1sudo ', $line);
|
|
}
|
|
|
|
// Check all keywords with word boundary matching
|
|
// Match keyword followed by space, semicolon, or end of line
|
|
foreach ($bashKeywords as $keyword) {
|
|
if (preg_match('/^'.preg_quote($keyword, '/').'(\s|;|$)/', $trimmedLine)) {
|
|
// Keep any shell negation before sudo in the condition.
|
|
if ($keyword === 'if') {
|
|
return preg_replace('/^(\s*if\s+(?:!\s+)*)/', '$1sudo ', $line);
|
|
}
|
|
|
|
return $line;
|
|
}
|
|
}
|
|
|
|
return "sudo $line";
|
|
});
|
|
|
|
$commands = $commands->map(function ($line) use ($server) {
|
|
if (Str::startsWith($line, 'sudo mkdir -p')) {
|
|
$path = trim(Str::after($line, 'sudo mkdir -p'));
|
|
if (shouldChangeOwnership($path)) {
|
|
// No sudo here: the && rule below adds it. `sudo sudo` fails where root is not in sudoers (Alpine).
|
|
return "$line && ".ownershipCommand($path, $server);
|
|
}
|
|
|
|
return $line;
|
|
}
|
|
|
|
return $line;
|
|
});
|
|
|
|
$commands = $commands->map(function ($line) {
|
|
$line = str($line);
|
|
|
|
// Detect complex piped commands that should be wrapped in bash -c
|
|
$isComplexPipeCommand = (
|
|
$line->contains(' | sh') ||
|
|
$line->contains(' | bash') ||
|
|
$line->contains(' sh -c ') ||
|
|
($line->contains(' | ') && ($line->contains('||') || $line->contains('&&')))
|
|
);
|
|
|
|
// If it's a complex pipe command and starts with sudo, wrap it in bash -c
|
|
if ($isComplexPipeCommand && $line->startsWith('sudo ')) {
|
|
$commandWithoutSudo = $line->after('sudo ')->value();
|
|
// Escape single quotes for bash -c by replacing ' with '\''
|
|
$escapedCommand = str_replace("'", "'\\''", $commandWithoutSudo);
|
|
|
|
return "sudo bash -c '$escapedCommand'";
|
|
}
|
|
|
|
// For non-complex commands, apply the original logic
|
|
if (str($line)->contains('$(')) {
|
|
$line = $line->replace('$(', '$(sudo ');
|
|
}
|
|
if (! $isComplexPipeCommand && str($line)->contains('||')) {
|
|
$line = $line->replace('||', '|| sudo');
|
|
}
|
|
if (! $isComplexPipeCommand && str($line)->contains('&&')) {
|
|
$line = $line->replace('&&', '&& sudo');
|
|
}
|
|
// Don't insert sudo into pipes for complex commands
|
|
if (! $isComplexPipeCommand && str($line)->contains(' | ')) {
|
|
$line = $line->replace(' | ', ' | sudo ');
|
|
}
|
|
|
|
return $line->value();
|
|
});
|
|
|
|
return $commands->toArray();
|
|
}
|
|
function parseLineForSudo(string $command, Server $server): string
|
|
{
|
|
if (! str($command)->startSwith('cd') && ! str($command)->startSwith('command')) {
|
|
$command = "sudo $command";
|
|
}
|
|
if (Str::startsWith($command, 'sudo mkdir -p')) {
|
|
$path = trim(Str::after($command, 'sudo mkdir -p'));
|
|
if (shouldChangeOwnership($path)) {
|
|
// No sudo here: the && rule below adds it.
|
|
$command = "$command && ".ownershipCommand($path, $server);
|
|
}
|
|
}
|
|
if (str($command)->contains('$(') || str($command)->contains('`')) {
|
|
$command = str($command)->replace('$(', '$(sudo ')->replace('`', '`sudo ')->value();
|
|
}
|
|
if (str($command)->contains('||')) {
|
|
$command = str($command)->replace('||', '|| sudo ')->value();
|
|
}
|
|
if (str($command)->contains('&&')) {
|
|
$command = str($command)->replace('&&', '&& sudo ')->value();
|
|
}
|
|
|
|
return $command;
|
|
}
|