Home » Deploying Content Security Policy from Report-Only to Enforcement

Deploying Content Security Policy from Report-Only to Enforcement

Script blocks pass through a page policy stencil into a report lane and an enforcement lane.

A useful Content Security Policy rollout starts with a report-only policy that resembles the policy you intend to enforce. Review the violations, identify the scripts legitimate flows require, and move a tested subset of pages into enforcement. A nonce can authorize individual script elements if the application generates a fresh unpredictable value for each response and applies it consistently.

Inventory scripts by workflow

A page that opens correctly may still fail when a visitor starts checkout, signs in, or loads a consent-dependent widget. Trace each important flow and note inline scripts, third-party scripts and code loaded later. Find which template inserts each script and which team controls it. That map gives a violation somewhere to go besides a global list of exceptions.

Content Security Policy Level 3 defines the policy headers, directives and source matching. A Content-Security-Policy-Report-Only header lets the browser evaluate that policy without blocking under it. A separate enforced policy can still block. Configure a reporting destination supported by your browsers and test that you actually receive an event from a deliberate violation in a controlled page.

Tie the nonce to the response

In an illustrative deployment, the server creates one unpredictable value for a generated HTML response. Its script-src directive includes that nonce and the template adds the matching nonce to selected script elements. A later independent response should use a new value. Use the real generated value in both places. Mark scripts only where the application owns that decision. Automatically attaching the nonce to any HTML that reaches a page would defeat the purpose.

Check caching at the document boundary. A cached HTML body and independently replaced headers can disagree. Shared caching can also replay the same nonce across fresh visits. Decide where to generate the policy and body together, then verify their relationship after any CDN transformation. Do not assume a server-side template test covers that path.

Use reports as clues

Group reports by directive, affected page, script or blocked origin, and browser. Distinguish a first-party regression from extensions or malicious probes. Treat report content as untrusted input, keep retention narrow, and avoid publishing paths or user information from reports. An empty report stream can reflect a broken endpoint or a flow nobody exercised.

The Subresource Integrity guide covers a separate control for checking the exact bytes of a resource. CSP controls what a page may execute. Both can be useful on the same page.

Decide whether the receipts route is ready

Our receipt template had one first-party inline script. It enabled the Print preview button and rendered a preview when the reader clicked it. We served a fresh synthetic response to each browser context. The policy named a freshly generated nonce. The template either placed that value on its own script or omitted the attribute.

One legitimate workflow under three responses
Response Handler marked by template Observed result
Report-only No Button enabled. Click showed Preview ready.
Enforced No Button stayed disabled. No preview.
Enforced Yes Button enabled. Click showed Preview ready.

A successful report-only click would have approved the broken template. The middle result calls for holding enforcement on the receipts route. We marked the owned handler in the template and repeated the same user action with enforcement enabled. The results are from a local Chromium session. Browser report delivery and CDN behavior require separate observations when those components are in the rollout path.

Stage enforcement against real entry points

Start with a low-risk group of routes and retain an authorized way to revert its policy. Exercise a legitimate script and deliberate violations, including an inline script without the matching nonce. Record browsers, document responses, and outcomes. Expand only when the routes and open exceptions are understood. A policy that silently allows every script required by a demo may still fail at the boundary the rollout was meant to protect.

You may also like