Security

Content Security Policy in practice: step by step to a strict CSP

Many websites have a Content Security Policy that allows everything and therefore protects nothing. A strict CSP is not rocket science, but it needs a clean inventory and a fixed process for every change.

Airnite Editorial Team27 November 2026 · 3 min
Published
Reading time
3 min
Author
Airnite Editorial Team

In short

  • A strict CSP allows only the sources the website really needs and forbids inline scripts without a hash.
  • Every own inline script gets a SHA-256 hash that has to be updated with every change.
  • Before enforcement, the policy runs in report-only mode and is tested on every page in the browser.

What it is about

The Content Security Policy, CSP for short, is an instruction from the server to the browser: this website may only load scripts, styles and fonts from these sources. If an attacker manages to inject foreign code into a page, for example through a flaw in a form, the browser still does not execute it. This makes CSP one of the most effective shields against cross-site scripting.

In practice, however, you often see policies that allow everything with 'unsafe-inline' and wildcards for whole domains. They show up as present in an audit report, but hardly protect anything. The difference between such a policy and a strict CSP lies less in the technology than in the process.

How it works

It starts with an inventory: which scripts, stylesheets, fonts, images and third-party services does the website really load? For a lean website, the list is short. From it, a policy is derived that by default only allows the site’s own domain and names every additional source individually.

Inline scripts are the tricky part. Instead of allowing them across the board, every own inline script gets a SHA-256 hash that is listed in the policy. The browser only executes code whose checksum matches. The W3C standard CSP Level 3 and the OWASP cheat sheet recommend exactly this approach, or random nonces for dynamic pages. New policies first run in report-only mode: the browser reports violations but blocks nothing. Only when no legitimate source is reported any more is the policy enforced.

Where it goes wrong

The most common break comes with small changes. Someone adjusts an inline script, such as the dark mode switch, and forgets the hash. Nothing shows up on their own machine, but live the switch suddenly stops working. A proven approach is to let the build calculate the hashes itself and to have a check block the go-live if policy and script do not match.

The second classic is new third-party services. When an analytics tool or a map service is added, the CSP needs new entries. Anyone who generously allows whole domains under time pressure opens the door again. A better option is a centrally maintained list of permitted third-party addresses that only grows with approval.

How to proceed

List all resources of your website and remove what nobody needs. Write a policy with default-src 'self', without 'unsafe-inline' and 'unsafe-eval' for scripts, and with object-src 'none', base-uri and frame-ancestors. Allow own inline scripts via hash and let the build generate these hashes. Roll out in report-only mode first. After every change, test every page in a real browser for violations. A multilingual website quickly has several hundred pages, so the check should be automated.

Read more: HTTPS and security headers: your website's invisible armour, Static websites: less technology, less attack surface and Deployments with checksums: live is what was reviewed.

How Airnite helps

We develop a strict Content Security Policy for your website, build the hash calculation into your process and check every page automatically in the browser. In a penetration test, we assess existing policies and show where they actually stop attacks and where they do not.

Checklist

Sources

Every fact in this article is based on the following publications. Primary sources are marked.

  1. W3CContent Security Policy Level 3StandardPrimary source
  2. OWASPContent Security Policy Cheat SheetStandardPrimary source
  3. MDN Web DocsContent Security Policy (CSP)Vendor

Matching services

Read on

Current incidents and warnings are covered in the Zero Day magazine. Go to the magazine

Want to put this into practice in your company?

We assess your situation and tell you clearly what to do. For businesses, in confidence.

Talk to an expert now