articles

CSP basics: allow only what you mean to run, not block everything

Published 2026-11-21 · 9 min read

CSP is not a way to block all scripts. It is an allowlist: you name what may run on the page, and everything else is stopped. This article covers how to read a policy, the relationship between unsafe-inline and nonces, Report-Only for a safe rollout, how V02 and V03 show it in vuln, and how to set it on Cloudflare Pages, Netlify, Vercel, or Next.js. It also includes what we measured on our own site on 21 November 2026.

Someone told you to add CSP, so you pasted the one line default-src 'self'. What actually changed? It is easy to end up with no idea.

CSP is not a way to block all scripts. It is an allowlist: you name what may run on the page, and everything else is stopped. Start from default-src 'self' and add only the sources you really use.

What CSP is

Picture the front desk at an event. There is a guest list, and only people on the list get in. Anyone else stays outside, even if they are standing at the door.

CSP (Content-Security-Policy) is one of the headers a server sends with its response. It tells the browser where this page may load scripts, styles, images, and frames from, and which inline code (written directly in the HTML) it may run.

Nothing about the page looks different. What changes is whether the browser runs or stops code you did not expect to be there.

Why it concerns you

A malicious script can be slipped into a form field on your page. This attack is called XSS (cross-site scripting).

If whatever people type is shown on the page as-is, the script is shown too, and it runs. Common examples are:

  • comment sections and reviews that put what people wrote straight onto the page
  • profile bios and display names shown on other people's screens
  • a search box that puts the words you typed into a heading such as "Results for …"
  • notes and to-do lists that save what you enter and list it back

For example, a script written into a comment can run in the browser of someone else who opens that page.

It is not the input field itself that is dangerous. If the text is treated as text (escaped) when it is displayed, the script does not run.

CSP is the last resort at that moment. Even a script that made it onto the page will not run if it is not on the list.

The more your site has forms, logins, payments, or contact pages, the more this matters. A purely static profile page has less to protect, but adding CSP still does no harm.

The basics of an allowlist

As with the guest list at the desk, only the sources you list load on the page.

Start with default-src 'self'. default-src is the fallback for every other directive, and 'self' means "only this site (the same origin)".

From there, add only what you actually use.

  • script-src: where scripts may load from
  • style-src: where stylesheets may load from
  • img-src: where images may load from
  • connect-src: where fetches (APIs, analytics) may go
  • font-src: where fonts may load from
  • frame-src: where iframes (another page embedded in this one) may load from

'none' means "load from nowhere". It is a good idea to add object-src 'none' and base-uri 'self' from the start. object is an old plugin entry point, and a base tag can rewrite where links on the page point.

Do not use * or http:. * means "from anywhere", which defeats the allowlist. http: allows sources that are not encrypted.

unsafe-inline and nonces

'unsafe-inline' allows scripts and styles written directly in the HTML. 'unsafe-eval' allows eval() and similar calls that run a string as code.

Both weaken CSP a great deal. MDN warns that they defeat much of the purpose of having a CSP.

The alternatives are nonces and hashes. A nonce is a one-time pass that changes with every response; only a <script nonce="..."> that matches the 'nonce-...' in the header runs. A hash is a fingerprint computed from the code itself.

When script-src contains a nonce, a hash, or 'strict-dynamic', the browser ignores 'unsafe-inline'.

vuln is a check that looks at the settings of a public URL from the outside (vuln). It does not call the policy weak when 'unsafe-inline' is the only problem. If 'unsafe-eval' is still there, however, the result is "Needs work".

'strict-dynamic' extends the trust given by a nonce or hash to scripts that the trusted script loads later. It also ignores host lists and 'self', so it takes some getting used to.

Do not enforce it straight away

Adding CSP can stop scripts that used to work. The more external scripts your site uses (analytics, chat, payments, ads), the more likely that is.

That is what Content-Security-Policy-Report-Only is for. It does not enforce anything; it only reports violations. Run it for about a week and see what would break.

To receive the reports, declare the destination in a Reporting-Endpoints header and name it in report-to. Also include report-uri for older browsers. A report tells you which directive was violated and which URL was not loaded.

But Report-Only does not enforce, so V02 in vuln stays at "Needs work". vuln looks for the enforcing header or a meta tag (a setting written inside the page). With a report-only policy, the internal record keeps note=report-only-only.

How it looks in vuln

CSP appears in two rows of the vuln result table.

  • V02 CSP present (area: Headers, priority 3)
  • V03 CSP weaknesses (area: Headers, priority 2)

V02 checks whether the top page has a CSP in its response headers or in the HTML meta tag. If neither is there, the result is "Needs work".

V03 looks inside the policy when one exists.

  • script-src (or default-src if there is no script-src) has both 'unsafe-inline' and 'unsafe-eval': Needs work
  • script-src has * or http:: Needs work
  • default-src has *: Needs work
  • only 'unsafe-inline' is present (no nonce, hash, or 'strict-dynamic'): Check
  • object-src or base-uri is missing: Check

frame-ancestors is handled by V04 (clickjacking), so V03 does not report it.

Here is an easy-to-miss point. If there is no CSP at all, V03 shows as a pass. V03 asks whether the policy is weak; the absence of a policy belongs to V02. If V02 says "Needs work", fix V02 first.

A measurement from our own site

On 21 November 2026 we checked this site's response headers with curl -sSI https://ismyappok.com.

Here are the main directives from the CSP header (the nonce value, some sources, and some directives are omitted).

default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; …; script-src 'self' 'nonce-…' https://static.cloudflareinsights.com …; connect-src 'self' https://static.cloudflareinsights.com …; upgrade-insecure-requests

Three things follow.

  • The CSP header is present, so V02 is a pass
  • script-src has neither 'unsafe-inline' nor 'unsafe-eval', and no * or http:, so V03 is a pass
  • object-src and base-uri are present, so there is no missing-directive finding

We do not use a meta tag. The nonce is generated per response, so opening the same URL again gives a different value.

How to fix it

The order is: list what you use, start small, test with Report-Only, then enforce.

  • List: the external scripts you use (analytics, ads, chat, payments, fonts)
  • Start small: default-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; script-src 'self'
  • Add: only the missing sources, directive by directive (each directive is one instruction such as script-src)
  • Replace: scripts written directly in the HTML with a nonce or a hash
  • Test: run Content-Security-Policy-Report-Only for a week
  • Enforce: if nothing breaks, rename the header to Content-Security-Policy

Where you set this depends on your hosting. The official steps are here.

On Netlify and Cloudflare Pages, you put a _headers file in the publish directory and list a path such as /* with the headers. On Vercel, you write them under headers in vercel.json. Next.js documents how to issue a nonce in middleware (code that runs before the response is sent).

If you want to add the header at Cloudflare's proxy instead, you can do it with a Transform Rule ("Modify response header"). Decide which side owns the header first, so the app and the proxy do not both add it.

If you already followed Security headers, explained, this is one more line in the same place.

How it looks in the result screen

"3. Result" in vuln shows a pass-rate summary and the checklist. The table has six columns: ID, Item, Priority, Result, Area, and Evidence and fix.

V02 and V03 both appear in the Headers area. The Evidence and fix column keeps what was observed as key=value lines. With no CSP you see request=GET … and header=content-security-policy. The column alone does not tell you whether the policy was report-only.

The "Needs work only" filter includes "Check" as well. Combined with the priority sort, it helps you fix V02 and then V03.

What CSP does not prevent

vuln is not a penetration test. It checks the public face of a site from the outside.

CSP stops a script that got onto the page from running. It does not fix the reason the script got there (input validation and escaping). It does not look at server-side holes, broken authentication, or vulnerable dependencies (holes in how the site is built that can be attacked) either.

"CSP means XSS cannot happen" is wrong. The right way to put it is that CSP makes the damage smaller when an XSS does happen.

Finding nothing does not mean the site is safe.

Wrapping up

CSP is not "block everything"; it is "only the people on the list get in". Aiming for perfect on day one breaks sites. Starting from default-src 'self' and adding sources while checking with Report-Only seems like the realistic path.

Start with V02 in vuln. If there is no CSP, V02 says "Needs work". If V02 passes, look at V03 for weaknesses inside the policy.

The fixes are collected on these pages.

When you have changed the settings, run vuln again. You can check display and links before launch with audit (no login) as well.

This article is based on checking our own site's settings. We did not scan anyone else's site for it. The numbers are recommended values and implementation thresholds; the only measurements are the ones with the date and method given in the text.

Check your own site

Enter a URL and see what is missing before launch. Free, and no login required.

Run the free check