Introduction
Websites often face risks from harmful scripts or content that can steal information or damage the site. Content Security Policy helps stop these risks by controlling what content a website can load and run.
Jump into concepts and practice - no test required
Imagine a nightclub that only lets in guests on a special list. The bouncer checks each person and stops anyone not approved from entering. This keeps the club safe from troublemakers.
┌─────────────────────────────┐
│ Website Server │
│ Sends CSP Policy to Browser │
└─────────────┬───────────────┘
│
│
┌─────────────▼───────────────┐
│ Browser │
│ Receives CSP Policy Header │
│ Checks Content Sources │
│ Blocks Unapproved Content │
└─────────────┬───────────────┘
│
▼
Safe Content DisplayedContent-Security-Policy.X-Content-Type-Options controls MIME sniffing, Strict-Transport-Security enforces HTTPS, and Cache-Control manages caching, none set CSP.Content-Security-Policy: script-src 'self' https://trusted.com;script-src 'self' https://trusted.com; means scripts can load from the same origin ('self') and from https://trusted.com.Content-Security-Policy: default-src 'none'; img-src https://images.com;default-src 'none' blocks all content sources unless specifically allowed.script-src directive.