Skip to content
uencoder

CSP

CSP (Content Security Policy) declares, via an HTTP header, which resource origins a page may load, shrinking the attack surface for XSS and data injection.

Purpose

It defends against XSS and content injection by whitelisting the origins a page may load resources from.

Mechanism

The Content-Security-Policy header declares directives: default-src sets the default source, script-src the script sources, style-src the style sources, and so on. A nonce or hash permits inline scripts, and report-uri reports violations. Strict CSP uses a nonce plus strict-dynamic.

Use cases

  • XSS mitigation
  • Restricting third-party scripts
  • Inline script control
  • Violation reporting

Examples

  • Input
    script-src 'self' 'nonce-abc'
    Output
    Only same-origin and nonce-tagged scripts are allowed

    Whitelisting

Common errors

An overly strict CSP breaks page functionality
Observe first with Report-Only mode

Related terms

References