Skip to main content
Sealgap

What is a Content Security Policy?

A Content Security Policy (CSP) is a set of instructions a website sends to the browser that restricts what the code on a page is allowed to load and run. Its main purpose is to limit the damage when an attacker manages to inject their own script into a page, an attack known as cross-site scripting or XSS.

How a Content Security Policy works

The policy is sent as a response header named Content-Security-Policy. It consists of directives separated by semicolons, and each directive names a type of content and the sources it may come from. The directive default-src sets the general rule, script-src sets the rule for JavaScript, and frame-ancestors decides which sites, if any, may show the page inside a frame. A policy that says scripts may only come from the site's own address tells the browser to refuse a script from anywhere else, including one an attacker has slipped into the page.

A strict policy also blocks scripts written directly into the page, since injected code often takes that form. A site that needs its own inline scripts can mark them with a nonce, a random value the server generates for each response, or with a hash of the script's contents. The shortcut is the keyword unsafe-inline, which allows all inline scripts again. The MDN guide to Content Security Policy advises against it, because it defeats much of the purpose of having a policy.

Present is not the same as strict

A policy can exist and still permit nearly everything. A header that allows scripts from any address, or that includes unsafe-inline, satisfies a check that only looks for the presence of the header, yet it stops very little.

Introducing a policy without breaking the app

A strict policy can break a working app that loads scripts, fonts and data from several places. Browsers offer a trial mode for this. A policy sent in the Content-Security-Policy-Report-Only header is not enforced, but each violation is reported, so you can see what the policy would block before you turn it on. Start there, adjust the list of sources until the reports stop, and then switch to the enforcing header.

What a policy does not do

A Content Security Policy is a second line of defense. It does not remove the flaw that let a script be injected, and MDN is explicit that it is no alternative to sanitizing input.

CSP in a client app

For apps generated with AI app builders, the policy is set in the hosting configuration and not in the generated code, so it is easy to leave out at launch. It is one of the standard security headers, and the one that takes the most work. In a client report, describe it in one sentence: the browser has a list of the places this app may load code from, and refuses the rest.

Also known as

CSP

Free check

See what one of your apps answers.

Certificate, browser protections and keys in public code. No account needed.

See what one of your own apps answers.

The free check looks at an app from the outside: whether it answers, its certificate, its browser protections and keys in its public code.