How To Configure Content Security Policy In Magento 2

Ipad on Macbook Pro Beside Apple Magic Mouse

Content Security Policy (CSP) is an added layer of security for web applications. It helps to mitigate the risk of cross-site scripting (XSS) attacks by allowing a website owner to define the content sources that are trusted for their site. This added security can help protect user data and prevent malicious attackers from gaining access to sensitive information.

Speed Up Your Magento Store by 300%

In this article, we will walk through how to configure Content Security Policy in Magento 2.

Understanding Content Security Policy

Content Security Policy creates a whitelist of trusted sources for certain types of content, such as scripts, stylesheets, and images. The browser will only load content from sources that are on this whitelist. This prevents malicious scripts or other content from executing on the site.

CSP works by placing a header in the HTTP response that defines the policy for the current page. For example, the following header defines a very strict policy that only allows content from the same domain to be loaded:

Content-Security-Policy: default-src 'self'

This means that all content, such as scripts, images, and stylesheets, must come from the same domain as the website itself. If any content is loaded from a different domain, it will be blocked by the browser.

Enabling Content Security Policy in Magento 2

Magento 2 includes built-in support for Content Security Policy. To enable it, follow these steps:

  1. Log in to the admin panel of your Magento 2 store and navigate to Stores > Configuration > Advanced > Developer.

  2. Under the Security section, locate the Content Security Policy setting.

  3. Set the Enabled option to Yes.

  4. You can then specify the policy that you want to use in the Policy field. This should be a whitelist of trusted sources separated by semicolons. For example:

default-src 'self' *; 
script-src 'self' *;

In the example above, we allow content to come from our own domain as well as Google’s domains for scripts and Google Analytics.

  1. Save your changes and refresh your website to see the new policy in action.

Testing Your Content Security Policy

To test that your Content Security Policy is working correctly, you can use your browser’s developer tools.

  1. Open the developer tools and navigate to the Console tab.

  2. Look for any errors or warnings related to CSP. If your policy is too strict, you may see errors that indicate that certain content was blocked. If your policy is too lenient, you may see warnings that indicate that content is being loaded from untrusted sources.

  3. Adjust your Content Security Policy as needed and repeat the testing process until you have a policy that allows all necessary content to load while still providing adequate security.


Content Security Policy is a powerful tool for securing web applications against XSS attacks and other types of malicious content. In Magento 2, configuring Content Security Policy is relatively straightforward, and can help to protect your users and your data from harm. By understanding how this feature works and following the steps outlined above, you can easily implement a robust Content Security Policy for your Magento 2 store.

Scroll to Top