Search by

yireo / magento2-csp-utilities

yireo

CSP utilities for various Magento modules

Package info

github.com/yireo/Yireo_CspUtilities

Type:magento2-module

pkg:composer/yireo/magento2-csp-utilities

Fund package maintenance!

yireo

www.paypal.me/yireo

Statistics

Installs: 992 153

Dependents: 8

Suggesters: 0

Stars: 3

Open Issues: 0

1.2.0 2026-12-02 19:10 UTC

This package is auto-updated.

Last update: 2026-12-02 19:23:06 UTC


README

Magento version PHP version License Latest Version

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 module Yireo_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;

With the built-in Full Page Cache, the generated hashes are stored together with the cached page (in the internal header X-Yireo-Csp-Hashes, which is never sent to the browser) and added to the CSP policy again when the cached page is served. Without this, Magento_Csp would only render the policies collected during the current request, and the inline scripts of a cached page would be blocked.

The hashes are added to the CSP policy via a dedicated policy collector, not via the DynamicCollector of Magento_Csp. This matters because Magento\Csp\Model\BlockCache stores all policies of the DynamicCollector with every block_html cache entry, so hashes of other pages would pile up in the CSP header. Instead, every block_html cache entry only stores the hashes of the scripts that were rendered inside that block (as an HTML comment in front of the cached HTML), and these are added to the CSP policy again when the block is loaded from cache. Hashes that are added by Magento itself (for instance, via SecureHtmlRenderer) are still handled by Magento_Csp.

After upgrading from a version that stored hashes via the DynamicCollector, clean the block_html cache once (bin/magento cache:clean block_html), so that hashes of other pages are removed from existing cache entries.

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.

Flagged blocks can be cached in the block_html cache (via cache_lifetime): Before the output is saved in the cache, the marker of the current request is replaced by a signature of the script (a HMAC with the Magento crypt key). When the block is loaded from the cache, scripts with a valid signature receive a fresh nonce or hash, while all other markers are stripped. Nonces are never stored in the cache. After changing the crypt key, existing signatures are no longer valid, so flush the block_html cache as well. Blocks that call ReplaceInlineScripts::replaceMarked() themselves can use ReplaceInlineScripts::replaceMarkedOrSigned() instead, to support the block_html cache.

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

Static Tests Unit Tests Integration Tests Playwright DI Compilation