What does Content Security Policy protect?
Useful first: HTTP · optional, not a requirement
Choose your depth
Stay with the essentials, or look closer.
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.
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]CSP gives the browser restrictions, not a certificate that a website is safe.
A page with CSP is automatically safe from every injected script.
The actual policy and application code determine protection; CSP is an additional defense.
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.
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.
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.
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.
Identify the resource type, the applicable directive and whether the policy is enforced.
default-src is a master switch that covers every CSP directive.
It supplies fetch fallbacks; directives such as frame-ancestors need their own setting.
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.
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.
Keep script authorization precise and evaluate every policy that the browser receives.
A more permissive second CSP header overrides an earlier restrictive one.
Each enforcing policy applies; a request must satisfy all of them.
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.
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.
- MDN — Content Security Policy guide
- MDN — Content-Security-Policy header
- MDN — Content-Security-Policy-Report-Only header
- MDN — default-src directive
- MDN — script-src directive
- MDN — frame-ancestors directive
- W3C — Content Security Policy Level 3, Working Draft