Files
coolify/tests/Feature/SshHostKeyCheckingConfigTest.php
033f0c84da v4.7 (#12478)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com>
Co-authored-by: Aditya Tripathi <aditya@climactic.co>
2026-11-06 14:25:02 +01:00

70 lines
2.5 KiB
PHP

<?php
use App\Helpers\SshMultiplexingHelper;
use App\Models\Server;
use Illuminate\Support\Facades\Log;
/**
* Load config/constants.php with SSH_HOST_KEY_CHECKING set to the given value.
*/
function sshHostKeyCheckingFromEnv(?string $value): mixed
{
$original = [$_SERVER['SSH_HOST_KEY_CHECKING'] ?? null, $_ENV['SSH_HOST_KEY_CHECKING'] ?? null, getenv('SSH_HOST_KEY_CHECKING')];
try {
if ($value === null) {
unset($_SERVER['SSH_HOST_KEY_CHECKING'], $_ENV['SSH_HOST_KEY_CHECKING']);
putenv('SSH_HOST_KEY_CHECKING');
} else {
$_SERVER['SSH_HOST_KEY_CHECKING'] = $_ENV['SSH_HOST_KEY_CHECKING'] = $value;
putenv("SSH_HOST_KEY_CHECKING={$value}");
}
return (require config_path('constants.php'))['ssh']['host_key_checking'];
} finally {
[$server, $env, $putenv] = $original;
if ($server === null) {
unset($_SERVER['SSH_HOST_KEY_CHECKING']);
} else {
$_SERVER['SSH_HOST_KEY_CHECKING'] = $server;
}
if ($env === null) {
unset($_ENV['SSH_HOST_KEY_CHECKING']);
} else {
$_ENV['SSH_HOST_KEY_CHECKING'] = $env;
}
putenv($putenv === false ? 'SSH_HOST_KEY_CHECKING' : "SSH_HOST_KEY_CHECKING={$putenv}");
}
}
it('normalizes the SSH host key checking mode', function (?string $value, string $expected) {
expect(sshHostKeyCheckingFromEnv($value))->toBe($expected);
})->with([
'default' => [null, 'warn'],
'uppercase enforce' => ['ENFORCE', 'enforce'],
'mixed case enforce' => ['Enforce', 'enforce'],
'padded enforce' => [' enforce ', 'enforce'],
'padded off' => [' OFF ', 'off'],
'warn' => ['warn', 'warn'],
]);
it('logs one warning for an unknown SSH host key checking mode and uses warn', function () {
config(['constants.ssh.host_key_checking' => 'enforec']);
Log::spy();
$server = new Server;
expect(SshMultiplexingHelper::hostKeyCheckingMode($server))->toBe('warn')
->and(SshMultiplexingHelper::hostKeyCheckingMode($server))->toBe('warn');
Log::shouldHaveReceived('warning')->once()->withArgs(fn (string $message, array $context) => str_contains($message, 'SSH_HOST_KEY_CHECKING') && $context['value'] === 'enforec');
});
it('does not log a warning for a known SSH host key checking mode', function (string $mode) {
config(['constants.ssh.host_key_checking' => $mode]);
Log::spy();
SshMultiplexingHelper::hostKeyCheckingMode(new Server);
Log::shouldNotHaveReceived('warning');
})->with(['warn', 'enforce', 'off']);