You moved away from 'unsafe-inline', added a hash to your Content Security Policy, deployed, and the browser still blocks the script. The policy looks right and the script hasn't changed. In most cases the hash itself is fine as a hash. It just isn't the hash of the exact bytes the browser sees, or it's written in the wrong encoding.
This post walks through one small inline script, computes its CSP hash by hand, and shows the three mistakes that cause most "refused to execute inline script" errors.
What the browser actually hashes
A hash-source in CSP looks like this:
Content-Security-Policy: script-src 'sha256-<base64-value>' The rules that matter, from MDN's script-src page and the CSP Level 3 spec:
- The hash is computed over the contents of the
<script>element, not including the<script>and</script>tags. - Capitalisation and whitespace matter, including leading and trailing whitespace (MDN states this explicitly).
- CSP supports
sha256,sha384andsha512. - The digest is written as Base64, not hex. In the CSP3 grammar it's
base64-value = 1*( ALPHA / DIGIT / "+" / "/" / "-" / "_" )*2( "=" ).
Sources (checked 2 Dec 2026): MDN – CSP: script-src and W3C Content Security Policy Level 3 (Working Draft dated 27 October 2026).
Hashes allow inline <script> blocks, but not inline event handlers like onclick="…". Those need 'unsafe-hashes' as well, which is a different topic.
Worked example: one inline script, three different hashes
Here's the script, as it appears in the HTML:
<script> window.APP_ENV = "prod"; </script> The content between the tags isn't just window.APP_ENV = "prod";. It's a newline, two spaces, the statement, and another newline. od -c shows the 28 bytes:
\n w i n d o w . A P P _ E N V = " p r o d " ; \n Step 1 – hash the exact bytes
printf '\n window.APP_ENV = "prod";\n' > lf.js openssl dgst -sha256 -binary lf.js | base64 # rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX/62vu5N4RPc= Same thing in Node.js:
const crypto = require("crypto"); const body = '\n window.APP_ENV = "prod";\n'; console.log(crypto.createHash("sha256").update(body, "utf8").digest("base64")); // rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX/62vu5N4RPc= So the policy is:
Content-Security-Policy: script-src 'sha256-rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX/62vu5N4RPc=' Step 2 – see how easily it changes
We ran the same statement through three small variations (all values computed with sha256sum / openssl on 2 Dec 2026):
| Script content | Bytes | SHA-256 (Base64) |
|---|---|---|
\n window.APP_ENV = "prod";\n (LF, as written) | 28 | rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX/62vu5N4RPc= |
\r\n window.APP_ENV = "prod";\r\n (CRLF) | 30 | snqKRHN+kP043edfxTn6hIDZV6x6bwKsUzPq4Nbb0ww= |
window.APP_ENV = "prod"; (trimmed) | 24 | /MzdPJ8Wj/3uLrpdhHBB0mAlmLWGysNKKyutq/K9pQ0= |
Two extra \r bytes or two missing newlines and you get a completely different hash. That's how SHA-256 is supposed to behave, but it means a hash you copied from your editor can be wrong for the file your server actually sends.
Step 3 – don't Base64 the hex string
sha256sum prints hex:
af3e488ceea7795a6710a7ce81e1e77425e713d49f84b497ffadafbb937844f7 That's 64 hex characters for 32 bytes. The common mistake is to feed that text into a Base64 encoder:
sha256sum lf.js | cut -d' ' -f1 | tr -d '\n' | base64 -w0 # YWYzZTQ4OGNlZWE3Nzk1YTY3MTBhN2NlODFlMWU3NzQyNWU3MTNkNDlmODRiNDk3ZmZhZGFmYmI5Mzc4NDRmNw== That's 88 characters, because you encoded 64 ASCII characters rather than 32 raw bytes. A correct SHA-256 value in Base64 is always 44 characters ending in one =. If your SHA-256 value is 88 characters long, you've Base64-encoded the hex text. (For SHA-512, 88 characters is actually the correct length. The same mistake there gives 172.)
The fix is to decode the hex back to bytes first:
python3 -c "import base64;print(base64.b64encode(bytes.fromhex('af3e488ceea7795a6710a7ce81e1e77425e713d49f84b497ffadafbb937844f7')).decode())" # rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX/62vu5N4RPc= It matches Step 1, so the hex and the Base64 describe the same 32 bytes.
The three usual suspects
- The bytes changed after you hashed them. Templating engines, minifiers, HTML formatters, and Git's
core.autocrlfon Windows can all change the indentation or line endings of an inline block. Hash the HTML your server actually serves (curlthe page and pull out the block), not the source template. - The hash is in hex. CSP wants Base64. If the value is 64 characters from
0-9a-f, convert it to Base64 first. - You Base64-encoded the hex text. For SHA-256, 88 characters instead of 44 is the giveaway.
A quick tip: when Chrome blocks an inline script, its console error usually includes the 'sha256-…' value it computed for that block. Compare it with yours. If they differ, the bytes differ.
The CSP3 grammar accepts - and _ in the value, so you might see a URL-safe form like rz5IjO6neVpnEKfOgeHndCXnE9SfhLSX_62vu5N4RPc somewhere. The safest thing is to use standard Base64 with padding, exactly as openssl … | base64 prints it, which is also what the MDN examples use.
How to use the tool for step 3
If you already have a hex digest from sha256sum, CI logs or a build plugin, you can convert it without writing code on the Hex ↔ Base64 converter. The page is in English:
- Choose the Hex to Base64 tab.
- Paste the 64-character digest into Hex String Input.
- Copy the result from Base64 Output, check that it's 44 characters long, and put it in your policy as
'sha256-<that value>'.
To go the other way (for example, to compare a CSP value with sha256sum output), use the Base64 to Hex tab or Swap Inputs. The conversion runs in the browser, which matters if the hash belongs to a script you don't want to paste into a server-side tool.
Checklist
- Hash the exact content between
<script>and</script>, including the newlines and indentation, from the HTML that's actually served. - Use
openssl dgst -sha256 -binary | base64ordigest("base64"), not hex. - SHA-256 in Base64 = 44 characters, one
=. SHA-384 = 64 characters, no=. SHA-512 = 88 characters,==. - Re-hash after every build step that touches the inline block.
Hex ↔ Base64 converter: https://base64decodetool.com/hex/
Disclosure: this tool is from Mind vs Drive, the team behind this account.