# Content Security Policy (CSP)

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

**Field:** Software Development  
**Category:** Security  
**Also known as:** CSP, CSP Header, Content-Security-Policy

## 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.

## 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

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

## 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.

## Why it matters

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

## Common mistake

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

## Three principles

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

## Related terms

- [Cross-Site Scripting (XSS)](https://learn.benibix.com/term/development/cross-site-scripting)
- [Output Encoding](https://learn.benibix.com/term/development/output-encoding)
- [Same-Origin Policy (SOP)](https://learn.benibix.com/term/development/same-origin-policy)
- [HTTP Header](https://learn.benibix.com/term/infrastructure/http-header)
- [Cross-Origin Resource Sharing (CORS)](https://learn.benibix.com/term/development/cors)

![Three panels. Same-origin policy: a page on bad.example asks to read a reply from bank.example and the answer is no, another origin, where an origin is the scheme, host and port. Content Security Policy: a header says script-src 'self', so /app.js from the site runs and a script from evil.example is blocked. Output encoding: a user types a script tag, the page writes it with &lt; and &gt; codes, and it is shown as text, never run.](https://learn.benibix.com/diagrams/dev2-browser-guards.svg)

---

Source: https://learn.benibix.com/term/development/content-security-policy
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
