Explore our new Cybersecurity Terms Glossary, explained by our experts.
Cybersecurity Terms Glossary
Content Security Policy is an HTTP response header that tells the browser which scripts, styles, and other resources a page may load, limiting the damage an injected script can do.

What Is Content Security Policy?
Content Security Policy (CSP) is an HTTP response header through which a site declares where the browser may load scripts, styles, images, frames, and connections from. If a payload is injected into the page, the browser still refuses to execute it unless it satisfies the policy. CSP does not prevent injection; it is a containment control that turns a working cross-site scripting bug into a blocked console message.
A policy is a list of directives, each naming a resource type and the sources allowed for it. A minimal example:
Content-Security-Policy: default-src 'self'; script-src 'nonce-r4nd0m' 'strict-dynamic'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'
The directives that carry the most weight are script-src, which governs executable code; object-src, which should almost always be 'none' because plugins bypass other restrictions; base-uri, which stops an injected <base> tag redirecting every relative script URL; and frame-ancestors, which replaces the older X-Frame-Options header and defends against clickjacking.
The first generation of policies listed trusted hostnames. In 2016 a Google study that examined over a billion hostnames found that 94.72% of the policies it collected could be bypassed, usually because an allowed CDN also hosted a JSONP endpoint or an old copy of AngularJS that could be coaxed into running attacker-supplied code. Listing a large content delivery network is close to listing the whole internet.
The modern answer is a nonce-based policy. The server generates an unpredictable value for every response, places it on each legitimate <script> tag, and names it in the header. Injected markup cannot guess the value, so it never runs. Adding 'strict-dynamic' lets a trusted script load its own dependencies without the policy having to enumerate them, which is what makes the approach practical for applications that use tag managers or module loaders.
Content-Security-Policy-Report-Only is evaluated and reported but never enforced, so you can measure the impact on real traffic.report-to and a reporting endpoint, and expect noise: browser extensions and injected ISP content generate reports you did not cause.onclick attributes and javascript: URLs cannot be covered by a nonce, so they have to move into script files before the policy can tighten.'unsafe-inline' and 'unsafe-eval' last, and treat their removal as the point at which the policy starts providing value.Avoid the common shortcut of generating one static nonce at build time. A nonce that does not change per response is simply a password the attacker can read from the page source.
Payment pages are the clearest case. In the British Airways compromise disclosed on 6 September 2018, a modified script on the booking pages skimmed the card details of hundreds of thousands of customers; the ICO issued a £20 million penalty on 16 October 2020. A policy restricting script-src and connect-src would have blocked the exfiltration call even after the script was altered. PCI DSS v4.0 requirements 6.4.3 and 11.6.1 now require payment page scripts to be authorised and monitored for change, and CSP together with Subresource Integrity is the usual way of meeting them.
Policies are also used to force secure transport with upgrade-insecure-requests, to limit where forms may submit with form-action, and to isolate a document from cross-origin windows alongside the Cross-Origin-Opener-Policy header. CSP sits alongside the other HTTP security headers rather than replacing them. If you want the policy reviewed against your actual page behaviour, our web application security testing includes header and bypass analysis.
No. CSP limits what an injected script can do; it does not stop the injection. Policies can be bypassed through allowed origins, DOM clobbering, or dangling markup, and a browser that does not support a directive ignores it. Treat the policy as a second line of defence behind context-aware output encoding and framework auto-escaping, not as a substitute for them.
Use a nonce when the server renders the page and can emit a fresh random value per response. Use hashes when the script content is fixed and the page is served statically from a cache or CDN, where a per-response value is impossible. Hashes break whenever the script text changes by a single byte, so they suit small, stable inline blocks best.
Most unexplained reports come from the client side rather than your code: browser extensions injecting scripts, antivirus products rewriting pages, in-app browsers, and occasionally content injected on public networks. Filter by document URL and blocked URI before investigating, and expect a baseline of noise that never fully disappears on a high-traffic site.
It can, using a meta element with http-equiv set to Content-Security-Policy, which is useful when you cannot change server configuration. The limitations matter though: frame-ancestors, sandbox, and report-uri are ignored in meta form, and any markup before the tag is already parsed. A response header is the reliable delivery mechanism.
You May Be Interested In These
We use a few cookies that are needed to make this website work, and they are always on. We would also like to set analytics cookies, to see which pages are useful; functional cookies, so that our live chat works; and advertising cookies, to measure our campaigns. We will not set any of those until you say yes, and you can change your mind at any time from the Cookie Settings link in the footer.
Choose the cookies you are happy for us to use. Strictly necessary cookies are always on; everything else is off until you turn it on.
These cookies keep the website working: they hold your security checks, your form submissions and the choice you make here. The site cannot run without them, so they are always on.
These let our live chat open and remember your conversation, and let the newsletter sign-up work. Switch them off and the chat window will not appear.
These tell us which pages are read and how people find us, so that we can improve the site. The reports are about groups of visitors, never about you personally.
These let us measure our advertising and show you our adverts on other websites, such as LinkedIn, Google and Meta. Switch them off and you will still see adverts, but they will be less relevant.