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
@@ -6,11 +6,11 @@ namespace App\Controller;
|
||||
use App\Service\ThrottleService;
|
||||
use Cake\Controller\Controller;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\Http\Response;
|
||||
|
||||
/**
|
||||
* Base controller of PlenarIO.
|
||||
* Base controller of PlenarIO. Security headers (CSP, framing, nosniff) are set for every
|
||||
* response by \App\Middleware\SecurityHeadersMiddleware.
|
||||
*/
|
||||
class AppController extends Controller
|
||||
{
|
||||
@@ -24,22 +24,6 @@ class AppController extends Controller
|
||||
$this->loadComponent('Flash');
|
||||
}
|
||||
|
||||
/**
|
||||
* Security headers for every HTML response.
|
||||
*
|
||||
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
|
||||
* @return void
|
||||
*/
|
||||
public function beforeRender(EventInterface $event): void
|
||||
{
|
||||
parent::beforeRender($event);
|
||||
|
||||
$this->response = $this->response
|
||||
->withHeader('X-Content-Type-Options', 'nosniff')
|
||||
->withHeader('X-Frame-Options', 'SAMEORIGIN')
|
||||
->withHeader('Referrer-Policy', 'same-origin');
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a hit of the client IP in a throttle bucket (see ThrottleService).
|
||||
*
|
||||
|
||||
@@ -26,7 +26,7 @@ use Cake\Routing\Router;
|
||||
* heartbeats stop, PollsTable::pauseIfEmbedStale() pauses the poll.
|
||||
*
|
||||
* Runs in third-party iframes: no CSRF check (see Application::middleware()),
|
||||
* framing allowed via CSP frame-ancestors (Plenario.embed.frameAncestors).
|
||||
* framing allowed via CSP frame-ancestors (Plenario.embed.frameAncestors, see SecurityHeadersMiddleware).
|
||||
*/
|
||||
class EmbedController extends AppController
|
||||
{
|
||||
@@ -76,7 +76,7 @@ class EmbedController extends AppController
|
||||
|
||||
/**
|
||||
* Like admin pages: never leak the key as referrer, never index or cache.
|
||||
* Unlike everything else, framing by other sites is allowed.
|
||||
* Framing by other sites is allowed by SecurityHeadersMiddleware (embed paths).
|
||||
*
|
||||
* @param \Cake\Event\EventInterface<\Cake\Controller\Controller> $event Event
|
||||
* @return void
|
||||
@@ -84,9 +84,7 @@ class EmbedController extends AppController
|
||||
public function beforeRender(EventInterface $event): void
|
||||
{
|
||||
parent::beforeRender($event);
|
||||
$this->response = $this->protect($this->response)
|
||||
->withoutHeader('X-Frame-Options')
|
||||
->withHeader('Content-Security-Policy', 'frame-ancestors ' . $this->frameAncestors());
|
||||
$this->response = $this->protect($this->response);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -210,14 +208,4 @@ class EmbedController extends AppController
|
||||
{
|
||||
return ['_name' => $name, 'key' => (string)$this->poll->embed_key, 'id' => $this->slide->id];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string CSP source list; falls back to 'self' for anything unexpected
|
||||
*/
|
||||
protected function frameAncestors(): string
|
||||
{
|
||||
$value = trim((string)Configure::read('Plenario.embed.frameAncestors', '*'));
|
||||
|
||||
return preg_match('#^[A-Za-z0-9*:/.\' -]+$#', $value) ? $value : "'self'";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user