yireo / magento2-page-cache-csp-nonce-regenerator
Magento module to regenerate CSP nonces when built-in Page Cache is enabled
Yireo PageCacheCspNonceRegenerator
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_PageCachemodule enabledMagento_Cspmodule 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
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
nonceattributes 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_Storeandext-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
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.
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.
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.
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
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.
More from yireo
View vendorReplace bundled third party packages from Magento
N/A
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.