Security

CSP (Content Security Policy)

How a Content Security Policy header tells the browser which scripts and resources a page may load, and how it stops injected XSS scripts from running.

  • 3 min read
  • 3 questions
  • +25 XP
Transcript

CSP, explained by cats. Kitty opens her new online cafe. Other cats visit, browse the menu, and leave reviews. Everything's going great until one day something strange happens. A customer's review secretly includes a line of code. Now every visitor sees fake offers and suspicious links.

Kitty's cafe has been hijacked. It's executing scripts from different sources. That's a cross -site scripting attack, also known as XSS, bad code sneaking into good pages. So Kitty adds a content security policy header, a list of strict rules.

It tells browsers exactly what's allowed inside her site. Scripts, styles, and images can only come from trusted sources. No outside ingredients, no mystery code. When a malicious script tries to run, CSP blocks it before it reaches the customers. That's content security policy, a rule book that keeps Kitty's cafe clean, her customers safe, and her code trustworthy.