Content Security Policy (CSP)
Content Security Policy lets a web application restrict browser resource loading and execution through a declared policy.
Download Markdown (.md)Soroush’s Product Wiki · https://wiki.soroush.uk/topic/content-security-policy/
01What it means
Content Security Policy, or CSP, is a browser-enforced policy typically delivered in an HTTP response header. Its directives constrain resource and execution behaviors, including scripts, styles, connections, and framing. Nonces and hashes can authorize particular scripts. A report-only policy observes violations without blocking them; an enforced policy applies the restrictions.[1][2]
02Why it matters
CSP can reduce the impact of some injection attacks and make unexpected resource use visible. It also creates an explicit agreement about which scripts and external services a page is allowed to depend on.
03When to use it
Use it as part of layered web security, particularly for applications handling untrusted content or third-party resources. Retain context-appropriate output handling and secure coding. Plan for framework behavior, analytics, embedded content, and a staged rollout rather than copying a broad policy without testing.
04How it works
- Inventory resources, inline execution, embedding, and connection requirements.
- Design a restrictive policy using appropriate directives and script authorization.
- Deploy report-only where useful and investigate expected and unexpected violations.
- Test complete workflows, including errors, authentication, and third-party integrations.
- Enforce the reviewed policy and monitor it as dependencies change.
05A practical example
A support portal introduces CSP in report-only mode and finds an old widget injecting inline scripts. The team replaces that behavior, then enforces a nonce-based script policy while retaining its existing output-encoding protections.
06Where it breaks
CSP cannot fix every injection vulnerability or make authorized malicious code harmless. Broad allowances weaken it, and incorrect policies can break essential features. Nonces require correct per-response generation and handling; report delivery is not a complete security monitor.
Common mistakes
- Treating report-only mode as active blocking.
- Adding unsafe allowances solely to silence violations.
- Using CSP as a replacement for fixing unsafe output handling.
07Sources
- Content Security Policy (CSP)MDN Web Docs · Official documentationContent Security Policy (CSP)
- Content Security Policy Cheat SheetOWASP · Security guidanceContent Security Policy Cheat Sheet