Content Security Policy (CSP) Explained: How to Prevent XSS
A CSP header tells the browser which scripts, styles and resources it may load, which is what blunts XSS. Directives, nonces, and how to roll one out safely.
ProfitTool Team
Security & Dev
What CSP Does
Cross-Site Scripting (XSS) is one of the most common web vulnerabilities. An attacker injects malicious JavaScript into your page — through a comment field, URL parameter, or compromised dependency — and that script runs in the user's browser, potentially stealing session tokens or data.
A Content Security Policy tells the browser exactly which sources of content are trusted. Even if an attacker manages to inject a script tag, the browser will refuse to execute it if the source is not on the approved list.
The CSP Header
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; style-src 'self' 'unsafe-inline'; img-src * data:Key Directives
| Directive | Controls |
|---|---|
default-src | Fallback for unspecified directives |
script-src | JavaScript sources |
style-src | CSS sources |
img-src | Image sources |
connect-src | fetch(), XHR, WebSocket destinations |
font-src | Web font sources |
frame-src | What can be embedded in iframes |
frame-ancestors | Who can embed this page (replaces X-Frame-Options) |
form-action | Where forms can submit |
upgrade-insecure-requests | Auto-upgrade HTTP to HTTPS |
Values
| Value | Meaning |
|---|---|
'self' | Same origin only |
'none' | Nothing allowed |
'unsafe-inline' | Allow inline scripts/styles — weakens CSP significantly |
'unsafe-eval' | Allow eval() — avoid if possible |
'nonce-abc123' | Allow specific inline blocks tagged with nonce |
'sha256-hash' | Allow specific script by its hash |
https://cdn.example.com | Specific external origin |
Nonce-Based CSP (Recommended for SPAs)
Instead of unsafe-inline, generate a random nonce per request and include it in both the header and the script tag:
<!-- Server generates nonce per request -->
<script nonce="r4nd0m123">/* this script is allowed */</script>Content-Security-Policy: script-src 'nonce-r4nd0m123'Since the attacker cannot guess the nonce, injected scripts are blocked.
Report-Only Mode
Start with Content-Security-Policy-Report-Only to log violations without blocking anything:
Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-reportReview reports, fix legitimate sources, then switch to enforcement mode.
Check your CSP with our Security Headers Checker.
ProfitTool Team
Security & DevWritten and maintained by the ProfitTool team. Where an article references a ProfitTool utility, the behaviour described is the behaviour of the tool on this site — you can open it and check the output yourself.
More from the Knowledge Hub
What Is a CSP Nonce and How Does It Work?
A nonce-based Content Security Policy allows specific inline scripts without using unsafe-inline. This guide explains what a nonce is, how to implement it, and why it is more secure.
Privacy & SecurityHTTP Security Headers Explained: HSTS, CSP, X-Frame-Options and More
HTTP security headers are directives your web server sends to browsers to prevent common attacks like XSS, clickjacking and protocol downgrade. Here is what each one does.
Privacy & SecurityWhat Is Clickjacking and How Does X-Frame-Options Prevent It?
Clickjacking tricks users into clicking on hidden elements overlaid on a legitimate page. The X-Frame-Options header and CSP frame-ancestors directive prevent this attack.