yireo / magento2-page-cache-csp-nonce-regenerator

yireo/magento2-page-cache-csp-nonce-regenerator

Magento module to regenerate CSP nonces when built-in Page Cache is enabled

magento2-module Compatibility: 2.4.7-2.4.9 Code Quality: Fail Tests: Pass Security: Pass OSL-3.0

Yireo PageCacheCspNonceRegenerator

Magento version
PHP version
License
Latest Version

Magento 2 module that replaces the CSP nonces added to various script tags with a fresh instance, while the built-in Magento Page Cache is enabled. In other words, even with the Page Cache enabled, nonces are generated per request

Requirements

  • Magento_PageCache module enabled
  • Magento_Csp module enabled
  • Magento Page Cache configured to use the Built-In cache

^^Note that this module does not provide a solution for Varnish.

Installation

composer require yireo/magento2-page-cache-csp-nonce-regenerator
bin/magento module:enable Yireo_PageCacheCspNonceRegenerator

After installing or upgrading, flush the Full Page Cache (bin/magento cache:flush full_page). Pages that were cached before that don't contain the information this module needs, so their nonces are not regenerated.

How it works

When the built-in Page Cache stores a page, this module remembers the nonce that Magento generated for that page in an internal response header (X-Yireo-Csp-Cached-Nonce), which is saved with the cached page. When that cached page is served (a cache hit), only that exact nonce is replaced with a fresh one, both in the HTML and in the script-src directive of the CSP header. The internal header is always removed before the response is sent.

Any other nonce="..." attribute in the HTML (for instance markup injected through CMS content) is left alone, so that it doesn't get a valid nonce. Cache misses, uncacheable pages and non-HTML responses are not touched.

Security notes

  • With Varnish (or any other external full page cache), cached pages keep the same nonce for as long as they're cached, so an attacker who has seen one of those pages knows the nonce. This module doesn't fix that.
  • On a cache hit, Magento can't use page-specific CSP mode settings, so the default storefront CSP mode (report-only or restrict) is used.

Current status

Static Tests
Unit Tests
Integration Tests
Playwright
DI Compilation

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog,
and this project adheres to Semantic Versioning.

[Unreleased]

[0.0.3] - 25 November 2026

Fixed

  • Security: on built-in page cache hits, only the nonce Magento generated for the cached page is replaced, so nonce attributes in injected markup no longer receive a valid CSP nonce
  • Nonces are only regenerated for HTML responses served from the built-in page cache
  • The rewritten CSP header no longer ends up empty or with duplicate nonces, and a failure during regeneration leaves the response untouched
  • Add missing dependencies on Magento_Store and ext-pcre

Changed

  • Requires PHP 8.2 up to 8.5; PHP 8.1 is no longer supported

Added

  • README and SECURITY.md document the nonce regeneration behaviour and security notes

[0.0.2] - 4 September 2024

  • Code quality, readonly, SOLID #1 @mcspronko
  • Set minimum PHP requirement to 8.1

[0.0.1] - 29 August 2024

  • Initial release
Versions
Version Stability QA Status Compatibility Released
0.0.3 stable Pass Magento 2.4.7-2.4.9 Details 2026-11-25 17:11:08
0.0.2 stable Fail Not yet tested Details 2024-09-04 10:38:19
0.0.1 stable Not tested Not yet tested Details 2024-08-28 10:37:35

Requires 6

Package Constraint
ext-pcre *
magento/framework ^103.0
magento/module-csp ^100.0
magento/module-page-cache ^100.0
magento/module-store ^101.1
php >=8.2 <8.6

Requires-dev 2

Package Constraint
phpunit/phpunit ^10.5
yireo/magento2-integration-test-helper ~0.0

Compatibility

Each Magento release line is installed on its supported PHP versions, then the module is built (DI compilation + static-content deploy) and its unit and integration suites are run. The matrix shows the lines and PHP versions the module is confirmed to install and run on. Code-quality results further down (phpstan, phpcs, …) are reported separately and never affect compatibility.

Compatibility matrix (Magento × PHP)
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

Code Quality

Advisory checks against the module's source. Static analysis runs once across the whole module; PHPStan re-runs per Magento + PHP version because resolvable symbols differ between releases. These NEVER affect the Compatibility badge. A phpcs finding can't make a module incompatible.

Static analysis

Coding standards (phpcs), mess detection (phpmd), copy-pasted code (cpd), PHP cross-version compatibility, composer.json validity. Each runs once for the whole module.

Static analysis results
Tool Status Findings Summary
PHPCS Warning 3 3 warnings (ruleset: Magento2)
PHPMD Warning 1 1 rule violation (TooManyPublicMethods:1)
Cpd Pass 0
Composer validate Info 1 valid; 1 advisory note (composer validate --strict)

PHPStan

Type-checks the module's PHP against a real Magento install at the configured gate level. Re-runs per Magento and PHP version because resolvable symbols differ between releases.

PHPStan results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Error Error
2.4.8 Pass Pass
2.4.9 Pass Pass

Tests

Unit and integration suites, run for each applicable Magento and PHP version. A test failure speaks to the module's behaviour, not its compatibility with a Magento line, so it is reported here separately and never reddens the compatibility matrix.

Unit tests

Unit tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 N/A N/A
2.4.8 N/A N/A
2.4.9 N/A N/A

Integration tests

Integration tests results by Magento and PHP version
Magento PHP 8.2 PHP 8.3 PHP 8.4 PHP 8.5
2.4.7 Pass Pass
2.4.8 Pass Pass
2.4.9 Pass Pass

Security

Security checks run directly against the module: an audit of its declared dependencies for known vulnerabilities (composer audit) and a scan of its source for malware and web-shell signatures. Each runs once. A malware detection fails the version outright.

Security results
Tool Status Findings Summary
Composer audit Pass 0
Malware scan Pass 0
License
OSL-3.0

More from yireo

View vendor
Make it pay

Turn an existing module into recurring revenue.

If you already maintain a Magento 2 module on GitHub or GitLab, listing it on Packagento takes about five minutes. We mirror your tags, handle distribution signing, and route paid licenses through Stripe Connect, so you can keep shipping the way you already do.