Content Security Policy Examples

Example Content Security Policies. Includes policies with different directives, sources and security tradeoffs.

This article contains a collection of Content-Security-Policy examples, starting with the recommended Strict CSP patterns.

This is the recommended starter policy for most dynamic web applications.

  • script-src 'nonce-...' 'strict-dynamic' 'report-sample' only allows <script> tags carrying the matching per-request nonce attribute (and any scripts they dynamically load) to execute.
  • object-src 'none' blocks legacy plugin embeds (<object>, <embed>).
  • base-uri 'none' prevents attackers from injecting a <base> tag to hijack relative URLs (use 'self' if your app requires <base>).
  • report-uri and report-to send violation reports to your endpoint.

The nonce must be cryptographically random and unique on every HTTP response, and attached to every allowed script tag:

<script nonce="5e442efeecd37e3d7e7ea5b6d6cd0dc2" src="/main.js"></script>

If you need backwards compatibility with very old browsers that do not support 'strict-dynamic' or nonces, you can append 'unsafe-inline' https: to script-src:

Modern browsers ignore 'unsafe-inline' and https: when a nonce and 'strict-dynamic' are present.

Strict CSP (Hash-Based)

If your site is static or cached at a CDN and you cannot inject a fresh nonce on every request, use a hash-based Strict CSP instead.

The 'sha256-...' source allows Javascript who's exact content matches that SHA-256 digest, and 'strict-dynamic' lets that loader script dynamically load the rest of your application bundles. When computing hashes, be careful with whitespace and newlines (browser DevTools console errors and Reporting APIs will show the exact expected hash).

Reporting (report-uri and report-to)

Pair the policy with a Reporting-Endpoints HTTP response header:

Reporting-Endpoints: csp-endpoint="https://demo.endpoint.csper.io/?v=1"

When a browser blocks a resource, it sends a JSON violation report to your endpoint. Including both report-uri and report-to gives you full coverage across older and newer browsers. Csper collects and analyzes these reports (Learn More).

Clickjacking Protections

Previously, to protect against clickjacking attacks (where an attacker embeds your site in a hidden iframe to trick a victim into clicking something unintended), it was recommended to use the X-Frame-Options header. It is now recommended to use the frame-ancestors directive instead.

The above policy allows the webpage to be embedded in an iframe only if the parent page is from the same origin or a subdomain of *.mydomain.com. Use frame-ancestors 'none' to block all framing.

Upgrading Insecure Requests

The upgrade-insecure-requests directive automatically converts all http:// requests to https://. This complements Strict-Transport-Security (HSTS), which applies to navigations to your domain, whereas upgrade-insecure-requests applies to all resources requested by the page.

Unsafe Policy (Anti-Pattern)

By default, Content Security Policy blocks inline JavaScript and eval()-style sinks because they are the primary vectors for XSS.

Adding 'unsafe-inline' (without a nonce or hash) in script-src allows arbitrary injected inline scripts to run and defeats the XSS protection of CSP (info). Similarly, 'unsafe-eval' re-enables eval() and new Function(), which should be avoided whenever possible.

Legacy Allowlist CSP

Historically, CSP was deployed as a host allowlist specifying every allowed domain per resource type (script-src, style-src, img-src, font-src, connect-src, etc.). In practice, allowlists are brittle to maintain and frequently bypassable via JSONP endpoints or open redirects on allowlisted CDNs. For new deployments, use Strict CSP instead, and optionally add secondary directives like frame-ancestors, img-src, or connect-src only if you have specific defense-in-depth requirements.

Recommendations

  • Always roll out a new policy in Content-Security-Policy-Report-Only mode first so you can monitor violations without breaking production.
  • Always include report-uri and report-to (Reporting-Endpoints) so you have visibility into blocked scripts and potential XSS attempts (Learn More).

An Introduction To Content Security Policy

An introduction to content security policy and cross site scripting.

Getting Started with Content Security Policy using NodeJS/Express and Csper

This guide covers building and installing content security policy (CSP) on a nodejs express server. This guide covers content security policy, generating a content security policy, report-uri, inline scripts, and deployment.

Other Security Features of Content Security Policy

Other security features of content security policy including upgrade-insecure-requests, block-all-mixed-content, frame-ancestors, sandbox, form-actions, and more

Looking for tools to make CSP easier?

Csper has the tools to help you understand, deploy and manage your content security policy. Get started in minutes. Report aggregations, classification, analysis, alerting, realtime and more. Free 14 day trial.