MORTIMERDEV GUIDES / CONTENT-SECURITY-POLICY

What 'unsafe-inline' does in a Content-Security-Policy, and what to use instead

4 minute read Updated Sources checked 30 November 2026

Short answer: 'unsafe-inline' allows code written directly in your HTML to run: <script> blocks without a src, event handler attributes such as onclick="…", and javascript: links. In style-src it allows <style> blocks and style="…" attributes. That is exactly what an attacker injects in a cross-site scripting (XSS) attack, so a script-src with 'unsafe-inline' gives you very little protection. Replace it with a nonce or a hash.

What it allows

With script-src 'self' 'unsafe-inline', all of these run:

<script>
  startApp();
</script>
<button onclick="buy()">Buy</button>
<a href="javascript:openMenu()">Menu</a>

So does anything an attacker manages to inject, such as <img src=x onerror="steal()"> in a comment field. The browser can't tell your inline code from theirs. That's why the policy's own name for it starts with "unsafe".

For styles, style-src 'unsafe-inline' is much less dangerous. Injected CSS can still leak some data (for example, attribute values through CSS selectors) or deface a page, but it can't run code. A common first step is to remove 'unsafe-inline' from script-src and deal with style-src later.

When browsers ignore it

'unsafe-inline' is ignored when the same directive also has a nonce or a hash (or, for scripts, 'strict-dynamic'):

script-src 'self' 'unsafe-inline' 'nonce-r4nd0m'

Here, browsers that understand nonces (all current ones) ignore 'unsafe-inline', so only inline scripts carrying nonce="r4nd0m" run. Older browsers that don't understand nonces fall back to 'unsafe-inline'. That's deliberate: you can add 'unsafe-inline' as a fallback without weakening the policy in modern browsers.

Replace it with a nonce

A nonce is a random value your server generates for every response and adds to both the header and each script you trust:

Content-Security-Policy: script-src 'nonce-8IBTHwOdqNKAWeKl7plt8g==' 'strict-dynamic'; object-src 'none'; base-uri 'none'
<script nonce="8IBTHwOdqNKAWeKl7plt8g==">
  startApp();
</script>
<script nonce="8IBTHwOdqNKAWeKl7plt8g==" src="/app.js"></script>
  • Generate a new one for every response, from a cryptographically secure random generator. The spec says it should be at least 128 bits before encoding; base64 is the usual encoding. A fixed or predictable nonce lets anyone who can read it run any script, which is little better than 'unsafe-inline'.
  • 'strict-dynamic' lets a nonce- or hash-trusted script add more scripts with document.createElement('script') (for example a tag manager adding its own), so you don't have to list every host. Scripts added with document.write() or written straight into the HTML still need a nonce or hash. In browsers that support it, host and scheme sources (such as https:), 'self' and 'unsafe-inline' in that directive are ignored.
  • Nonces only work when HTML is generated per request. A page cached and served to everyone has the same nonce for everyone.

Or a hash, for static pages

If your HTML is static (a static site, or a page cached on a CDN), use the hash of each inline script instead. The browser hashes the script's exact text and runs it only if the hash is in the policy. For <script>startApp()</script>:

Content-Security-Policy: script-src 'sha256-tbHiLfFxk+3UKR1aZbQCNmx+rx8ndFa/zkXGLnfe+Ic='

The hash covers every character between <script> and </script>, including spaces and line breaks, so recalculate it whenever the script changes. A policy with only hashes also blocks external <script src> files unless you list their sources, give each one an integrity attribute whose hash is in the policy (check browser support), or add 'strict-dynamic' and load them from a hashed script. The CSP tool's "Test it" section calculates the hash for you and tells you whether your policy allows the script.

Event handlers and style attributes

Nonces can't go on attributes, so onclick="…" and style="…" need more work:

  • Best: move handlers into a script with addEventListener, and inline styles into a stylesheet or classes.
  • If you can't yet: 'unsafe-hashes' plus the hash of the attribute's value lets that exact code run in a handler, without allowing every inline script. It's still weaker than moving the code out, because the same code would also run if an attacker injected it somewhere else, so use it as a stepping stone.

A safe path to removing it

  1. Deploy your new policy as Content-Security-Policy-Report-Only first. Browsers report what would be blocked without blocking it.
  2. Add nonces or hashes to your inline scripts, and move event handlers into scripts.
  3. Watch the reports (or the browser console) until nothing you need is reported.
  4. Switch the header to Content-Security-Policy.

Paste your policy into the CSP explainer to see what 'unsafe-inline' is doing in it, whether it's being ignored, and what each resource type falls back to.