The Complete Magento CSP Guide 2026 — Content Security Policy

CodeAtoZ Engineer, CodeAtoZ
15 min read · 2881 words
Share: 𝕏 in
MAGENTO SECURITY · #2

The Complete Magento CSP Guide 2026 — Content Security Policy

Magento CSP explained — how to roll out Content Security Policy from report-only to enforced with csp_whitelist.xml, and why it is your main defence against checkout skimmers.

Built insince 2.3.5
Reportthen restrict
Skimmerswhat it stops
PCI 4.0script controls
In this guide
What CSP doesSkimmersReport-onlycsp_whitelist.xmlEnforceMaintain

Magento CSP — Content Security Policy — is a browser security standard Magento supports natively, and it is the single most effective control against payment-card skimming. A skimmer works by getting attacker JavaScript to run on your checkout page and post card details to an external server. Magento CSP restricts which script sources may execute and where data may be sent, so even a successful injection fails to exfiltrate. This guide covers rolling out Magento CSP safely: report-only first, tuning via csp_whitelist.xml, then enforcement.

Search intent: informational/procedural — Magento developers implementing Content Security Policy. We cover what CSP does, the skimmer threat model, report-only rollout, whitelist configuration, switching to enforcement, and ongoing maintenance. It builds on our Magento security checklist and patch guide.

01

Magento CSP: What It Does and Why Skimmers Matter

Content Security Policy is an HTTP response header telling the browser which sources of script, style, image, font and connection are permitted. Anything outside the policy is blocked by the browser itself. Magento CSP has shipped as core functionality since Magento 2.3.5, with policies configurable separately for the storefront and admin areas.

The threat it addresses is specific and expensive. Magecart-style skimming attacks compromise a store — often through a vulnerable extension or unpatched core — and inject JavaScript into checkout that reads card fields and posts them to an attacker-controlled domain. Crucially, this happens client-side, so server-side scanning may not detect it, and customers see a normal checkout. With Magento CSP enforced, the injected script either cannot load (its source is not whitelisted) or cannot transmit (the destination is not permitted by connect-src). That is why CSP is the control that matters here: it breaks the attack even after a compromise.

Report-only mode blocks nothing. This is the most common Magento CSP misunderstanding. In report-only mode the browser reports violations but still executes everything, including a skimmer. The protection exists only once you switch to restrict mode. Report-only is a tuning stage, not a destination.

02

Magento CSP: Rolling Out in Report-Only Mode

The safe rollout order for Magento CSP is report-only first. Enforcing a policy before you know what your store legitimately loads will break payment iframes, analytics, chat widgets and your own theme JavaScript — usually discovered by customers at checkout. Report-only lets you collect the full picture without breaking anything.

Start by inventorying what the store loads: payment provider scripts and iframes, analytics and tag managers, chat and review widgets, font and CDN sources, and any inline scripts your theme or extensions emit. Then enable report-only and gather violations from the browser console and your report endpoint across real journeys — home, category, product, cart, checkout, and the admin.

BASH · CSP MODE PER AREA
# storefront: start in report-only (observe, block nothing)
bin/magento config:set system/csp/mode/storefront/report_only 1

# admin area, same approach
bin/magento config:set system/csp/mode/admin/report_only 1

bin/magento cache:flush

# check what the browser is actually receiving
curl -sI https://example.com/checkout/ | grep -i content-security-policy

Walk the whole purchase journey with the console open and log every violation. Expect more than you predict — third-party tags load other third-party tags, and those nested sources need whitelisting too. This inventory stage is the real work of a Magento CSP implementation.

03

Magento CSP: Configuring csp_whitelist.xml

Legitimate sources are declared in csp_whitelist.xml, placed in a module’s or theme’s etc/ directory (use etc/frontend/ or etc/adminhtml/ to scope a policy to one area). The correct practice for Magento CSP is to add these in your own module rather than editing core files, so the configuration survives upgrades and lives in version control.

XML · app/code/Vendor/Module/etc/frontend/csp_whitelist.xml
<?xml version="1.0"?>
<csp_whitelist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <policies>
        <policy id="script-src">
            <values>
                <value id="payment_provider" type="host">js.paymentprovider.com</value>
                <value id="analytics" type="host">www.googletagmanager.com</value>
            </values>
        </policy>
        <policy id="connect-src">
            <values>
                <value id="payment_api" type="host">api.paymentprovider.com</value>
            </values>
        </policy>
        <policy id="frame-src">
            <values>
                <value id="payment_iframe" type="host">secure.paymentprovider.com</value>
            </values>
        </policy>
    </policies>
</csp_whitelist>

Two directives deserve particular attention in any Magento CSP policy. script-src controls what may execute — obviously central. But connect-src is what actually stops exfiltration: even if a skimmer runs, it cannot POST card data to a destination your policy does not permit. Teams often whitelist script sources carefully and leave connect-src permissive, which undermines the entire defence.

DirectiveControlsWhy it matters for skimming
script-srcWhich scripts may executeBlocks the injected skimmer loading
connect-srcWhere XHR/fetch may send dataBlocks exfiltration of card data
frame-srcWhich iframes may embedPayment iframes; blocks fake overlays
style-srcStylesheet sourcesPrevents CSS-based data leaks
form-actionWhere forms may submitBlocks redirected checkout POSTs
04

Magento CSP: Enforcing and Maintaining the Policy

Once report-only runs clean across every journey, switch Magento CSP to restrict mode — this is where protection begins. Do it on staging first, walk the full purchase path including a real payment, then promote. Watch closely for the first days, because low-traffic pages and less-common payment methods surface violations that testing missed.

BASH · SWITCH TO ENFORCEMENT
# report_only = 0  ->  policy is ENFORCED
bin/magento config:set system/csp/mode/storefront/report_only 0
bin/magento cache:flush

# confirm the enforcing header is present (not -Report-Only)
curl -sI https://example.com/checkout/ | grep -i "content-security-policy"

Maintenance is where most Magento CSP implementations quietly decay. Every new marketing tag, payment method or extension may introduce a new source that the policy blocks — so CSP has to become part of your change process, not a one-off project. Keep the report endpoint active even in enforce mode so you see what is being blocked, and review those reports: a sudden violation from an unfamiliar domain is exactly the signal CSP exists to give you. That signal is genuinely valuable, and worth more than the initial rollout. The MDN CSP reference and Adobe’s Commerce CSP documentation cover directives and module configuration in full. Our Magento team runs CSP rollouts end to end.

CodeAtoZ Experience

What we have learned rolling out Magento CSP

Most stores that “have CSP” are in report-only and think they are protected. It is the most consequential misunderstanding in this area — report-only observes and blocks nothing, so a skimmer runs exactly as it would with no policy at all. We treat enforcement as the only meaningful milestone.

connect-src is where the real protection lives. Teams carefully curate script sources and then leave connect-src wide open, which means an injected script can still post card data anywhere. We tighten connect-src deliberately, because that directive is what turns a compromise into a failed attack.

CSP decays without process. A policy tuned perfectly at launch breaks the first time marketing adds a tag — or worse, someone loosens it to make the tag work. We put CSP into the change checklist so new scripts get whitelisted deliberately rather than by weakening the policy under deadline pressure.

Reviewed by CodeAtoZ Engineering Team
❓

Frequently Asked Questions About Magento CSP

Q

What is Magento CSP?

Magento CSP is native support for Content Security Policy, an HTTP header telling the browser which sources may load scripts, styles, frames and connections. It has shipped as core functionality since Magento 2.3.5, configurable separately for storefront and admin, and is the primary defence against injected payment skimmers.

Q

What is the difference between report-only and restrict mode?

Report-only reports violations to the console and your report endpoint but still executes everything, including a skimmer — it blocks nothing. Restrict mode enforces the policy and blocks disallowed sources. Report-only is a tuning stage; the security benefit of Magento CSP exists only once you switch to restrict.

Q

How does Magento CSP stop card skimming?

Skimmers inject JavaScript into checkout that reads card fields and posts them to an attacker’s server. An enforced policy blocks this two ways: script-src prevents the unauthorised script executing, and connect-src prevents data being sent to a destination not on the whitelist — so exfiltration fails even if injection succeeds.

Q

Where do I whitelist domains for Magento CSP?

In a csp_whitelist.xml file inside your own module’s etc/ directory, using etc/frontend/ or etc/adminhtml/ to scope to an area. Declaring sources in your own module rather than editing core files means the configuration survives upgrades and stays in version control alongside the rest of your code.

Q

Will enabling CSP break my Magento checkout?

It can, if you enforce before tuning — payment iframes, analytics and chat widgets often load sources you have not whitelisted. That is exactly why the rollout order matters: run report-only, walk every journey including a real payment, whitelist all legitimate sources, then enforce on staging before production.

Q

Do I still need CSP if I use a hosted payment iframe?

Yes. A hosted iframe protects card fields from your page, but skimmers can still overlay fake forms, redirect submissions or capture data elsewhere in checkout. Magento CSP’s frame-src and form-action directives constrain exactly those techniques, and PCI DSS 4.0 expects script controls on payment pages regardless of iframe use.

Need CSP Enforced Without Breaking Checkout?

Our team runs Magento CSP rollouts — source inventory, whitelist tuning, safe enforcement and ongoing monitoring.

CSP rolloutWhitelist tuningSkimmer defencePCI alignment
CodeAtoZ
Lead Engineer & Founder, CodeAtoZ

10+ years building Magento 2 and WooCommerce platforms for high-volume e-commerce businesses. Writes about performance engineering, plugin architecture, and scalable commerce systems.

Join the Discussion

Your email address will not be published. Required fields are marked *