Free auditStart free
GlossaryGlossary · Performance

Content Security Policy

Content Security Policy (CSP) limits what a page may load and run. See the core directives, nonce versus hash, report-only rollout, and a quick curl check.

2 min
read
2
sources
Oct 30, 2026
updated
Scaplet Editorial Team
author
In one line

Content Security Policy (CSP) is an HTTP response header that tells the browser which sources a page may load scripts, images, frames and other resources from. It is mainly a defense against cross-site scripting. MDN recommends a strict policy built on per-response nonces over long allowlists.

30-second check

Is this costing your site traffic?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode

At a glance

4 facts from the sources below
`Content-Security-Policy`
Enforcing header
`Content-Security-Policy-Report-Only`
Test header
`default-src`
Fallback directive
`frame-ancestors`
Framing control
01At a glance

Anatomy of a policy

Part

script-src

Job
Which sources may supply JavaScript. Here only scripts carrying the matching nonce run
Part

'nonce-...'

Job
An unpredictable value generated for every response and repeated on each allowed <script> tag
Part

object-src 'none'

Job
Blocks plugin content
Part

base-uri 'none'

Job
Blocks injected <base> tags
Part

frame-ancestors

Job
Who may embed the page. MDN calls it a more flexible replacement for X-Frame-Options
Part

default-src

Job
Fallback for any fetch directive you did not list
A strict, nonce-based policy
Content-Security-Policy:
  script-src 'nonce-416d1177-4d12-4e3b-b7c9-f6c409789fb8';
  object-src 'none';
  base-uri 'none';
  frame-ancestors 'self'

Directives are separated by semicolons. The first three lines above follow the strict CSP shape MDN recommends. The frame-ancestors line is our addition, so adjust it to your embedding needs.

03How it works

Roll it out in report-only mode first

Reporting setup from MDN's pattern
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports"
Content-Security-Policy-Report-Only: default-src 'self'; report-to csp-endpoint
  1. 01

    Send the policy as report-only

    Use the Content-Security-Policy-Report-Only header. It generates reports without blocking anything. MDN says a report-only policy can't be delivered in a <meta> element.

  2. 02

    Collect violations

    Define an endpoint with Reporting-Endpoints and add report-to to the policy. MDN says report-uri is deprecated and suggests declaring both until report-to has full support.

  3. 03

    Fix what the reports show

    Add nonces to legitimate scripts, remove inline handlers, and drop sources you don't use.

  4. 04

    Switch to enforcing

    Rename the header to Content-Security-Policy. If both headers are present, MDN says both policies are honored.

04Check it

Check it in 2 minutes

A policy that blocks a script your pages need to render can change what crawlers see. After any change, test a key URL in the URL Inspection tool. For how CSP sits next to HSTS and the other headers, see security headers.

See the policy on several response types
curl -sI https://example.com/ | grep -i 'content-security-policy'
curl -sI https://example.com/app.js | grep -i 'content-security-policy'
# no output on a route means that route sends no policy
Questions

Asked a lot.

2 questions people ask next, answered from the sources on this page.

01Does a CSP replace input sanitizing?

No. MDN says setting a CSP is not an alternative to sanitizing input, and sites should do both.

02Can I use a meta tag instead of a header?

Yes, with limits. MDN says a <meta http-equiv> policy doesn't support all CSP features, and report-only policies can't be delivered that way. Prefer the header when you control the server.

Sources · 2

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • mozilla.org
  1. 01Content Security Policy (CSP) guide (MDN)developer.mozilla.org · checked 2026-11-01
  2. 02script type=speculationrules (MDN)developer.mozilla.org · checked 2026-11-01
Free · 2 minutes

Know the term. Now check your site.

The free audit reads robots.txt, sitemaps, status codes, canonicals, redirects and structured data.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • +212% organic clicks in 90 days Ledgerly