2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
2021-01-02 03:43:50 +01:00
|
|
|
|
2021-06-26 17:23:15 +02:00
|
|
|
namespace Tests;
|
2021-01-02 03:43:50 +01:00
|
|
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
class SecurityHeaderTest extends TestCase
|
|
|
|
{
|
|
|
|
public function test_cookies_samesite_lax_by_default()
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
foreach ($resp->headers->getCookies() as $cookie) {
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->assertEquals('lax', $cookie->getSameSite());
|
2021-01-02 03:43:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_cookies_samesite_none_when_iframe_hosts_set()
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->runWithEnv('ALLOWED_IFRAME_HOSTS', 'http://example.com', function () {
|
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
foreach ($resp->headers->getCookies() as $cookie) {
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->assertEquals('none', $cookie->getSameSite());
|
2021-01-02 03:43:50 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_secure_cookies_controlled_by_app_url()
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->runWithEnv('APP_URL', 'http://example.com', function () {
|
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
foreach ($resp->headers->getCookies() as $cookie) {
|
|
|
|
$this->assertFalse($cookie->isSecure());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->runWithEnv('APP_URL', 'https://example.com', function () {
|
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
foreach ($resp->headers->getCookies() as $cookie) {
|
|
|
|
$this->assertTrue($cookie->isSecure());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_iframe_csp_self_only_by_default()
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
$cspHeaders = collect($resp->headers->get('Content-Security-Policy'));
|
|
|
|
$frameHeaders = $cspHeaders->filter(function ($val) {
|
|
|
|
return Str::startsWith($val, 'frame-ancestors');
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($frameHeaders->count() === 1);
|
|
|
|
$this->assertEquals('frame-ancestors \'self\'', $frameHeaders->first());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function test_iframe_csp_includes_extra_hosts_if_configured()
|
|
|
|
{
|
2021-06-26 17:23:15 +02:00
|
|
|
$this->runWithEnv('ALLOWED_IFRAME_HOSTS', 'https://a.example.com https://b.example.com', function () {
|
|
|
|
$resp = $this->get('/');
|
2021-01-02 03:43:50 +01:00
|
|
|
$cspHeaders = collect($resp->headers->get('Content-Security-Policy'));
|
2021-06-26 17:23:15 +02:00
|
|
|
$frameHeaders = $cspHeaders->filter(function ($val) {
|
2021-01-02 03:43:50 +01:00
|
|
|
return Str::startsWith($val, 'frame-ancestors');
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertTrue($frameHeaders->count() === 1);
|
|
|
|
$this->assertEquals('frame-ancestors \'self\' https://a.example.com https://b.example.com', $frameHeaders->first());
|
|
|
|
});
|
|
|
|
}
|
2021-06-26 17:23:15 +02:00
|
|
|
}
|