Content Security Policies (CSP) have been added to Magento 2, starting with Magento 2.3.5.

CSP’s are a powerful tool that helps to mitigate Cross Site Scripting (XSS) and other related attack vectors such as session hijacking and skimmers. A web server would send CSPs in the HTTP response headers Content-Security-Policy and Content-Security-Policy-Report-Only. These headers whitelist the origin locations of any style, script or other resources.

The long and short is that CSPs can help to prevent malicious scripts from being loaded, and run within your Magento 2 website.

Magento 2 now supports these headers, and by default, they are configured in “report-only” mode. Report only mode means that Magento will report any CSP violations by outputting them to your web browsers console.

Set the CSP mode

You can set the CSP mode in your own modules/extensions by editing the etc/config.xml. Change the values for storefront and/or admin to a 1 to enable report-only mode. Example below:

<?xml version="1.0"?>
<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">

How do I whitelist resources?

Magento 2 has provided a number of ways to whitelist your extension or theme resources. There are a number of CSP types that Magento supports, and these are provided in full here:

Magento recommends being cautious with your CSPs, and only add resources within extensions that require it. For example, if you only need to load a javascript file, it wouldn’t be a great idea to add a default-src policy when you could instead add a script-src policy. You can add your domain to the whitelist by adding a csp_whitelist.xml file to your extension’s etc folder.

An example is included below of how this file is structured:

<?xml version="1.0"?>
<csp_whitelist xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
        <policy id="script-src">
                <value id="devdocs-base" type="host"></value>
                <value id="magento" type="host"></value>

Are there any extensions to manage this for me?

If you are scratching your head and wondering how to approach this, fear not. There are a number of addons that have started to appear which aim to tackle the issue, including Experius and Netalco.

Magento 2.4.7 is here!
David Pratt

Magento 2.4.7 Has Arrived!

Magento 2.4.7 is set to land, with the latest release due to be launched on Tuesday the 9th of April 2024. The latest updates sees over 150 performance and security fixes as well as platform enhancements and upgrades.
Read More »
DTF Digital