- New SecurityHeadersMiddleware (outermost): CSP with a per-request script nonce, framing (SAMEORIGIN, embeds via frame-ancestors), nosniff, Referrer- and Permissions-Policy. JSON, files and error pages are covered now, not only rendered HTML; headers set by controllers still win. - The inline theme script carries the nonce; flash messages are dismissed via data-dismiss instead of inline onclick handlers, and the flash elements share one template. - reCAPTCHA sources are only allowed when it is enabled. - The embed CSRF exception and framing use the same isEmbed() check. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.8 KiB
PHP
101 lines
3.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Test\TestCase\Middleware;
|
|
|
|
use App\Middleware\SecurityHeadersMiddleware;
|
|
use App\Model\Entity\Slide;
|
|
use App\Test\TestCase\PlenarioTestTrait;
|
|
use Cake\Core\Configure;
|
|
use Cake\TestSuite\IntegrationTestTrait;
|
|
use Cake\TestSuite\TestCase;
|
|
|
|
/**
|
|
* CSP, framing and other security headers on every kind of response.
|
|
*/
|
|
class SecurityHeadersMiddlewareTest extends TestCase
|
|
{
|
|
use IntegrationTestTrait;
|
|
use PlenarioTestTrait;
|
|
|
|
/**
|
|
* @var list<string>
|
|
*/
|
|
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
|
|
|
|
public function testHtmlPageHasCspWithNonceForTheInlineScript(): void
|
|
{
|
|
$this->get('/');
|
|
$this->assertResponseOk();
|
|
|
|
$csp = $this->_response->getHeaderLine('Content-Security-Policy');
|
|
$this->assertMatchesRegularExpression("#script-src 'self' 'nonce-([A-Za-z0-9+/=]+)'#", $csp);
|
|
preg_match("#'nonce-([A-Za-z0-9+/=]+)'#", $csp, $m);
|
|
$this->assertResponseContains('<script nonce="' . $m[1] . '">', 'theme_init carries the nonce');
|
|
$this->assertStringContainsString("frame-ancestors 'self'", $csp);
|
|
$this->assertStringContainsString("object-src 'none'", $csp);
|
|
$this->assertStringNotContainsString('google.com', $csp, 'reCAPTCHA only when enabled');
|
|
$this->assertHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
$this->assertHeader('X-Content-Type-Options', 'nosniff');
|
|
$this->assertHeader('Referrer-Policy', 'same-origin');
|
|
$this->assertResponseNotContains('onclick=');
|
|
}
|
|
|
|
public function testNonceChangesPerRequest(): void
|
|
{
|
|
$this->get('/');
|
|
$first = $this->_response->getHeaderLine('Content-Security-Policy');
|
|
$this->get('/');
|
|
$this->assertNotSame($first, $this->_response->getHeaderLine('Content-Security-Policy'));
|
|
}
|
|
|
|
public function testJsonAndErrorResponsesAreCoveredToo(): void
|
|
{
|
|
[$poll] = $this->createPoll();
|
|
$this->get("/p/{$poll->join_code}/state");
|
|
$this->assertResponseOk();
|
|
$this->assertHeader('X-Content-Type-Options', 'nosniff');
|
|
$this->assertHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
|
|
$this->get('/p/ZZZZZZ');
|
|
$this->assertResponseCode(404);
|
|
$this->assertHeader('X-Content-Type-Options', 'nosniff');
|
|
$this->assertHeaderContains('Content-Security-Policy', "default-src 'self'");
|
|
}
|
|
|
|
public function testControllerHeadersWin(): void
|
|
{
|
|
[, $token] = $this->createPoll();
|
|
$this->get("/admin/$token");
|
|
$this->assertHeader('Referrer-Policy', 'no-referrer');
|
|
$this->assertHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
}
|
|
|
|
public function testEmbedMayBeFramedBySites(): void
|
|
{
|
|
Configure::write('Plenario.embed.frameAncestors', 'https://*.canva.com');
|
|
[$poll] = $this->createPoll();
|
|
$slide = $this->createSlide($poll, Slide::TYPE_CHOICE);
|
|
$key = $this->pollsTable()->ensureEmbedKey($poll);
|
|
|
|
$this->get("/e/$key/{$slide->id}");
|
|
$this->assertResponseOk();
|
|
$this->assertHeaderContains('Content-Security-Policy', 'frame-ancestors https://*.canva.com');
|
|
$this->assertSame('', $this->_response->getHeaderLine('X-Frame-Options'));
|
|
}
|
|
|
|
public function testInvalidFrameAncestorsFallBackToSelf(): void
|
|
{
|
|
Configure::write('Plenario.embed.frameAncestors', 'https://x.org; script-src *');
|
|
$this->assertSame("'self'", SecurityHeadersMiddleware::embedFrameAncestors());
|
|
}
|
|
|
|
public function testRecaptchaSourcesOnlyWhenEnabled(): void
|
|
{
|
|
Configure::write('Plenario.captcha.type', 'recaptcha');
|
|
$this->get('/');
|
|
$this->assertHeaderContains('Content-Security-Policy', 'https://www.gstatic.com/recaptcha/');
|
|
$this->assertHeaderContains('Content-Security-Policy', 'frame-src');
|
|
}
|
|
}
|