How to Fix Content Security Policy (CSP) Issues on Magento
Content Security Policy (CSP) is a web security feature that helps detect and mitigate certain types of attacks, including Cross Site Scripting (XSS) and data injection, which can be used for everything from data theft to malware attacks. CSP is supported in Magento 2.3.5 and later versions.
CSP Configuration in Magento 2
When you upgrade to Magento 2.3.5, you may run into resource loading problems caused by CSP. To fix this, you need to configure CSP correctly. Here are the steps:
Step 1: Create a Custom Module
First of all, create a custom module to configure CSP. Let’s say we create a module called Custom_Csp.
1 – Create the registration.php file in the path app/code/Custom/Csp/
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Dolphin_Cspallow',
__DIR__
);2 – Create the module.xml file in the path app/code/Custom/Csp/etc/
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Dolphin_Cspallow" setup_version="1.0.0" />
</config>3 – Create the csp_whitelist.xml file in the path app/code/Custom/Csp/etc/
<?xml version="1.0"?>
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp/etc/csp_whitelist.xsd">
<policies>
<policy id="script-src">
<values>
<value id="cloudflare" type="host">*.cloudflare.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
<value id="google-analytics" type="host">*.google-analytics.com</value>
<value id="twimg" type="host">*.twimg.com</value>
<value id="gstatic" type="host">*.gstatic.com</value>
<value id="trustedshops" type="host">*.trustedshops.com</value>
<value id="usercentrics" type="host">*.usercentrics.eu</value>
<value id="fontawesome" type="host">*.fontawesome.com</value>
<value id="bing" type="host">*.bing.com</value>
<value id="zopim" type="host">*.zopim.com</value>
<value id="zdassets" type="host">*.zdassets.com</value>
<value id="google" type="host">*.google.com</value>
</values>
</policy>
<policy id="style-src">
<values>
<value id="cloudflare" type="host">*.cloudflare.com</value>
<value id="googleapis" type="host">*.googleapis.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
<value id="twimg" type="host">*.twimg.com</value>
<value id="gstatic" type="host">*.gstatic.com</value>
<value id="typekit" type="host">*.typekit.net</value>
<value id="trustedshops" type="host">*.trustedshops.com</value>
<value id="fontawesome" type="host">*.fontawesome.com</value>
<value id="bing" type="host">*.bing.com</value>
</values>
</policy>
<policy id="img-src">
<values>
<value id="cloudflare" type="host">*.cloudflare.com</value>
<value id="klarna-base" type="host">*.klarna.com</value>
<value id="googleadservices" type="host">*.googleadservices.com</value>
<value id="google-analytics" type="host">*.google-analytics.com</value>
<value id="paypal" type="host">*.paypal.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
<value id="twimg" type="host">*.twimg.com</value>
<value id="vimeocdn" type="host">*.vimeocdn.com</value>
<value id="youtube-img" type="host">*.ytimg.com</value>
<value id="data" type="host">'self' data:</value>
<value id="fontawesome" type="host">*.bing.com</value>
<value id="zopim" type="host">*.zopim.com</value>
<value id="zopimio" type="host">*.zopim.io</value>
<value id="doubleclick" type="host">*.doubleclick.net</value>
<value id="google" type="host">*.google.com</value>
<value id="googlein" type="host">*.google.co.in</value>
<value id="mastercard" type="host">*.mastercard.com</value>
</values>
</policy>
<policy id="connect-src">
<values>
<value id="google-analytics" type="host">www.google-analytics.com</value>
<value id="cloudflare" type="host">*.cloudflare.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
<value id="paypal" type="host">*.paypal.com</value>
<value id="twimg" type="host">*.twimg.com</value>
<value id="zdassets" type="host">*.zdassets.com</value>
<value id="zopim" type="host">*.zopim.com</value>
<value id="zopimio" type="host">*.zopim.io</value>
<value id="mediator" type="host">wss://widget-mediator.zopim.com</value>
<value id="googleanalytics" type="host">*.google-analytics.com</value>
<value id="doubleclick" type="host">https://stats.g.doubleclick.net</value>
</values>
</policy>
<policy id="font-src">
<values>
<value id="cloudflare" type="host">*.cloudflare.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
<value id="gstatic" type="host">*.gstatic.com</value>
<value id="typekit" type="host">*.typekit.net</value>
<value id="twimg" type="host">*.twimg.com</value>
<value id="trustedshops" type="host">*.trustedshops.com</value>
<value id="googleapis" type="host">*.googleapis.com</value>
<value id="zopim" type="host">*.zopim.com</value>
<value id="zopimio" type="host">*.zopim.io</value>
</values>
</policy>
<policy id="frame-src">
<values>
<value id="youtube.com" type="host">https://www.youtube.com</value>
<value id="sandbox.paypal.com" type="host">http://www.sandbox.paypal.com</value>
<value id="paypal.com" type="host">www.paypal.com</value>
<value id="twitter.com" type="host">*.twitter.com</value>
</values>
</policy>
<policy id="media-src">
<values>
<value id="zopim" type="host">*.zopim.com</value>
<value id="zopimio" type="host">*.zopim.io</value>
</values>
</policy>
<policy id="form-action">
<values>
<value id="twitter.com" type="host">*.twitter.com</value>
</values>
</policy>
</policies>
</csp_whitelist>Step 2: Enable and Configure the Module
- Enable the module by running the following commands:shCopy code
bin/magento module:enable Custom_Csp bin/magento setup:upgrade bin/magento cache:clean bin/magento cache:flush - Check the Browser ConsoleOpen your website and check that there are no CSP errors in the browser console. If there are, update
csp_whitelist.xmlto allow the blocked resources.
Step 3: Future Updates
With CSP enabled and configured, make sure you keep the csp_whitelist.xml file up to date as you add new resources to your site.