Constrain browser script execution with CSP
Quick answer
Content Security Policy (CSP) is a browser-enforced policy that restricts selected resource loads and execution. A response header is the preferred deployment mechanism. CSP is a defense-in-depth layer, not a replacement for safe rendering.
How it works
A script-src directive controls script sources and inline execution. For a strict deployment, use a reviewed nonce- or hash-based policy; each server response using a nonce needs a fresh unpredictable value inserted only into trusted scripts. Report-Only mode reports violations without enforcing the policy and requires a response header. A CSP meta tag has limited support: it must precede the resources it controls, and cannot express every directive or report-only mode. The examples apply a restrictive meta policy to a fresh document and insert only harmless synthetic script text.
Local examples
Run in a fresh browser document. Actual ByteMind editor support is unverified.
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="script-src 'none'";document.head.append(m);const script=document.createElement("script");script.textContent="document.body.dataset.demo='ran'";document.head.append(script);console.log(document.body.dataset.demo===undefined);})()Expected console output:
trueThe inserted script does not run under this local policy.
Run in a fresh browser document. Actual ByteMind editor support is unverified.
(async () => {const script=document.createElement("script");script.nonce="fixture-only";console.log(script.nonce,script.nonce==="other-response");})()Expected console output:
fixture-only falseThe nonce property exposes the assigned value; a different response value would not match. The literal is not suitable for deployment.
Common mistakes
Do not copy a fixed nonce into production, or enable unsafe-inline merely to silence a policy error. Test the real response policy and its required resources.
Practice
In a dedicated local fixture, verify a script is blocked under script-src none; then design a reviewed header policy for the application rather than weakening the rule.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Constrain browser script execution with CSP — Knowledge Check
Apply the concept and reason about local example output.
What is the preferred production delivery mechanism for CSP?
What is required of a deployment nonce?
What does Content-Security-Policy-Report-Only do?
What is the exact console output in Constrain browser script execution with CSP example 1?
(async () => {const m=document.createElement("meta");m.httpEquiv="Content-Security-Policy";m.content="script-src 'none'";document.head.append(m);const script=document.createElement("script");script.textContent="document.body.dataset.demo='ran'";document.head.append(script);console.log(document.body.dataset.demo===undefined);})()What is the exact console output in Constrain browser script execution with CSP example 2?
(async () => {const script=document.createElement("script");script.nonce="fixture-only";console.log(script.nonce,script.nonce==="other-response");})()