Content Security Policy for React Apps
Configure a strict CSP header that prevents inline scripts and unauthorized external resources in React applications.
Content Security Policy for React Apps is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Content Security Policy
Content Security Policy (CSP) is an HTTP response header that tells the browser which content sources are trusted. By declaring exactly which scripts, styles, images, and fonts are allowed to load, CSP prevents injected malicious content from executing — even if an XSS vulnerability allows the attacker to inject HTML into the page.
default-src and script-src
default-src 'self' sets the baseline: all resource types (scripts, styles, images, fonts, frames) may only load from the same origin. script-src 'self' narrows script loading to the same origin, blocking scripts from external CDNs unless explicitly listed. These two directives together form the foundation of a restrictive CSP.
Why Blocking eval Matters
eval() converts a string into executable code at runtime — which is exactly what XSS payloads need. A CSP with script-src 'self' (without 'unsafe-eval') blocks eval() and related functions like new Function() and setTimeout(string). This eliminates a major class of XSS payload execution.
strict-dynamic for Script Chains
'strict-dynamic' in script-src grants trust to scripts loaded by already-trusted scripts. This enables dynamic script loading patterns used by bundlers without requiring you to whitelist every CDN URL. A trusted script can load additional scripts, and those inherit trust.
Hash-Based CSP for Inline Scripts
If your application needs a specific inline script (like an analytics initialization snippet), you can compute its SHA-256 hash and add 'sha256-base64hash' to script-src. Only scripts matching that exact hash are allowed to execute inline. Any attacker-injected script will have a different hash and be blocked.
Nonce-Based CSP
A nonce is a cryptographically random value generated by the server for each request. The server adds the nonce to both the script tag (<script nonce="abc123">) and the CSP header (script-src 'nonce-abc123'). Only scripts bearing the correct nonce execute. Attackers cannot predict the nonce, so injected scripts cannot use it.
CSP Reporting
CSP supports violation reporting via report-uri /csp-violation-endpoint or the newer report-to directive. When the browser blocks a resource due to CSP, it sends a JSON report to the endpoint. Monitoring these reports helps you identify both legitimate configuration issues and active attack attempts.
Implementing CSP in Next.js
In Next.js, add CSP headers in middleware: create middleware.ts, generate a nonce, set the Content-Security-Policy header, and pass the nonce to the page via a response header. The page layout reads the nonce from the header and applies it to all inline scripts and style tags.
Testing CSP Without Blocking
Content-Security-Policy-Report-Only is the testing mode: the browser enforces nothing but sends violation reports for everything that would have been blocked. Deploy in Report-Only mode first, fix all violations, then switch to the enforcing Content-Security-Policy header to avoid breaking the application.
Chrome DevTools Console Violations
When CSP blocks a resource, Chrome DevTools console shows a red error message identifying the blocked URL and the violated directive. These messages are invaluable during development and testing. Each violation must be resolved either by fixing the policy, moving the resource to an allowed source, or using a nonce/hash.
Practical CSP for React SPA
A practical React SPA CSP: default-src 'self'; script-src 'self' 'strict-dynamic' 'nonce-NONCE'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://api.yourdomain.com; frame-ancestors 'none'. The frame-ancestors 'none' also prevents clickjacking by blocking your app from being embedded in iframes.
CSP script-src self
What does script-src 'self' in a Content Security Policy prevent?
Lesson Recap
CSP is an HTTP header declaring trusted content sources. default-src 'self' and script-src 'self' block external scripts and eval. Inline scripts are allowed via hashes or nonces. 'strict-dynamic' supports dynamic script loading. Use Content-Security-Policy-Report-Only to test before enforcing. Next.js implements CSP via middleware. DevTools console shows violations in real time.
Frequently asked questions
Is the “Content Security Policy for React Apps” lesson free?
Yes — the full text of “Content Security Policy for React Apps” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.
What will I learn in “Content Security Policy for React Apps”?
Configure a strict CSP header that prevents inline scripts and unauthorized external resources in React applications. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start React Academy?
No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Content Security Policy for React Apps” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this React Academy lesson?
Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- XSS in React: dangerouslySetInnerHTML and Third-Party Scripts
- CSRF Protection in React and API Setups
- Content Security Policy for React Apps
- Secrets Management and Environment Variables