Engineering 4 min read Updated
A Content Security Policy that holds in practice
In short
A policy holds when inline scripts are gone, every nonce is fresh on each response, and the rules have been tested in report-only mode first.
An injection bug can get through. A Content Security Policy then tells the browser which scripts, styles and other resources the page may load and run. A policy written well limits the damage, while a loose one looks like protection and does little. Start by allowing only the sources you can name, and deploy the policy in report-only mode first.
A policy limits what the site’s code may do
MDN describes CSP as a set of instructions from a website to the browser that restrict what the site’s code may do. The key directive is default-src. It sets a fallback for every resource type that has no directive of its own. A policy such as default-src 'self' loads same-origin resources by default. A line like img-src 'self' example.com then allows one named host for images as well. Each directive narrows one type of resource, so start from a small default and open only what the site needs.
Host allowlists can be bypassed
An allowlist names the hosts that scripts may load from. web.dev says that allowlist policies can be bypassed by attackers and need a lot of customisation. It gives a host allowlist for one Google API as an example, and says such a policy is probably not effective against cross-site attacks.
| Approach | Example | Main weakness |
|---|---|---|
| Host allowlist | A named host in script-src | Can be bypassed and needs much customisation (web.dev) |
| Nonce with strict-dynamic | A random value per response in script-src, plus strict-dynamic | The nonce must change on every response and stay unpredictable; only script and style elements take one |
| Hash | A hash of one inline script in script-src | Any change inside the script tag, even whitespace, produces a new hash and the script stops running |
Build a strict policy around nonces
web.dev recommends a nonce-based policy: script-src with a nonce plus strict-dynamic, and object-src and base-uri set to none. The server generates a new nonce for every response, and the same value appears on each script element the application is allowed to run. OWASP warns against middleware that adds nonces to every script tag, because an injected script would then receive a valid nonce too. Use the nonce only on scripts that your own templates write.
OWASP’s basic policy is default-src 'self' with frame-ancestors 'self' and form-action 'self'. Its tighter variant also limits script-src, connect-src, img-src and style-src to 'self'. Set object-src to 'none' where embedded objects are unnecessary, and use frame-ancestors 'none' to block all framing.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Unsafe-inline undermines the policy
MDN warns developers to avoid 'unsafe-inline', because inline JavaScript is among the most common XSS vectors, and preventing its uncontrolled use is one of the main goals of a policy. Once a directive contains a nonce or a hash, browsers ignore 'unsafe-inline' in that directive, so keeping the keyword beside them adds nothing. A policy that still needs it has inline scripts to move out first.
Roll out in report-only mode first
The Content-Security-Policy-Report-Only header applies the policy without enforcing it. Violations are sent to the reporting endpoint that the policy names. MDN notes that both headers can appear in one response, in which case both policies are honoured, and that a report-only policy cannot be delivered in a meta element. web.dev suggests report-only mode for testing a change that could break the site, and then switching to the enforcing header once you are confident. OWASP calls report-only a precursor to blocking mode.
A practical sequence is to run report-only long enough to cover the pages and user paths that matter, fix the inline scripts and third-party sources that appear in the reports, and only then enforce. This order is general practice rather than a finding from one source.
A practical checklist
- Start with default-src 'self' and object-src 'none', then add only the sources you can name.
- Generate a new nonce for each response, and do not add it to every script tag through middleware.
- Move inline scripts and event handlers into files, and remove 'unsafe-inline'.
- Deploy in report-only mode first, and read the reports before enforcing.
- Set frame-ancestors to 'self' or 'none', depending on whether the site is ever framed.
The other response headers are covered in HTTP security headers worth setting. The output side of the problem, escaping data before it reaches the page, is covered in escaping output and stopping XSS.
The advice on nonces, hashes and the rollout order follows the cited guidance. The checklist and the rollout sequence summarise it for this article. Whether a policy suits a particular application depends on its scripts and third-party services.
Common questions
- Why not simply allow the domains my site uses?
- web.dev says allowlist policies can be bypassed and need a lot of customisation, and a host allowlist for one Google API is probably not effective against cross-site attacks.
- Does unsafe-inline still matter when I add a nonce?
- Browsers ignore “unsafe-inline” in a directive that contains a nonce or hash, so the keyword adds nothing there. MDN advises avoiding it, because inline JavaScript is among the most common XSS vectors.
- Can I test a policy without blocking anything?
- Yes. The Content-Security-Policy-Report-Only header does not enforce the policy but sends violations to a reporting endpoint, and both headers can be sent together.
Sources
- Content Security Policy (CSP) - HTTP | MDN, Mozilla Developer Network (MDN). Accessed 24 November 2026
- Why should you use a strict CSP?, web.dev (Google). Accessed 24 November 2026
- Content Security Policy - OWASP Cheat Sheet Series, OWASP Foundation. Accessed 24 November 2026
Further reading
-
UTF-8 from the editor to the database
Use UTF-8 in every layer, declare it in the HTTP header and the page, name utf8mb4 explicitly in MySQL, and normalise text to NFC.
-
Escaping output so that XSS cannot run
Escape each value for the place where it is printed, because text that is safe in HTML can still run as code inside a script, a URL or CSS.
-
CSRF protection in plain terms
Every state-changing request needs its own token; SameSite and an Origin check add layers of defence, but they do not replace the token.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea