Skip to content
BENIBIX Learn

Content Security Policy (CSP)

Development🔐 SecurityDefences

Also known as CSP, CSP Header, Content-Security-Policy

A header that tells the browser where scripts may load from.

Think of it like

Think of it like a guest list at the door. Scripts that are not on the list do not get in.

Example

Lucy's site sends script-src 'self'. A script injected from a stranger's server is blocked, and the browser logs a warning.

Why it matters

It limits the damage of cross-site scripting. One slip in your code no longer means a stranger's script runs.

Definition

A Content Security Policy is a list of rules a site sends to the browser in a header. It says where scripts, styles and pictures may load from. The browser blocks anything that is not on the list. So even if an attacker sneaks a script onto the page, it will not run. It is a second line of defence after output encoding, not a way to skip it.

Example code

http
Content-Security-Policy: default-src 'self'; script-src 'self'
Load everything from this site only. A script from anywhere else is refused.
The middle panel: the site's own script runs, and one from a stranger's server is blocked.

Common mistake

Adding 'unsafe-inline' to make an error go away. That lets inline scripts run again, and most of the guard is gone.

Key takeaways

  1. 1
    An allow list. Only listed places may load.
  2. 2
    The browser enforces it. It blocks everything else.
  3. 3
    A second layer. Use it with output encoding.