Secure WooCommerce Checkout with Content Security Policy Headers and Subresource Integrity

Content Security Policy for the WooCommerce checkout

Card skimmers on online shops work by getting one extra script onto the checkout page: through a hacked plugin, a compromised third-party script or an injected database option. The script reads the card fields and sends them to the attacker’s server. A Content Security Policy tells the browser which hosts may provide scripts and receive data, so an injected script from a foreign domain is blocked and reported.

Problem: The checkout page loads scripts from the shop, the payment provider and an analytics service, and nothing would stop or even notice a script from another domain.

Solution: Send a Content-Security-Policy-Report-Only header on the checkout and cart pages from template_redirect (where is_checkout() already works), list the hosts of your payment gateway in script-src, frame-src and connect-src, collect the violation reports in a small REST endpoint, and switch to the enforcing header after a few weeks without unexpected reports.

The header

add_action( 'template_redirect', 'ha_checkout_csp' );

function ha_checkout_csp() {
    if ( ! function_exists( 'is_checkout' ) || ! ( is_checkout() || is_cart() ) ) {
        return;
    }
    $policy = implode( '; ', [
        "default-src 'self'",
        "script-src 'self' 'unsafe-inline' https://js.stripe.com https://www.googletagmanager.com",
        "frame-src 'self' https://js.stripe.com https://hooks.stripe.com",
        "connect-src 'self' https://api.stripe.com https://*.google-analytics.com",
        "img-src 'self' data: https:",
        "style-src 'self' 'unsafe-inline'",
        "form-action 'self'",
        "frame-ancestors 'self'",
        "base-uri 'self'",
        'report-uri ' . esc_url_raw( rest_url( 'ha/v1/csp-report' ) ),
    ] );
    header( 'Content-Security-Policy-Report-Only: ' . $policy );
}

The hook matters: send_headers runs before the main query, so conditional tags such as is_checkout() are always false there. template_redirect runs after the query and before any output.

The hosts above are for Stripe and Google Analytics; take the real list from your payment gateway’s documentation and from the reports.

About ‘unsafe-inline’

WordPress, WooCommerce and most gateways print inline scripts (localized data, settings, the gateway’s init code). A policy without 'unsafe-inline' needs a nonce or hash on every one of them, which is hard to complete on a WooCommerce checkout. Even with 'unsafe-inline', the policy still does the main job against skimmers: connect-src, img-src and form-action limit where data can be sent, and script-src blocks scripts loaded from foreign hosts. For inline scripts that WordPress prints through wp_print_inline_script_tag(), the wp_inline_script_attributes filter (WordPress 5.7+) can add a nonce, which is a starting point for a stricter policy later.

Collect the reports

add_action( 'rest_api_init', function () {
    register_rest_route( 'ha/v1', '/csp-report', [
        'methods'             => 'POST',
        'permission_callback' => '__return_true',
        'callback'            => function ( WP_REST_Request $request ) {
            $report = json_decode( $request->get_body(), true );
            $data   = $report['csp-report'] ?? [];
            if ( $data ) {
                error_log( sprintf(
                    'CSP %s blocked %s on %s',
                    sanitize_text_field( $data['violated-directive'] ?? '' ),
                    esc_url_raw( $data['blocked-uri'] ?? '' ),
                    esc_url_raw( $data['document-uri'] ?? '' )
                ) );
            }
            return new WP_REST_Response( null, 204 );
        },
    ] );
} );

Browsers send the reports as JSON with the content type application/csp-report, so the body is read raw. The endpoint is public by nature; it only writes a line to the log, and nothing from the report is output anywhere.

Enforce

When the log shows only expected entries (browser extensions report a lot of noise from chrome-extension:// and similar), change the header name to Content-Security-Policy. Keep the report URI: a new report after that is either a new legitimate service or an attack.

NOTE: CSP limits the damage; it does not remove the hole. Keep plugins updated, use two-factor login for administrators, and watch the checkout page’s scripts. PCI DSS 4.0 (requirement 6.4.3, mandatory from March 2025) asks shops to keep an inventory of the scripts on payment pages and to justify each one, and a report-only CSP is a practical way to build that list. For the other security headers see HTTP security headers via .htaccess.