From 03d0877fe54bcfb7560c6717a8c17b65f481beb3 Mon Sep 17 00:00:00 2001 From: Mark Scherer Date: Wed, 11 Nov 2026 12:56:52 +0100 Subject: [PATCH] Add CSP nonces to the admin layout The admin layout emitted the CDN Bootstrap script, the CDN stylesheet links and an inline style block without a nonce, so they were blocked under a nonce-based policy from CakePHP's CspMiddleware. Read cspScriptNonce (falling back to cspNonce) for the script and cspStyleNonce for the links and the style block. --- templates/layout/captcha-admin.php | 13 +++-- .../Admin/CaptchaControllerTest.php | 57 +++++++++++++++++++ 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/templates/layout/captcha-admin.php b/templates/layout/captcha-admin.php index cf2d6ec..703d9df 100644 --- a/templates/layout/captcha-admin.php +++ b/templates/layout/captcha-admin.php @@ -4,6 +4,11 @@ * * @var \App\View\AppView $this */ + +$cspScriptNonce = (string)($this->getRequest()->getAttribute('cspScriptNonce') ?? $this->getRequest()->getAttribute('cspNonce', '')); +$scriptNonceAttr = $cspScriptNonce !== '' ? ' nonce="' . h($cspScriptNonce) . '"' : ''; +$cspStyleNonce = (string)$this->getRequest()->getAttribute('cspStyleNonce', ''); +$styleNonceAttr = $cspStyleNonce !== '' ? ' nonce="' . h($cspStyleNonce) . '"' : ''; ?> @@ -11,17 +16,17 @@ <?= $this->fetch('title') ?: __d('captcha', 'Captcha Admin') ?> - rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous" referrerpolicy="no-referrer"> - rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.2/css/all.min.css" integrity="sha384-PPIZEGYM1v8zp5Py7UjFb79S58UeqCL9pYVnVPURKEqvioPROaVAJKKLzvH2rDnI" crossorigin="anonymous" referrerpolicy="no-referrer"> -