CSP für Shopware 6 konfigurieren

Content Security Policy über das native CSP-System einrichten — mit Payment-Provider-Domains, %nonce%-Platzhalter und separaten Policies für Storefront und Administration.

Shopware · Schritt für Schritt

Content Security Policy in Shopware 6

Content Security Policy (CSP) ist der wichtigste HTTP-Security-Header gegen Cross-Site Scripting (XSS). Er teilt dem Browser mit, welche Ressourcen geladen werden dürfen — und blockiert alles andere. CSP ist mit 35 von 166 Punkten der einflussreichste Header im Wolf-Agents Web Security Check.

Shopware 6.5+ bietet ein natives CSP-System über config/packages/shopware.yaml mit dem Schlüssel csp_templates. Vier Kontexte (default, storefront, administration, store-api) erlauben separate Policies. Der %nonce%-Platzhalter wird automatisch pro Request durch einen kryptographisch sicheren Nonce ersetzt.

Besonders kritisch für Shopware: Payment-Provider wie PayPal, Stripe, Klarna und Mollie laden externe Scripts und iFrames. Ohne korrekte CSP-Konfiguration bricht der Checkout — und ohne CSP riskieren Sie die PCI DSS 4.0 Compliance (Requirement 6.4.3).

Implementierung: shopware.yaml und EventSubscriber

Variante A (empfohlen): Das native CSP-System über csp_templates in shopware.yaml. Variante B: Einen EventSubscriber für Report-Only-Tests oder erweiterte CSP-Konfigurationen, die über das native System hinausgehen.

Variante A — shopware.yaml (Empfohlen)
config/packages/shopware.yaml Basis-CSP
# config/packages/shopware.yaml — Storefront CSP
shopware:
    security:
        csp_templates:
            default:
                default-src: "'self'"
                object-src: "'none'"
                base-uri: "'self'"
                form-action: "'self'"
                frame-ancestors: "'none'"
            storefront:
                script-src: "'self' '%nonce%' 'strict-dynamic'"
                style-src: "'self' 'unsafe-inline'"
                img-src: "'self' data: https:"
                font-src: "'self'"
                connect-src: "'self'"
                frame-src: "'self'"
                # Payment-Provider (an Ihren Shop anpassen)
                # PayPal
                # script-src: *.paypal.com *.paypalobjects.com
                # frame-src: *.paypal.com
                # connect-src: *.paypal.com
                # Stripe
                # script-src: js.stripe.com
                # frame-src: *.stripe.com
                # connect-src: *.stripe.com
            administration:
                script-src: "'self' 'unsafe-eval' 'unsafe-inline'"
                style-src: "'self' 'unsafe-inline'"
                img-src: "'self' data: https:"
                connect-src: "'self'"
                frame-src: "'self' blob:"
Payment-Provider-Domains (vollständig)
config/packages/shopware.yaml Produktiv
# config/packages/shopware.yaml — vollständige Payment-CSP
shopware:
    security:
        csp_templates:
            storefront:
                script-src: >-
                    'self' '%nonce%' 'strict-dynamic'
                    *.paypal.com *.paypalobjects.com
                    js.stripe.com
                    *.klarna.com *.klarnaservices.com
                    js.mollie.com
                    *.amazonpay.com
                frame-src: >-
                    'self'
                    *.paypal.com *.stripe.com
                    *.klarna.com
                    *.mollie.com
                    *.amazonpay.com *.amazon.de
                connect-src: >-
                    'self'
                    *.paypal.com *.stripe.com
                    *.klarna.com *.klarnaservices.com
                    *.mollie.com
                    *.amazonpay.com
Variante B — EventSubscriber (Report-Only)
SecurityHeaderSubscriber.php Report-Only
// EventSubscriber für CSP Report-Only (Testphase)
public function onResponse(ResponseEvent $event): void
{
    $response = $event->getResponse();

    // CSP Report-Only Header parallel zum nativen CSP setzen
    $reportPolicy = implode('; ', [
        "default-src 'self'",
        "script-src 'self' 'strict-dynamic'",
        "report-uri /api/csp-report",
    ]);
    $response->headers->set(
        'Content-Security-Policy-Report-Only',
        $reportPolicy
    );
}
Administration braucht unsafe-eval

Das Shopware-Admin-Panel basiert auf Vue.js und benötigt 'unsafe-eval' in script-src. Das ist eine bekannte Einschränkung. Verwenden Sie niemals 'unsafe-eval' in der Storefront-CSP — nur in der administration-Konfiguration.

Verifizierung

Testen Sie die CSP separat für Storefront und Administration. Prüfen Sie besonders den Checkout-Prozess mit allen aktiven Payment-Providern. Lassen Sie die Policy mindestens 1 Woche im Report-Only-Modus laufen.

Terminal Verifizierung
# Cache leeren
bin/console cache:clear

# CSP-Header der Storefront prüfen
curl -sI https://ihr-shop.de | grep -i content-security-policy

# Erwartete Ausgabe:
# content-security-policy: default-src 'self'; script-src 'self' 'nonce-...' ...

# CSP-Header der Administration prüfen
curl -sI https://ihr-shop.de/admin | grep -i content-security-policy
Testen Sie den kompletten Checkout-Flow: Warenkorb, Payment-Auswahl, iFrame-Einbettung, Bestätigungsseite. Jeder Payment-Provider lädt unterschiedliche externe Ressourcen.

Häufige Fehler bei CSP in Shopware

Payment-Provider nach CSP blockiert

Der Checkout bricht ab, weil PayPal/Stripe/Klarna-Scripts nicht geladen werden können. Lösung: Payment-Provider-Domains in script-src, frame-src und connect-src des storefront-Kontexts aufnehmen.

Administration-CSP vs Storefront-CSP verwechselt

Die Administration braucht 'unsafe-eval', die Storefront darf es nicht haben. Shopware wendet den passenden Kontext automatisch an — aber nur wenn Sie die Kontexte in csp_templates korrekt trennen.

Google Analytics/Tag Manager blockiert

Tracking-Scripts benötigen *.google-analytics.com, *.googletagmanager.com in script-src und connect-src. Auch *.google.com und *.google.de für reCAPTCHA im Storefront-Kontext freigeben.

bin/console cache:clear vergessen

Änderungen in shopware.yaml werden erst nach Cache-Invalidierung aktiv. Im Produktivbetrieb: bin/console cache:clear && bin/console cache:warmup für minimale Downtime.

Compliance-Relevanz

CSP ist für Shopware-Shops mit Online-Zahlungen nicht optional — PCI DSS 4.0 macht sie zur Pflicht. Die korrekte Konfiguration erfüllt gleichzeitig Anforderungen mehrerer Frameworks.

PCI DSS 4.0Requirement 6.4.3 — Kontrolle aller auf Zahlungsseiten geladenen Scripts. Seit März 2025 verpflichtend. CSP ist die effektivste Methode zur Umsetzung.
NIS2Art. 21(e) — Sicherheit bei Erwerb, Entwicklung und Wartung von Netz- und Informationssystemen
BSIAPP.3.1 — Webserver-Absicherung mit Security Headern, speziell CSP gegen XSS
DSGVOArt. 32 — Technische Maßnahmen zum Schutz personenbezogener Daten im E-Commerce

Wie steht Ihre Domain bei Content Security Policy?

Prüfen Sie es jetzt — kostenlos, ohne Registrierung, mit 166 Prüfpunkte.