yireo / magento2-csp-whitelist-inline-js
Magento module to automatically add inline JS script to CSP whitelist
Yireo CspWhitelistInlineJs
When, in Magento 2, the CSP policy to disallow inline scripts is enabled, any script requires either a hash or a nonce (or something similar). This module scans any PHTML template for scripts on the fly and adds a nonce where needed, depending on your configuration.
Installation
composer require yireo/magento2-csp-whitelist-inline-js
bin/magento module:enable Yireo_CspWhitelistInlineJs
Configuration
- Mode (global scope only)
Custom whitelisting(recommended)Whitelist everything(dangerous, ignored in production mode unless explicitly allowed, see below)
- Logging: See below
Which scripts are whitelisted
Only static inline scripts that are literally present in the template file of the whitelisted block itself receive a nonce (or hash). This means that:
- Scripts rendered by child blocks are not whitelisted by whitelisting their parent. Whitelisting a container like
head.additionaltherefore does not whitelist anything that is added to it; - Scripts containing PHP output (
<?= ... ?>) in the template are never whitelisted, because dynamic values could be used to inject code; - Scripts with a
srcattribute, an existingnonceattribute or a non-JavaScripttypeare skipped (seeYireo_CspUtilities); - Blocks without a template file are skipped.
Scripts that need dynamic data should get CSP-support in their own template instead (for instance, by using the \Magento\Framework\View\Helper\SecureHtmlRenderer).
Mode: Custom whitelisting
A block can be whitelisted by this module, using one of the following techniques:
1. Custom whitelisting via DI (recommended)
Add the name of the block to the allowedBlocks argument of \Yireo\CspWhitelistInlineJs\Util\AllowBlock in the etc/frontend/di.xml file of your own module:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Yireo\CspWhitelistInlineJs\Util\AllowBlock">
<arguments>
<argument name="allowedBlocks" xsi:type="array">
<item name="example" xsi:type="string">example.block.name</item>
</argument>
</arguments>
</type>
</config>
Because this configuration lives in code, it can not be modified via the database.
2. Custom whitelisting by XML layout
You can also whitelist a specific block via the XML layout, by adding a boolean argument csp_whitelist:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:View/Layout:etc/page_configuration.xsd">
<body>
<referenceBlock name="example.block.name">
<arguments>
<argument name="csp_whitelist" xsi:type="boolean">true</argument>
</arguments>
</referenceBlock>
</body>
</page>
Only a real boolean true is accepted, so string values (as used in CMS widgets or {{block}} directives) do not work.
Note that there is still a security issue here, because the layout could be updated via the database as well (which could be hijacked via SQL injection). Prefer whitelisting via DI.
3. Via DI plugin
You could also create a DI plugin afterAllow on \Yireo\CspWhitelistInlineJs\Util\AllowBlock::allow(Template $block).
Mode: Whitelist everything
With this mode, the static inline scripts of any template are allowed. It defeats the purpose of CSP, in general, but gets the job done. Do make sure to log things, so that you can use a different mode after a while. If you keep using this mode in production, you don't get it.
In production mode, this mode is ignored (and Custom whitelisting is used instead), unless it is explicitly allowed in the app/etc/env.php file:
'csp_whitelist_inline_js' => [
'allow_all_in_production' => 1,
],
Important note on whitelisting everything & security
Note that automatically fixing inline scripts with this module does not take away the security risk that was meant to be fixed with CSP. The best solution is to apply this module to make sure CSP is not causing harm in production. And next, go through all of the inline scripts that don't have CSP-support yet and add CSP-support to it. And then remove this module again.
What about CMS content?
Whenever content is added to the HTML output of Magento, security checks would need to be in place. For instance, the content of CMS pages or CMS blocks might be compromised by hackers and with that, XSS attacks might be initiated by adding inline scripts to that content. By default, this module does not do anything specific for that CMS content. However, in a default situation, CMS pages and CMS blocks are not being outputted via templates and therefore an inline script in CMS content is not nonced by this module (which is a good thing). While the default is safe, any modification (custom theming, 3rd party modules, etc) could lead into compromised CMS content being outputted with nonced scripts anyway.
Full Page Cache
When CSP nonces are used (the default Inline Script Mode of Yireo_CspUtilities), these nonces become part of the cached HTML when the Full Page Cache is enabled. All visitors would then share the same nonce, which makes the nonce predictable. Either install the module Yireo_PageCacheCspNonceRegenerator (built-in Full Page Cache only) or switch the Inline Script Mode to hash (required for Varnish). A warning is shown in the Magento Admin Panel when this situation is detected.
Logging
This module also allows logging of these inline scripts. Just enable logging via Yireo > Yireo CspWhitelistInlineJs > Settings > Logging (csp_whitelist_inline_js/settings/logging). Next, monitor the logfile var/log/yireo-csp-whitelist-inline-js.log. The log contains a listing of all block templates (relative to the Magento root) of which inline scripts have been whitelisted by this module.
The recommendation is to go through this file and make sure that each template is whitelisted by the extension or theme that is offering that template, so that - one day in the future - you are able to remove this Yireo_CspWhitelistInlineJs module.
Note that this file is regenerated at the end of every request (in which new templates were found) by combining all existing lines with new lines.
Compatibility
- Luma theme
- Hyvä Themes
- Hyvä Checkout
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.0.0] - 26 November 2026
Added
- Admin warning when CSP nonces may be cached by the Full Page Cache
- DI argument
allowedBlocksofAllowBlockto whitelist specific blocks
Changed
- BREAKING: Only static inline scripts literally present in the template of a whitelisted block are whitelisted; child blocks and scripts containing PHP output are no longer whitelisted
- BREAKING: The block
head.additionalis no longer whitelisted by default - Whitelist mode
allis restricted to the global scope and ignored in production mode, unlesscsp_whitelist_inline_js/allow_all_in_productionis enabled in env.php - Require yireo/magento2-csp-utilities ^1.1.0
- Document template-only whitelisting, the DI allowlist and caching in the README
Fixed
- Script logger no longer breaks rendering, logs paths relative to the Magento root and locks the log file against concurrent requests
[0.1.2] - 06 November 2026
Fixed
- Add type hints to code
- Pass trim() characters explicitly in ScriptLogger
[0.1.1] - 2 October 2024
Fixed
- Prevent null HTML from causing errors
[0.1.0] - 4 September 2024
Added
- New default mode
customto only allow blocks that are explicitly whitelisted - Change logging so that entries are de-duplicated and sorted
[0.0.3] - 2 August 2024
Fixed
- Add a logger for analysing which scripts are fixed
[0.0.2] - 4 July 2024
Fixed
- Extend from SecureHtmlRenderer\Proxy to delay the instantiation @hostep
[0.0.1] - 28 June 2024
Added
- Initial release
| Version | Stability | QA Status | Compatibility | Released |
|---|---|---|---|---|
| 1.0.0 | stable | Fail | Magento 2.4.7-2.4.9 Details | 2026-11-26 01:39:54 |
| 0.1.2 | stable | Fail | Magento 2.4.7-2.4.9 Details | 2026-11-06 13:31:04 |
| 0.1.1 | stable | Pass | Not yet tested Details | 2024-10-02 17:45:52 |
| 0.1.0 | stable | Not tested | Not yet tested Details | 2024-09-04 12:44:16 |
| 0.0.3 | stable | Not tested | Not yet tested Details | 2024-08-02 13:33:53 |
| 0.0.2 | stable | Not tested | Not yet tested Details | 2024-07-04 14:13:36 |
| 0.0.1 | stable | Not tested | Not yet tested Details | 2024-06-28 10:04:18 |
Requires 6
| Package | Constraint |
|---|---|
| magento/framework | ^102.0 || ^103.0 |
| magento/module-backend | ^100.0 || ^101.0 || ^102.0 |
| magento/module-config | ^101.0 |
| magento/module-store | ^100.0 || ^101.0 |
| php | >=8.2 <8.6 |
| yireo/magento2-csp-utilities | ^1.1.0 |
Requires-dev 5
| Package | Constraint |
|---|---|
| ext-pcre | * |
| loki/magento2-functional-tests | @dev |
| magento/module-csp | ^100.4 |
| phpunit/phpunit | ^9.5 || ^10.5 |
| yireo/magento2-integration-test-helper | ~0.0 |
Suggests 1
| Package | Reason |
|---|---|
| yireo/magento2-page-cache-csp-nonce-regenerator | Regenerate CSP nonces per request when the built-in Full Page Cache is enabled |
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.