Content Security Policy (CSP)
Content Security Policy (CSP) is a browser-enforced mechanism to restrict allowed resource sources. CSP helps reduce cross-site scripting and other injection attacks by letting developers declare permitted sources for scripts, styles, images and frames.
Classification
- ComplexityMedium
- Impact areaTechnical
- Decision typeArchitectural
- Organizational maturityIntermediate
Technical context
Principles & goals
Use cases & scenarios
Compromises
- Overly restrictive rules cause outages
- Insufficient report processing prevents insights
- False assumptions about third-party security
- Start in report-only mode and analyze real violations.
- Use nonces or hashes instead of broad 'unsafe-inline' rules.
- Integrate reports into existing monitoring tools.
I/O & resources
- Inventory of all included scripts, styles, images and frames
- List of authorized domains and CDNs
- Deployment mechanism for HTTP headers or meta tags
- CSP header/meta tag configuration
- Aggregated reports of policy violations
- Updated policies and documentation
Description
Content Security Policy (CSP) is a browser-enforced security standard that lets web applications declare allowed sources for content such as scripts, styles, images and frames. It reduces cross-site scripting and injection risks by restricting resource loading and execution. CSP requires careful policy design, reporting, and incremental rollout to avoid breakage.
✔Benefits
- Reduces XSS and script injection risks
- Enables granular protection against unsafe third-party resources
- Provides reporting for monitoring and forensic analysis
✖Limitations
- Not all browsers fully support every CSP level
- Incorrect policies can break site functionality
- Does not protect against already compromised allowed resources
Trade-offs
Metrics
- Number of CSP violations
Number of policy violations reported per period; indicator for misconfiguration or attack attempts.
- Error rate after policy rollout
Share of user sessions functionally impacted by CSP violations.
- Time to remediate a CSP violation
Average time from receiving a report to implementing remediation.
Examples & implementations
E‑commerce platform reduces XSS incidents
An online shop deployed CSP in report-only mode, collected violations and enabled a restrictive policy after iteration. Reported XSS incidents dropped significantly.
SPA with CDN integration
A SPA explicitly allowed only certain CDNs and used nonces for dynamic scripts, preventing unexpected third-party executions.
Continuous monitoring via CSP reports
A team integrated CSP reports into their SIEM and detected faulty library updates and unusual request patterns early.
Implementation steps
Perform inventory of resources and third parties.
Configure report-only CSP and collect reports.
Iteratively adjust policy and run staging tests.
Enable enforcement and establish monitoring.
Plan regular reviews and policy updates.
⚠️ Technical debt & bottlenecks
Technical debt
- Legacy code with inline scripts that does not support nonces.
- Missing automated tests for policy changes.
- No central infrastructure to process CSP reports.
Known bottlenecks
Misuse examples
- Setting 'unsafe-inline' across all pages to quickly satisfy functionality.
- Using overly broad wildcards ('*') for critical directives.
- Deploying an enforcement policy without prior staging tests.
Typical traps
- Forgetting to process and monitor CSP reports.
- Assuming CSP solves all web security problems.
- Not considering subresource integrations (workers, frames).
Required skills
Architectural drivers
Constraints
- • Browser compatibility of different CSP levels
- • Legacy code that does not support nonces or hashes
- • Business-required third-party integrations