# 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.

Canonical: https://scaplet.com/glossary/content-security-policy

Glossary Glossary · Performance

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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fglossary%2Fcontent-security-policy%20(%22Content%20Security%20Policy%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fglossary%2Fcontent-security-policy%20(%22Content%20Security%20Policy%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Fglossary%2Fcontent-security-policy%20(%22Content%20Security%20Policy%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Fglossary%2Fcontent-security-policy%20(%22Content%20Security%20Policy%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

## 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

_01_At 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.

_02_At a glance

## Nonce, hash, or allowlist

Approach

### Nonce

Works for

Pages rendered by a server or edge function

Catch

Must differ on every response, so static HTML can't carry it

Approach

### Hash (`sha256-`, `sha384-`, `sha512-`)

Works for

Static pages

Catch

Changes whenever the script changes. External scripts also need an `integrity` attribute

Approach

### Host allowlist

Works for

Simple setups

Catch

MDN calls allowlist CSPs hard to get right and hard to maintain

`'strict-dynamic'` lets a trusted script load further scripts, which eases third-party tags. MDN notes the trade-off: it weakens protection if those scripts create script elements from untrusted input.

**Avoid unsafe-inline**MDN advises avoiding `'unsafe-inline'` because it defeats much of the point of a CSP. Browsers ignore it in a directive that also contains a nonce or hash. Inline event handlers and `javascript:` URLs stop working once a script policy exists, so move them to `addEventListener()`.

_03_How 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.

_04_Check it

## Check it in 2 minutes

**Set it on every response**MDN says a CSP should be set on all responses, not only the main document. Check static files and API routes as well as HTML templates.

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](/glossary/url-inspection-tool). For how CSP sits next to HSTS and the other headers, see [security headers](/glossary/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.

_01_ Does a CSP replace input sanitizing?

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

_02_ Can 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. 01 [Content Security Policy (CSP) guide (MDN)](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) developer.mozilla.org · checked 2026-11-01
2. 02 [script type=speculationrules (MDN)](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/script/type/speculationrules) developer.mozilla.org · checked 2026-11-01

## Keep learning

[All glossary](/glossary)

[Learn CORS Performance](/glossary/cors) [Related HTTPS Crawling and indexing](/glossary/https) [Read next Speculation rules Performance](/glossary/speculation-rules) [Do Run a free site audit Free, about two minutes](/site-audit)

Nearby in glossary
[Content pruning](/glossary/content-pruning) [Content refresh](/glossary/content-refresh) [Content strategy](/glossary/content-strategy) [Content syndication](/glossary/content-syndication)

More in Performance
[Time to First Byte (TTFB)](/glossary/time-to-first-byte) [Time to Interactive (TTI)](/glossary/time-to-interactive) [Total Blocking Time (TBT)](/glossary/total-blocking-time) [TTL (time to live)](/glossary/ttl) [Viewport meta tag](/glossary/viewport-meta-tag) [WebP](/glossary/webp) [AVIF](/glossary/avif) [bfcache (back/forward cache)](/glossary/bfcache) [Brotli compression](/glossary/brotli-compression) [Cache-Control](/glossary/cache-control) [CDN](/glossary/cdn) [Chrome UX Report (CrUX)](/glossary/chrome-ux-report) [Content Security Policy](/glossary/content-security-policy) [Content-Length (HTTP header)](/glossary/content-length) [Core Web Vitals](/glossary/core-web-vitals) [Core Web Vitals report](/glossary/core-web-vitals-report) [CORS](/glossary/cors) [Critical CSS](/glossary/critical-css) [Critical rendering path](/glossary/critical-rendering-path) [Cumulative Layout Shift (CLS)](/glossary/cumulative-layout-shift) [dns-prefetch](/glossary/dns-prefetch) [Edge caching](/glossary/edge-caching) [fetchpriority](/glossary/fetchpriority) [First Contentful Paint](/glossary/first-contentful-paint)

Terms on this page
[URL Inspection tool](/glossary/url-inspection-tool) [Security headers](/glossary/security-headers)

Free · 2 minutes

## Know the term. _Now check your site._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **+212%** organic clicks in 90 days Ledgerly
