SECURITY & PRIVACYAI-REVIEWED

What does Content Security Policy protect?

Useful first: HTTP · optional, not a requirement

Choose your depth

Stay with the essentials, or look closer.

Simple explanation
2 min read · estimate

YOUR GOALRecognize what CSP restricts and what it cannot promise.

Rules the browser can enforce

A policy travels with the page

A server can send a Content-Security-Policy header in an HTTP response. The browser then applies its restrictions to the page. These rules can limit images, scripts and other resources. A policy can block a resource even when the network and server are working normally.

[1] [2]

An unwanted script is still a request

Imagine a recipe page whose enforcing policy permits external scripts only from its own origin. A script requested from an unrelated advertising origin fails that rule. But an allowed script is not certified harmless: the policy checks permission to load or execute, not whether every action in the program is safe.

[2] [5]

One layer of protection

CSP can reduce the damage from cross-site scripting, where unwanted code enters a page. It does not remove the underlying injection bug. Developers still need safe input handling and output encoding. HTTPS protects the connection; merely using HTTPS does not choose a CSP for the page.

[1] [7]
THE IDEA TO KEEP

CSP gives the browser restrictions, not a certificate that a website is safe.

ONE THING TO WATCH OUT FOR

A page with CSP is automatically safe from every injected script.

The actual policy and application code determine protection; CSP is an additional defense.

A QUICK CHECK, NOT A TEST
A recipe page blocks a script under its enforcing CSP. What does that show?
Show the explanation

The script violates a restriction in that policy. A policy violation can block a request on a working connection. Permission under CSP is not a safety review of the code.

Progress is saved only in this browser.

KEEP EXPLORINGSame-origin policy
Was this explanation clear?
Report a mistake by email
Basic explanation
2 min read · estimate

YOUR GOALInterpret fetch fallbacks, report-only behavior and framing restrictions.

Read the rule that applies

A fallback does not merge permissions

Content-Security-Policy: default-src 'self'; script-src https://scripts.example

In this deliberately small example, script-src supplies the script rule; it does not inherit 'self' from default-src. Images have no explicit rule here, so they use the default. 'self' means the page's own origin, not every subdomain. This header is a teaching example, not a complete deployment policy.

[2] [4]

Observe before enforcing

Content-Security-Policy-Report-Only checks a policy without imposing its blocks. Developers can investigate violations before enabling enforcement. Remote reports need a configured destination, such as Reporting-Endpoints together with report-to; reporting support varies. If an enforcing policy is also present, its restrictions still apply. Report-only does not turn that policy off.

[3] [7]

Embedding has its own control

default-src 'none' does not prevent another page from framing this one. frame-ancestors controls which ancestors may embed the page; frame-ancestors 'none' disallows embedding. It differs from frame-src, which governs frames loaded by this page. Deliver frame-ancestors in the HTTP header: it is not supported in a meta element.

[4] [6]
THE IDEA TO KEEP

Identify the resource type, the applicable directive and whether the policy is enforced.

ONE THING TO WATCH OUT FOR

default-src is a master switch that covers every CSP directive.

It supplies fetch fallbacks; directives such as frame-ancestors need their own setting.

A QUICK CHECK, NOT A TEST
A page sends only a report-only CSP, with reporting configured. A script violates that policy and no other restriction blocks it. What happens?
Show the explanation

That policy can report the violation without blocking the script. Report-only is for observing violations. It does not impose the candidate policy, although a separate enforcing policy would still be enforced.

Progress is saved only in this browser.

KEEP EXPLORINGSame-origin policy
Was this explanation clear?
Report a mistake by email
Advanced explanation
2 min read · estimate

YOUR GOALDistinguish nonce and hash authorization and reason about overlapping policies.

Authorize specific scripts carefully

A nonce belongs to one response

A nonce is an unpredictable value generated with cryptographically secure randomness for each response. The server places the same value in a nonce source in the policy and in the nonce attribute of each intended script element. Reusing a fixed value defeats its purpose. Attach it only to trusted script markup, not indiscriminately to every script found in generated HTML.

[1] [5]

A hash names exact inline content

For an inline script block, a CSP hash covers the script text without its enclosing tags. Changing whitespace can change the hash. If a build reformats a small startup script but retains its old policy hash, that hash no longer authorizes it. A matching script-block hash does not automatically authorize an inline onclick handler. These checks occur as the browser processes the document and its scripts.

[5] [7]

Two policies are two checks

Suppose one enforcing header contains connect-src 'none', while another allows a particular API origin. The first still blocks that connection: adding a second policy does not relax the first. Likewise, a broad report-only experiment cannot override enforcement. When diagnosing a blocked feature, inspect all delivered policies before changing the application or loosening its rules.

[2] [7]
THE IDEA TO KEEP

Keep script authorization precise and evaluate every policy that the browser receives.

ONE THING TO WATCH OUT FOR

A more permissive second CSP header overrides an earlier restrictive one.

Each enforcing policy applies; a request must satisfy all of them.

A QUICK CHECK, NOT A TEST
A build changes whitespace inside an inline script. Its only authorization is the old CSP hash. What should the developer check?
Show the explanation

Whether the hash matches the exact new script text. CSP hashing uses the script text, not its meaning. Reformatting may require a new hash; equivalent behavior does not preserve the digest.

Progress is saved only in this browser.

KEEP EXPLORINGSame-origin policy
Was this explanation clear?
Report a mistake by email
GO STRAIGHT TO THE SOURCE

Keep asking. Check the details.

Source-checked using AI (OpenAI Codex) on . The checks covered sources, examples, and quizzes for this article version.

No human review is claimed. AI review can miss errors; check the original sources below.

  1. MDN — Content Security Policy guide
  2. MDN — Content-Security-Policy header
  3. MDN — Content-Security-Policy-Report-Only header
  4. MDN — default-src directive
  5. MDN — script-src directive
  6. MDN — frame-ancestors directive
  7. W3C — Content Security Policy Level 3, Working Draft
How explanations are prepared