Send a Content-Security-Policy and security headers on every response
- 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>
This commit is contained in:
1 parent
6912d045cf
commit
2a53675ce5
15 files changed
+290
-66
No files matched your search
@@ -18,6 +18,7 @@ namespace App\Test\TestCase;
|
||||
|
||||
use App\Application;
|
||||
use App\Middleware\HostHeaderMiddleware;
|
||||
use App\Middleware\SecurityHeadersMiddleware;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Error\Middleware\ErrorHandlerMiddleware;
|
||||
use Cake\Http\MiddlewareQueue;
|
||||
@@ -77,12 +78,15 @@ class ApplicationTest extends TestCase
|
||||
|
||||
$middleware = $app->middleware($middleware);
|
||||
|
||||
$this->assertInstanceOf(ErrorHandlerMiddleware::class, $middleware->current());
|
||||
// Outermost, so error pages get the security headers too.
|
||||
$this->assertInstanceOf(SecurityHeadersMiddleware::class, $middleware->current());
|
||||
$middleware->seek(1);
|
||||
$this->assertInstanceOf(HostHeaderMiddleware::class, $middleware->current());
|
||||
$this->assertInstanceOf(ErrorHandlerMiddleware::class, $middleware->current());
|
||||
$middleware->seek(2);
|
||||
$this->assertInstanceOf(AssetMiddleware::class, $middleware->current());
|
||||
$this->assertInstanceOf(HostHeaderMiddleware::class, $middleware->current());
|
||||
$middleware->seek(3);
|
||||
$this->assertInstanceOf(AssetMiddleware::class, $middleware->current());
|
||||
$middleware->seek(4);
|
||||
$this->assertInstanceOf(RoutingMiddleware::class, $middleware->current());
|
||||
}
|
||||
}
|
||||
@@ -86,7 +86,7 @@ class EmbedTest extends TestCase
|
||||
$this->assertResponseContains('Wie <geht> es?');
|
||||
$this->assertResponseContains('data-embed');
|
||||
$this->assertResponseNotContains($token);
|
||||
$this->assertHeader('Content-Security-Policy', 'frame-ancestors *');
|
||||
$this->assertHeaderContains('Content-Security-Policy', 'frame-ancestors *');
|
||||
$this->assertNull($this->_response->getHeaderLine('X-Frame-Options') ?: null);
|
||||
$this->assertHeader('Referrer-Policy', 'no-referrer');
|
||||
$this->assertNull($this->reload($poll)->current_slide_id, 'viewing alone does not go live');
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
<?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');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user