SecurityResponse Header

Content-Security-Policy (CSP)

Restricts where scripts, images, styles, and frames can be loaded from, protecting websites from Cross-Site Scripting (XSS) and data injection attacks.

Recommended Production Value
Essential (A+)
default-src 'self'; img-src 'self' data: https:; script-src 'self'; style-src 'self' 'unsafe-inline'; object-src 'none';

Production Server Implementation

NGINX Configuration Snippet
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none';" always;

Directives & Syntax Breakdown

default-src 'self'

Fallback source for all unspecified fetch directives. Restricts resources to origin.

script-src 'self'

Restricts executable JavaScript sources to same-origin. Blocks unauthorized external scripts.

object-src 'none'

Disables Flash, Java applets, and other outdated plugins that carry severe vulnerabilities.

frame-ancestors 'none'

Modern CSP replacement for X-Frame-Options DENY to prevent clickjacking.

base-uri 'self'

Restricts allowed URLs in the document <base> element to avoid base tag hijacking.

Common Security Hazards & Pitfalls

  • β€’Avoid using 'unsafe-eval' or 'unsafe-inline' without nonces or hashes as they neutralize XSS protection.
  • β€’Test thoroughly using 'Content-Security-Policy-Report-Only' before enforcing to avoid breaking third-party analytics.
Frequently Asked Questions

Content-Security-Policy (CSP) - Questions & Answers

Common implementation questions, browser enforcement rules, and debugging.

X-Frame-Options only prevents clickjacking by restricting iframe embedding. CSP includes "frame-ancestors" for clickjacking and additionally guards against XSS, script injection, and unsafe plugin execution.