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
+5
-4
@@ -17,6 +17,7 @@ declare(strict_types=1);
|
||||
namespace App;
|
||||
|
||||
use App\Middleware\HostHeaderMiddleware;
|
||||
use App\Middleware\SecurityHeadersMiddleware;
|
||||
use Cake\Core\Configure;
|
||||
use Cake\Core\ContainerInterface;
|
||||
use Cake\Datasource\FactoryLocator;
|
||||
@@ -67,6 +68,9 @@ class Application extends BaseApplication
|
||||
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
|
||||
{
|
||||
$middlewareQueue
|
||||
// Outermost, so error pages get the security headers (CSP, framing, nosniff) too.
|
||||
->add(new SecurityHeadersMiddleware())
|
||||
|
||||
// Catch any exceptions in the lower layers,
|
||||
// and make an error page/response
|
||||
->add(new ErrorHandlerMiddleware(Configure::read('Error'), $this))
|
||||
@@ -98,10 +102,7 @@ class Application extends BaseApplication
|
||||
'httponly' => true,
|
||||
]))->skipCheckCallback(
|
||||
// Embedded slides run in third-party iframes (no cookies); the secret key is the authorisation.
|
||||
fn(ServerRequestInterface $request): bool => (bool)preg_match(
|
||||
'/^embed(:|$)/',
|
||||
(string)($request->getAttribute('params')['_name'] ?? ''),
|
||||
),
|
||||
fn(ServerRequestInterface $request): bool => SecurityHeadersMiddleware::isEmbed($request),
|
||||
));
|
||||
|
||||
return $middlewareQueue;
|
||||
|
||||
Reference in new issue
Block a user