Skip to content

CSP Concepts

Content Security Policy (CSP) is a security standard that helps prevent cross-site scripting (XSS), clickjacking, and other code injection attacks.

CSP works by specifying which resources browsers are allowed to load for a given page. You define a policy using HTTP headers, and browsers enforce it.

Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com

This policy says:

  • Load most resources only from the same origin ('self')
  • Load scripts from the same origin OR https://cdn.example.com

If a page tries to load a script from https://evil.com, the browser blocks it.

CSP has two modes:

Content-Security-Policy-Report-Only: default-src 'self'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID
  • Violations are reported but not blocked
  • Your site continues working normally
  • Use this to discover what your policy would break
Content-Security-Policy: default-src 'self'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID
  • Violations are blocked and reported
  • Resources that violate the policy won’t load
  • Use this after tuning your policy in report-only mode
DirectiveControlsExample
default-srcFallback for other directivesdefault-src 'self'
script-srcJavaScript sourcesscript-src 'self' https://cdn.com
style-srcCSS sourcesstyle-src 'self' 'unsafe-inline'
img-srcImage sourcesimg-src 'self' data: https:
font-srcFont sourcesfont-src 'self' https://fonts.com
connect-srcURLs for fetch, XHR, WebSocketconnect-src 'self' https://api.com
frame-srcSources for <iframe>frame-src 'none'
object-srcSources for <object>, <embed>object-src 'none'
base-uriURLs for <base> elementbase-uri 'self'
form-actionURLs for form submissionsform-action 'self'
frame-ancestorsWho can embed this page in a frameframe-ancestors 'none'
ValueMeaning
'self'Same origin as the document
'none'No sources allowed
'unsafe-inline'Allow inline scripts/styles (reduces security)
'unsafe-eval'Allow eval() and similar (reduces security)
https:Any HTTPS URL
data:Data URIs (e.g., data:image/png;base64,...)
blob:Blob URIs
https://example.comSpecific origin

For inline scripts without 'unsafe-inline', use nonces or hashes:

Content-Security-Policy: script-src 'nonce-abc123'
<script nonce="abc123">
// This script is allowed
</script>

Generate a new random nonce for each page load.

Content-Security-Policy: script-src 'sha256-abc123...'

The browser computes the hash of inline scripts and compares it to allowed hashes.

The report-uri directive tells browsers where to send violation reports:

Content-Security-Policy: default-src 'self'; report-uri https://ingest.headerhawk.com/csp/YOUR_SITE_ID

When a violation occurs, the browser sends a JSON report like:

{
"csp-report": {
"document-uri": "https://example.com/page",
"violated-directive": "script-src 'self'",
"blocked-uri": "https://evil.com/script.js",
"disposition": "enforce"
}
}

Header Hawk collects and analyzes these reports for you.