What CSP does
Content Security Policy (CSP) is a browser-enforced defense-in-depth control. It can restrict where scripts, styles, images, frames and other resources may load from. It does not fix cross-site scripting vulnerabilities; output encoding and safe DOM handling remain essential.
Start with report-only mode
Begin with Content-Security-Policy-Report-Only to observe violations without blocking resources. Inventory scripts, styles, analytics, embeds and payment integrations. Avoid immediately adding broad wildcards simply to silence reports.
Prefer nonces or hashes
A nonce-based policy generates a fresh unpredictable value per response and applies it to approved inline script elements. The same nonce must be used in the header and matching HTML. Hashes can be suitable for stable inline scripts. Avoid unsafe-inline where possible.
Roll out gradually
Move from report-only to enforcement after reviewing reports and testing key journeys. Use a restrictive default-src baseline and explicitly add required sources. Keep frame ancestors, base URI and form action directives in mind for clickjacking and injection defenses.
Keep policy maintainable
Generate the header centrally. Avoid having multiple layers emit conflicting CSP headers. Test cached pages carefully because a cached HTML nonce and a newly generated header must not become mismatched.
Checklist
- Begin with report-only observation.
- Inventory all required resource origins.
- Prefer nonces or hashes over unsafe-inline.
- Define frame-ancestors and base-uri.
- Test login, admin, forms and third-party integrations.
- Review reports and remove unused origins.
- Continue using output encoding and secure coding practices.