Creating a Strict Content Security Policy

Step-by-step guide to creating and rolling out a Strict Content Security Policy (CSP).

In this guide we'll quickly rollout and test a new Content-Security-Policy using Strict CSP.

Step 1: Choose Nonce-Based vs. Hash-Based Strict CSP

Pick the approach that matches how your HTML is served:

  • Nonce-based Strict CSP (Recommended for dynamic servers): Your backend (Node.js, Go, Django, Rails, etc.) generates a cryptographically random nonce on every HTTP response, inserts it into the CSP header, and adds nonce="..." to your <script> tags.
  • Hash-based Strict CSP (For static sites or CDN-cached HTML): If your HTML is static and cannot be modified per request, compute a SHA-256 hash of a small inline bootstrap <script> that dynamically loads your application scripts, and put 'sha256-...' in the policy.

Step 2: Deploy in Report-Only Mode

Before enforcing anything, deploy the Strict CSP template using Content-Security-Policy-Report-Only along with a Reporting-Endpoints header so browsers report violations to Csper without blocking users:

Reporting-Endpoints: csp-endpoint="https://YOUR_PROJECT.endpoint.csper.io/?v=1"
Content-Security-Policy-Report-Only: script-src 'nonce-RANDOM_PER_REQUEST' 'strict-dynamic' 'report-sample'; object-src 'none'; base-uri 'none'; report-uri https://YOUR_PROJECT.endpoint.csper.io/?v=1; report-to csp-endpoint

See Code Snippets for Deploying CSP for ready-to-use middleware examples in Node.js/Express, Go, Django, Nginx, and Apache.

Step 3: Add Nonces (or Hashes) and Refactor Inline Handlers

With Content-Security-Policy-Report-Only active, Csper will show you every script or inline handler that would be blocked:

  1. Tag trusted <script> elements with the per-request nonce (e.g., <script nonce="RANDOM_PER_REQUEST" src="/app.js"></script>). Because of 'strict-dynamic', any scripts dynamically loaded by a nonced script are automatically trusted.
  2. Refactor inline event handlers and javascript: URIs (such as onclick="..." or href="javascript:..."), since they cannot carry a nonce. See Removing Inline Resources for migration examples.
  3. Check object-src and base-uri: If your app uses a <base> tag, change base-uri 'none' to base-uri 'self'.

Step 4: Monitor Reports and Switch to Enforce Mode

Let your Report-Only policy run in production long enough to cover normal user traffic and review the incoming violations in your Csper dashboard (Learn More about Reporting).

csper report dashboard

Csper's CSP Reporting Dashboard

Once violation reports from your own application drop to zero, rename the header from Content-Security-Policy-Report-Only to Content-Security-Policy to begin enforcing protection against XSS, and configure Alerts to notify your team of new violations.

Legacy Allowlist Generation

If you are maintaining a legacy host-allowlist policy instead of Strict CSP, the historical Csper Builder browser extension (Chrome / Firefox) can still collect domain allowlist reports. For all new deployments, however, Strict CSP is strongly recommended.