Introduction
A Content Security Policy (CSP) tells browsers which sources of content are trusted, mitigating cross‑site scripting (XSS) and data‑injection attacks. For e‑commerce sites, CSP not only hardens security but also signals trust to search engines, contributing to better rankings.
1. CSP Basics
| Directive | Purpose |
|---|---|
| default-src | Fallback for any resource type not explicitly listed. |
| script-src | Controls allowed JavaScript origins. |
| style-src | Controls allowed CSS origins. |
| img-src | Controls allowed image origins. |
| frame-src | Controls allowed iframes (e.g., payment widgets). |
| object-src | Blocks Flash/Java applets (rarely needed). |
| base-uri | Restricts <base> tag usage. |
| form-action | Limits where forms can be submitted. |
2. CSP for Magento 2
2.1 Enable Magento CSP Module
Magento ships with a CSP module
(Magento_Csp). Enable it:
bin/magento module:enable Magento_Csp
bin/magento setup:upgrade
bin/magento cache:flush2.2 Default Policy
Magento ships with a sensible default that whitelists Magento’s own
domains and common CDNs. You can view it in
app/code/Magento/Csp/etc/csp_whitelist.xml. ### 2.3 Custom
Whitelisting Create a module Vendor/CustomCsp with
etc/csp_whitelist.xml:
<?xml version="1.0"?>
<CspWhitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Csp:etc/csp_whitelist.xsd">
<policy id="script-src">
<value id="google-analytics" type="host">https://www.google-analytics.com</value>
<value id="cloudflare" type="host">https://cdnjs.cloudflare.com</value>
</policy>
<policy id="style-src">
<value id="google-fonts" type="host">https://fonts.googleapis.com</value>
</policy>
<policy id="img-src">
<value id="cdn" type="host">https://cdn.example.com</value>
</policy>
</CspWhitelist>Deploy and run setup:upgrade again. ### 2.4 Reporting
Mode Add a report‑only header to test without breaking
functionality:
<type>report-only</type>Monitor reports at https://csp-report.example.com.
3. CSP for Shopify
Shopify’s hosted environment limits direct server header
manipulation, but you can add CSP via Theme Settings or
Shopify Functions. ### 3.1 Add CSP in
theme.liquid
{% assign csp = "default-src 'self'; script-src 'self' https://www.google-analytics.com https://cdnjs.cloudflare.com; style-src 'self' https://fonts.googleapis.com; img-src 'self' https://cdn.shopify.com data:; frame-src 'self' https://checkout.shopify.com;" %}
<meta http-equiv="Content-Security-Policy" content="{{ csp }}" />
3.2 Using Shopify Scripts (Beta)
Create a script tag via the Admin API with
Content-Security-Policy-Report-Only header for testing.
4. Testing CSP
- Chrome DevTools → Security panel shows CSP violations.
- Use csp-evaluator (Google) to scan your policy.
- Enable Report‑Only first, collect reports at
https://csp-report.example.com.
5. SEO Impact
| Impact | Explanation |
|---|---|
| Reduced XSS | Fewer security incidents → higher trust → better rankings. |
| Improved Crawlability | Clean pages without injected malicious scripts ensure Google can index without penalties. |
| Performance | Blocking unwanted third‑party scripts reduces render‑blocking resources, improving Core Web Vitals. |
6. Common Pitfalls & Mitigations
| Pitfall | Symptom | Fix |
|---|---|---|
Over‑restrictive script-src |
Legitimate third‑party scripts (e.g., payment gateway) blocked, causing checkout errors. | Add explicit host to whitelist, test in Report‑Only mode first. |
Missing nonce for inline scripts |
Inline analytics code fails. | Use nonce‑{{ nonce }} and add the nonce to the CSP
header dynamically. |
| Mixed‑content warnings | HTTPS page loads HTTP resources, CSP blocks them, causing broken images. | Ensure all assets are served over HTTPS or add
upgrade-insecure-requests. |
7. Checklist for CSP Deployment
8. Call‑to‑Action
A well‑crafted CSP protects your Magento or Shopify storefront while boosting SEO performance. Ready to lock down your site with a custom Content Security Policy? Click the “Start a project” button in the MODRACX portfolio, and let’s secure your e‑commerce experience together.
Kenneth D’Silva – Magento & Shopify specialist, MODRACX