# Content Security Policy \(CSP\)

Content Security Policy lets a web application restrict browser resource loading and execution through a declared policy.

- Type: framework
- Domain: Engineering & architecture
- Level: Advanced
- Last reviewed: 2026-10-19
- Original: [Soroush’s Product Wiki](<https://wiki.soroush.uk/topic/content-security-policy/>)

## What 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.

## Why 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.

## When 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.

## How it works

1. Inventory resources, inline execution, embedding, and connection requirements.
2. Design a restrictive policy using appropriate directives and script authorization.
3. Deploy report-only where useful and investigate expected and unexpected violations.
4. Test complete workflows, including errors, authentication, and third-party integrations.
5. Enforce the reviewed policy and monitor it as dependencies change.

## A 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.

## Where 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.

## Sources

1. [Content Security Policy \(CSP\)](<https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP>) — MDN Web Docs · Official documentation
2. [Content Security Policy Cheat Sheet](<https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html>) — OWASP · Security guidance

## Related topics

- [Cross-Site Scripting \(XSS\)](<https://wiki.soroush.uk/topic/cross-site-scripting/>)
- [Same-Origin Policy](<https://wiki.soroush.uk/topic/same-origin-policy/>)
- [Threat Modeling](<https://wiki.soroush.uk/topic/threat-modeling/>)
