Files
coolify/tests/Feature/SecureCookieAutoDetectionTest.php
Andras BacsaiandClaude Opus 5.5 0448c6ca51 refactor(http): remove the TrustHosts middleware that never checked the host
TrustHosts skipped its check while the cache key instance_settings_fqdn_host
was empty, and only the skipped check filled that key. So since e1fe58639
(October 2025) no release restricted the Host header. Turning it on now would
lock out users who reach Coolify by server IP or another name when the domain
or proxy is broken.

Links in emails and notifications do not use the Host header: base_url()
uses the configured FQDN or the server IP (see ResetPasswordUrlTest). So
remove the middleware, its cache key and its tests. TrustProxies is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 12:00:16 +02:00

68 lines
2.0 KiB
PHP

<?php
use App\Models\InstanceSettings;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
InstanceSettings::unguarded(fn () => InstanceSettings::updateOrCreate(['id' => 0], ['fqdn' => null]));
// Ensure session.secure starts unconfigured for each test
config(['session.secure' => null]);
});
it('sets session.secure to true when request arrives over HTTPS via proxy', function () {
$this->get('/login', [
'X-Forwarded-Proto' => 'https',
'X-Forwarded-For' => '1.2.3.4',
]);
expect(config('session.secure'))->toBeTrue();
});
it('does not set session.secure for plain HTTP requests', function () {
$this->get('/login');
expect(config('session.secure'))->toBeNull();
});
it('does not override explicit SESSION_SECURE_COOKIE=false for HTTPS requests', function () {
config(['session.secure' => false]);
$this->get('/login', [
'X-Forwarded-Proto' => 'https',
'X-Forwarded-For' => '1.2.3.4',
]);
// Explicit false must not be overridden — our check is `=== null` only
expect(config('session.secure'))->toBeFalse();
});
it('does not override explicit SESSION_SECURE_COOKIE=true', function () {
config(['session.secure' => true]);
$this->get('/login');
expect(config('session.secure'))->toBeTrue();
});
it('marks session cookie with Secure flag when accessed over HTTPS proxy', function () {
// With no users, /login redirects to first-user registration instead of rendering
User::factory()->create();
$response = $this->get('/login', [
'X-Forwarded-Proto' => 'https',
'X-Forwarded-For' => '1.2.3.4',
]);
$response->assertSuccessful();
$cookieName = config('session.cookie');
$sessionCookie = collect($response->headers->all('set-cookie'))
->first(fn ($c) => str_contains($c, $cookieName));
expect($sessionCookie)->not->toBeNull()
->and(strtolower($sessionCookie))->toContain('; secure');
});