Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Content Security Policy: Rules, Violations, Reporting, and Rollout

How Content-Security-Policy headers work, how Cloudflare content security rules enforce or log violations, and how to roll out CSP safely.

Most of this page follows Cloudflare Client-side security documentation on content security rules (formerly Page Shield policies), which generate Content-Security-Policy and Content-Security-Policy-Report-Only headers at the edge. It is grounded in the same HTTP mechanism described in Chrome’s Content Security Policy overview and Cloudflare’s CSP fundamentals.

A Content Security Policy (CSP) is an HTTP response header that tells the browser which scripts, connections, images, and other resources are allowed. It adds a layer of defense against content injection, cross-site scripting (XSS), malicious embeds, and related attacks. Browsers enforce allowlists: resources that do not match are blocked (enforcement mode) or only reported (report-only mode).

Content-Security-Policy headers: enforcement vs report-only

Section titled “Content-Security-Policy headers: enforcement vs report-only”

CSP is usually sent as Content-Security-Policy (enforced) or Content-Security-Policy-Report-Only (monitored). Chrome’s guidance for new deployments is to start in report-only:

If you’re just starting out with CSP, it makes sense to evaluate the current state of your application before rolling out a draconian policy to your users. As a stepping stone to a complete deployment, you can ask the browser to monitor a policy, reporting violations but not enforcing the restrictions. Instead of sending a Content-Security-Policy header, send a Content-Security-Policy-Report-Only header.

— Google Chrome, Content Security Policy

The policy value is a semicolon-separated list of directives (for example script-src, connect-src, default-src). Each directive names allowed sources: hostnames, full URIs, schemes, or quoted keywords such as 'self', 'none', 'unsafe-inline', or hash values like 'sha256-…'.

Delivery via HTTP is preferred over a <meta http-equiv="Content-Security-Policy"> tag, because injection before the meta tag can bypass the policy, and some directives (including frame-ancestors, report-uri, and sandbox) cannot be set in meta tags. See Ensure CSP is effective against XSS attacks.

Section titled “How script-src and related directives reduce XSS risk”

Instead of blindly trusting everything that a server delivers, CSP defines the Content-Security-Policy HTTP header, which allows you to create an allowlist of sources of trusted content, and instructs the browser to only execute or render resources from those sources. Even if an attacker can find a hole through which to inject script, the script won’t match the allowlist, and therefore won’t be executed.

— Google Chrome, Content Security Policy

For XSS-focused policies, Lighthouse’s CSP audit expects script-src, object-src, and base-uri (or default-src covering them), with no syntax errors. object-src limits plugins; base-uri blocks unauthorized <base> tags that could retarget relative script URLs.

Broad domain allowlists alone are weak: trusted hosts with JSONP or open redirects can still execute attacker-controlled script. Lighthouse recommends nonces or hashes plus 'strict-dynamic' instead of relying only on host allowlists. Cloudflare content security rules support hashes in rule values but do not support nonce directives in rules; for nonce-based strict CSP you typically set headers on the origin or via response header transform rules.

Cloudflare content security rules: Log vs Allow actions

Section titled “Cloudflare content security rules: Log vs Allow actions”

Content security rules (Client-side security Advanced) define which resources may load on matching requests. Cloudflare turns your configuration into CSP directives on responses.

Aspect Log rule Allow rule
CSP header Content-Security-Policy-Report-Only Content-Security-Policy
Browser behavior Loads resources; reports violations Blocks resources not in the allowlist
Typical use Validate allowlist before enforcement Positive security model (default deny for listed types)

Log rules report resources that fall outside your allowlist without blocking them. Allow rules block any resource not explicitly listed.

— Cloudflare, Content security rules

Log rules are for discovery; Allow rules enforce. How client-side security works contrasts this with automatic resource monitoring, which adds a sampled report-only header for script discovery, while customer-defined content security rules apply to 100% of matching responses and are not sampled.

In the dashboard, open Create a content security rule: scope the rule with an expression (for example http.host eq "myapp.example.com"), choose allowed directives and sources, then set action Log or Allow.

The Client-side security API accepts a value string of CSP directives. Example body uses action: "log" and a script-src allowlist:

curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/page_shield/policies" \
--request POST \
--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
--json '{
"description": "My first content security rule in log mode",
"action": "log",
"expression": "http.host eq \"myapp.example.com\"",
"enabled": "true",
"value": "script-src myapp.example.com cdnjs.cloudflare.com https://www.google-analytics.com/analytics.js '\''self'\''"
}'

With "action": "allow", scripts outside the allowlist are blocked. Supported directives in rules are listed in Supported CSP directives; script-src and connect-src are monitored by Cloudflare, while others (for example img-src, frame-ancestors) can still be set in the rule value.

Origin CSP headers alongside Cloudflare rules

Section titled “Origin CSP headers alongside Cloudflare rules”

When you create content security rules, Cloudflare will generate content security policy (CSP) directives from those rules based on their configuration: Log rules will create CSP directives for the Content-Security-Policy-Report-Only HTTP header. Allow rules will create CSP directives for the Content-Security-Policy HTTP header.

Client-side security only adds new CSP HTTP headers to the response. This means that Cloudflare will keep any Content-Security-Policy-Report-Only and Content-Security-Policy HTTP headers in the response set by the origin server and it will add separate HTTP headers for the content security rules configured on your Cloudflare zone.

— Cloudflare, Client-side security FAQ

When multiple Content-Security-Policy headers are present, browsers apply all of them and the most restrictive policy wins. Cloudflare recommends only one Allow rule in enforce mode. Cloudflare’s CDN does not change origin CSP except in documented cases (for example Zaraz). To add or replace headers yourself, you can use response header transform rules or a _headers file on Cloudflare Pages.

Some Cloudflare products require extra script-src or connect-src entries in your CSP (for example Web Analytics and Web Analytics FAQ). Test changes in report-only or Log mode before production.

Violations, reporting endpoints, and monitoring

Section titled “Violations, reporting endpoints, and monitoring”

A rule violation means the browser loaded a resource not covered by your allowlist. For Log rules the resource still loads; for Allow rules it is blocked. With Client-side security Advanced, violations appear in the dashboard (Content security rule violations) and via GraphQL or Logpush.

Continuous script monitoring sends CSP reports to a default endpoint documented in Configuration settings:

By default, reports are sent to a Cloudflare-owned endpoint: https://csp-reporting.cloudflare.com/cdn-cgi/script_monitor/report?<QUERY_STRING>

— Cloudflare, Client-side security settings

Advanced customers may use the same hostname (<YOUR-HOSTNAME>/cdn-cgi/script-monitor/report?<QUERY_STRING>) if no rate limiting or custom rules match cdn-cgi/*. The CSP HTTP header format page shows how report-only headers include report-uri toward that endpoint.

For your own CSP (not only Cloudflare rules), configure report-uri and, where supported, report-to with a Reporting-Endpoints header so violations reach your collector—see Monitor your web application with the Reporting API and Configure CSP reporting. In Chrome DevTools, Application → Background services → Reporting API lists queued and sent reports (Debug background services).

Redirect chains (for example some ad networks) can make violation reports list the original host, not the redirect target—troubleshooting redirects suggests adding destination domains to the allowlist.

Deploy content security rules in production warns against flipping enforce mode without validation:

  • Test in staging; use Log for at least seven days before Allow.
  • Change one rule at a time; monitor violations for 24 hours after each change.
  • Do not edit production rules blindly or delete all rules at once; document rollback.

The get started flow matches this: create a Log rule, review violations on Security rules, update sources, then switch to Allow.

Parallel pattern from Chrome for hand-written CSP: enforce one policy while monitoring another with Content-Security-Policy-Report-Only, or use report-only alone until reports are clean (Content Security Policy — Report-Only).

Strict CSP and Trusted Types (origin-controlled)

Section titled “Strict CSP and Trusted Types (origin-controlled)”

Beyond allowlists, strict CSP with nonces and Trusted Types harden DOM XSS sinks. Trusted Types rollout often starts with:

Content-Security-Policy-Report-Only: require-trusted-types-for 'script';

then moves to enforcement on the Content-Security-Policy header (Trusted Types guide). These directives are separate from Cloudflare content security rule allowlists but complement them on the same site.

  • Multiple enforcing CSP headers from origin plus Allow rules (or transform Add), causing stricter-than-intended blocking.
  • Switching Log → Allow without reviewing violations (legitimate third-party scripts blocked).
  • Rule scope vs URL rewrite: content security rules evaluate after URL rewrite rules; match raw.http.request.uri.path if needed.
  • Host allowlist bypass: relying on script-src https://trusted.example.com without nonces/strict-dynamic when JSONP or redirects exist on that host (CSP XSS audit).
  • Expecting nonces in Cloudflare rules: use hashes in rule values or set nonces on the origin (FAQ).
  • 'self' on SSL for SaaS zones: scoped alerts may fire for every custom hostname under a root zone (troubleshooting).

CSP does not replace other headers. Clickjacking is mitigated with X-Frame-Options or CSP frame-ancestors (HTTP only). Subresource Integrity pins third-party bytes. Connection-Allowlist is a newer, network-wide allowlist that can be paired with CSP for defense in depth. AngularJS v1.1.0+ works under strict CSP without extra tweaks; older versions need ng-csp.

  1. Load a representative page in Chrome, open DevTools → Network, select the document response, and confirm which Content-Security-Policy and Content-Security-Policy-Report-Only headers are present (origin vs Cloudflare). Cross-check How client-side security works.

  2. In the Cloudflare dashboard, open Security rules and review content security rule violation sparklines and top blocked or reported resources.

  3. Trigger a known-bad script URL in a test environment with a Log rule active; confirm a violation appears before switching the rule to Allow (deploy rules in production).

  4. Run Lighthouse Best Practices (CSP XSS) on staging to see whether enforcement CSP is missing or bypassable (Ensure CSP is effective against XSS attacks).

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact sentence you are asking about, and we will correct or clarify it.