yireo / magento2-csp-utilities
CSP utilities for various Magento modules
Magento 2 module CSP Utilities
This Magento 2 module offers CSP utilities for other modules, including various Yireo extensions.
Installation
Installation is usually done via other modules, but if you wanted to separately install this, here you go:
composer require yireo/magento2-csp-utilities
bin/magento module:enable Yireo_CspUtilities
Configuration
- Inline Script Mode
nonce: Generate CSP nonces for Yireo extensions. This is the default. On pages that are cached by the Full Page Cache, a nonce would be shared between visitors, so hashes are generated instead. With the built-in Full Page Cache and the moduleYireo_PageCacheCspNonceRegenerator, nonces are still used, because that module regenerates nonces for cached pages. With Varnish, hashes are always used for cacheable pages;hash: Generate CSP hashes for Yireo extensions. However, with this option, your HTTP headers could grow, which could lead into webserver limits;
Marking inline scripts
Only inline scripts that carry the marker of the current request are processed by ReplaceInlineScripts::replaceMarked(). The marker is a random token, generated per request, so that a script injected into the HTML (for instance, via a XSS vulnerability) never receives a valid nonce or hash. Once processed, the marker is removed again.
Add the marker to a script in your template via the ViewModel Yireo\CspUtilities\ViewModel\CspMarker:
<block name="example" template="Example_Module::example.phtml">
<arguments>
<argument name="csp_utilities" xsi:type="boolean">true</argument>
</arguments>
</block>
<?php /** @var \Yireo\CspUtilities\ViewModel\CspMarker $cspMarker */ ?>
<?php $cspMarker = $block->getData('csp_marker') ?>
<script data-csp-utilities="<?= $escaper->escapeHtmlAttr($cspMarker->getToken()) ?>">
console.log('Hello world');
</script>
The block argument csp_utilities enables processing of the marked scripts in the block output and makes the marker available as block data csp_marker. Alternatively, inject the CspMarker ViewModel as a block argument yourself and call ReplaceInlineScripts::replaceMarked() from your own code.
The method ReplaceInlineScripts::replace() is deprecated: It adds a nonce or hash to all inline scripts in the HTML, including scripts that were injected into it. Blocks that are added to the allowedBlocks argument of Yireo\CspUtilities\Plugin\AddCspInlineScripts via DI are still processed with replace(), for backwards compatibility. Use the block argument csp_utilities and the marker instead.
Only inline scripts are processed: Scripts with a src attribute, a nonce attribute or a non-JavaScript type are skipped, so that a nonce never bypasses the host whitelist of the CSP script-src directive.
Blocks of which the output is cached in the block_html cache (via cache_lifetime) are not supported, because the cached output would contain the marker of an earlier request.
Callers that already determined which scripts are trusted (for instance, by comparing them with the scripts in the template file) can pass these scripts to ReplaceInlineScripts::replaceScripts() directly, without a marker.
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]
[1.1.0] - 25 November 2026
Added
- Blocks can opt in via the
csp_utilitiesblock argument: only inline scripts carrying the per-request marker of the newCspMarkerViewModel then receive a nonce or hash (ReplaceInlineScripts::replaceMarked()) ReplaceInlineScripts::replaceScripts()to add nonces or hashes to a given set of already trusted scripts- In nonce mode, hashes are used on pages cached by the Full Page Cache, so nonces are no longer shared between visitors (unless
Yireo_PageCacheCspNonceRegeneratorregenerates them)
Deprecated
ReplaceInlineScripts::replace(), because it also trusts scripts injected into the HTML; blocks listed inallowedBlocksstill use it
Security
- Elements like
<script-data>were treated as scripts and turned into executable scripts with a valid nonce (YIREO-CSP-UTILITIES-2026-001) - External scripts with a
srcattribute received a nonce, bypassing the host whitelist ofscript-src
Fixed
- Script markup inside an inline script body (like
document.write) also received a nonce attribute, altering the JavaScript (#2) - Closing tags with whitespace, quoted
>characters in attributes andtype=modulescripts were not handled properly - Unknown inline script modes in the configuration now fall back to nonce mode
[1.0.2] - 06 November 2026
Fixed
- Allow manual blocks via DI type of
allowedBlocksarguments - Declare Magento_Config dependency for ACL and system configuration
- Verify CSP enforcement, fresh nonces and skipped non-JS scripts for Luma and Hyva
- Cover ReplaceInlineScripts, nonce generation and config edge cases
- Add Yireo_CspUtilities::config resource used by system configuration
- Match type and nonce as whole attributes, compare type case-insensitively
- Declare module dependencies and pass trim() characters explicitly
- Read inline script mode in store scope
- Strip script tags with attributes before calculating CSP hash
- Create FetchPolicy directly instead of runtime-generated FetchPolicyFactory
[1.0.1] - 10 March 2025
Fixed
- Potential regex fail could cause PHP notice
[1.0.0] - 23 August 2024
- Initial release
| Version | Stability | QA Status | Compatibility | Released |
|---|---|---|---|---|
| 1.1.0 | stable | Fail | Magento 2.4.7-2.4.9 Details | 2026-11-25 13:38:07 |
| 1.0.2 | stable | Fail | Magento 2.4.7-2.4.9 Details | 2026-11-06 13:40:00 |
| 1.0.1 | stable | Pass | Not yet tested Details | 2025-03-10 08:01:12 |
| 1.0.0 | stable | Not tested | Not yet tested Details | 2024-08-23 08:18:46 |
Requires 7
| Package | Constraint |
|---|---|
| ext-pcre | * |
| magento/framework | ^102.0 || ^103.0 |
| magento/module-backend | ^100.0 || ^101.0 || ^102.0 |
| magento/module-config | ^101.0 |
| magento/module-csp | ^100.0 |
| magento/module-store | ^100.0 || ^101.0 |
| php | >=8.2 <8.6 |
Requires-dev 3
| Package | Constraint |
|---|---|
| loki/magento2-functional-tests | @dev |
| phpunit/phpunit | ^9.5 || ^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.