Use this when restricting what a page may load to limit XSS.
Content Security Policy
Content Security Policy (CSP) is a response header that restricts where scripts, styles, images and frames may load from, such as script-src 'self', which greatly limits the impact of XSS.
1. CSP restricts sources for scripts, styles, frames.
2. Strict CSP with nonces/hashes.
3. Avoid 'unsafe-inline'/'unsafe-eval'.
4. Start with Report-Only.
5. frame-ancestors for clickjacking.
response CSP → browser blocks disallowed sources
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
- CSP with
unsafe-inlineeverywhere.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- script-src 'self' blocks inline scripts.
- Use nonces or hashes when inline scripts are needed.
- Report-only mode lets you test safely.
curl -sI https://<site> | grep -i content-security-policy— CSP- CSP report-uri/report-to — violation reports
- Nonces/hashes instead of 'unsafe-inline' — strict CSP
ReferenceWhy it matters1 section · depth on demand
Rolling it out
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'
1. Start with Content-Security-Policy-Report-Only and a report endpoint.
2. Remove inline scripts or use nonces/hashes; avoid 'unsafe-inline'.
3. Enforce once reports are clean.
A strict CSP makes most XSS non-exploitable even when an injection bug exists.
ReferenceHow it works1 section · depth on demand
In depth
Content Security Policy (CSP) is a response header that tells the browser which sources of scripts, styles, images and other content are allowed. It greatly reduces the impact of XSS: injected script will not run if it does not match the policy.
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; object-src 'none';
base-uri 'self'; frame-ancestors 'none'; upgrade-insecure-requests
Documented example, not captured output.
Rollout method: start with Content-Security-Policy-Report-Only and a reporting endpoint, fix violations, then enforce. Avoid 'unsafe-inline' and 'unsafe-eval' for scripts; use nonces or hashes for necessary inline scripts. frame-ancestors replaces X-Frame-Options for clickjacking protection.
ReferenceImportant terms & boundaries1 section · depth on demand
Example policy
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-r4nd0m'; style-src 'self';
img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'none';
report-to csp-endpoint
Documented example, not captured output. Avoid 'unsafe-inline' and 'unsafe-eval' for scripts; use nonces or hashes. Content-Security-Policy-Report-Only lets you test without blocking.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Web Security → Content Security Policy. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://owasp.org/www-project-application-security-verification-standard/ · confidence 94%