Content Security Policy explained: control what your pages can load
A Content Security Policy (CSP) is a security header your website sends that tells the browser exactly which sources it may load scripts, styles, images and other content from. Anything not on that approved list is blocked, which is what stops injected or cross-site code from running.
If you are asking what is a Content Security Policy in plain terms, think of it as a guest list for your web page. The browser already trusts your site, so if an attacker manages to slip a malicious script onto a page, the CSP is the bouncer that refuses to run it because it did not come from an approved source.
This guide explains how a CSP blocks cross-site scripting (XSS) and injection, shows a simple example policy you can read line by line, and notes exactly what our free scanner can and cannot tell you about the CSP your site sends.
What a Content Security Policy actually does
A CSP is delivered in a single HTTP response header called Content-Security-Policy. It is made up of directives, each naming a type of content and the sources the browser may load that content from. When the browser receives a page, it reads the policy first and then refuses to load or execute anything that falls outside it.
The headline benefit is defence against cross-site scripting and content injection. If a comment box, a URL parameter or a compromised third party tries to inject a script tag, a well-written CSP simply will not let the browser run it, because the script's source is not on the approved list.
- script-src: where JavaScript may be loaded from
- style-src: where stylesheets may come from
- img-src: approved sources for images
- connect-src: where the page may send requests (fetch, XHR, websockets)
- default-src: the fallback used when a more specific directive is not set
- frame-ancestors: who may embed your page in a frame (clickjacking control)
How a CSP blocks cross-site scripting and injection
Cross-site scripting works by getting the visitor's browser to run attacker-controlled JavaScript while it trusts your domain. That script can then read cookies, capture form input or quietly act as the logged-in user. A CSP breaks this chain at the last step: even if the script reaches the page, the browser checks it against the policy and blocks anything from an unapproved source.
The strongest part of a good policy is how it treats inline scripts. By default a strict CSP refuses inline script blocks and inline event handlers, which are exactly the form most injected payloads take. Modern policies let your own legitimate inline scripts through with a nonce (a one-time token) or a hash, so trusted code still runs while injected code is refused.
A simple example policy, read line by line
Here is a short, readable policy. It is deliberately strict, allowing content only from your own domain plus one named analytics provider:
Content-Security-Policy: default-src 'self'; script-src 'self' https://analytics.example.com; style-src 'self'; img-src 'self' data:; frame-ancestors 'none'
In plain English: load everything from the site's own origin by default; allow scripts only from the site and that one analytics domain; allow styles and images only from the site (plus inline image data); and let no other site put this page in a frame. The keyword 'self' means the page's own origin, and you widen the policy by listing extra trusted domains, not by using a wildcard.
- Start in report-only mode (Content-Security-Policy-Report-Only) so you can see what would break before you enforce it
- Avoid 'unsafe-inline' and 'unsafe-eval'; they switch off much of the protection
- Prefer a nonce or hash to allow your own inline scripts
- Tighten gradually: a policy you had to weaken to make the site work is doing little
Free, instant, no account
See your site's security grade in seconds.
Free, instant, and no account needed to see your grade.
Scan my site freePlain-English A to E grade in about 10 seconds. No account needed to see it.
Why a CSP is one layer, not the whole job
A Content Security Policy is a strong mitigation, but it is not a cure for insecure code. It reduces the damage when an injection slips through; it does not fix the underlying flaw that allowed user input to reach the page unescaped. Treat a CSP as a safety net that sits alongside proper input handling and output encoding, not as a replacement for them.
A CSP also needs maintaining. Every time you add a new script, font host, payment widget or embed, the policy has to allow it, or that feature silently breaks. Many sites end up loosening their policy so far that it stops protecting anything, so it is worth reviewing whenever the site changes.
How EB Security checks your Content Security Policy
Our free scanner performs an external, passive read of the headers your site already serves in public. For CSP, it checks whether a Content-Security-Policy header is present on the homepage and reports it as a security-header finding, with a plain-English explanation and the fix if it is missing.
Be clear on the limit: the scan tells you whether the header exists, not whether your particular policy is strict enough to stop every injection. A header that leans on 'unsafe-inline', for example, still counts as present. A missing CSP nudges your grade down but will not, on its own, sink it, because missing hardening headers are scored as improvements rather than critical faults. The scanner does not test your JavaScript, your plugins or your application logic, so use the result as a fast first signal, then have the policy itself reviewed before you rely on it.
A Content Security Policy is a header that lists which sources a browser may load content from, blocking cross-site scripting and injection by refusing anything off the list. Start strict, allow your own code with a nonce or hash, and treat it as one layer alongside good coding, not a replacement for it.