A CSP of script-src 'self' blocks every inline <script> block on the page by default, including ones you wrote. On a static site with no server available to generate a per-request nonce, the fix is a hash: compute the SHA-256 hash of the exact script content and add it to the policy explicitly.
Read the script
This is a spoken summary of the guide on Content Security Policy for a static site with inline scripts. The real hash, the steps and the full explanation are on the page.
The short answer. A C S P of script src self blocks every inline script block on the page by default, including ones you wrote. On a static site with no server available to generate a fresh nonce for each request, the fix is a hash. You compute the S H A two fifty-six hash of the exact script content and add it to the policy explicitly.
The takeaways. Self does not mean my own scripts. It means scripts loaded through a source attribute from your own domain. Inline script is excluded by design, because inline script is exactly what a cross-site scripting payload injects. Nonces don't work on a pure static site, since they need a server to create a fresh random value per response, and a static file has no such step. Hashes are the static-site answer: a fingerprint of content that never changes, which is exactly what a static file guarantees. Any edit to the script invalidates the hash, and that is the real maintenance cost. It is not implementing C S P once, but keeping the hash in sync with the script forever after.
Why self breaks inline scripts. Self only covers scripts loaded through a source attribute pointing at your own origin. An inline block has no source to match. It is excluded because of what it is, not where it lives.
A real worked example. Every page on this site carries one small inline script in the head, before the stylesheet loads. It reads a stored theme choice and applies it immediately, so nobody who picked dark mode sees a flash of the light theme. It is five hundred twenty characters, does one thing, and never changes unless the theme system does. That is the exact case a hash is built for. The hash is computed from the exact text between the script tags, hashed with S H A two fifty-six and base sixty-four encoded, and the page shows it in full. A policy of script src self plus that hash would allow exactly this script and nothing else inline. That is the goal: not no C S P, and not allow all inline script, but an allowlist of one known, unchanging block.
The cost later. Edit the script, even whitespace, and its hash changes. The policy would then reject the new version silently, the anti-flash script would stop running, and the flash of the wrong theme would come back. A hash policy is a permanent coupling between the script's content and the policy that allows it.
Why hashes and not nonces. A nonce is the more common recommendation for server-rendered apps, where the server mints a fresh value on every response. A static site has no server generating anything per request. Every visitor gets the identical file, so a nonce baked into it would be the same for everyone, which defeats the point of being unpredictable. A hash stays valid for as long as the content doesn't change, which for a static site is the right lifetime.
To get the hash for your own script. One, isolate the exact text between the script tags, whitespace and all. Two, hash it with S H A two fifty-six and base sixty-four encode the digest. Three, prefix it with sha two fifty-six and a dash, and add it to your script src directive. Four, verify in the browser. Deploy the policy and check the console. A blocked script's violation message includes the hash Chrome and Firefox actually expected, which is the fastest way to catch a mismatch.
One honest note. This site does not currently ship a C S P header. The hash is real and correctly computed from the live script, but adding the header is server configuration that a static file can't demonstrate on its own, so I'm not implying more than what is actually deployed.
Key takeaways
- 'self' does not mean "my own scripts." It means scripts loaded via a src attribute from your own domain — inline script is excluded by design, since inline script is exactly what an XSS payload injects.
- Nonces don't work on a pure static site. They need a server to mint a fresh random value per response — a static HTML file has no such step.
- Hashes are the static-site answer — a fingerprint of content that never changes, which is exactly what a static file guarantees.
- The hash below is real, computed from this site's own anti-flash theme script, the same one running on every page you're reading this on.
- Any edit to the script invalidates the hash. This is the actual maintenance cost — not implementing CSP once, but keeping the hash in sync with the script forever after.
Hash an inline script, in four steps
Isolate the exact text between your script's opening and closing tags — whitespace and all, exactly as it appears in the file.
Hash it with SHA-256 and base64-encode the digest.
Prefix it with sha256- and add it to your script-src directive.
Verify in the browser — deploy the policy, then check the console
A blocked script's CSP violation message includes the hash Chrome and Firefox actually expected, which is the fastest way to catch a mismatch without hand-computing it twice.
Why "self" breaks inline scripts
script-src 'self' is a common first CSP policy, and it looks like it should just work — the script is on the same domain, after all. But CSP's 'self' keyword only covers scripts loaded via a src attribute pointing at your own origin. An inline <script>...</script> block has no src to match — it's excluded categorically, not because of where it lives but because of what it is. That's deliberate: unauthorized inline script is precisely the payload a cross-site scripting attack injects, so CSP's whole model treats "inline" as the thing to distrust by default.
A real worked example, not a placeholder
Every page on this site carries one small inline script in the <head>, before the stylesheet loads — it reads a stored theme choice from localStorage and applies it immediately, so nobody who picked dark mode sees a flash of the light theme on the next page load. It's 520 characters, does one thing, and never changes unless the theme system itself changes. That makes it the exact case a hash-based CSP is built for.
| Property | Value |
|---|---|
| Script length | 520 characters |
| Hash algorithm | SHA-256 |
| Computed hash | sha256-6oplrr6UXKzzd9XzJqaE3iLEpvinpRxIv3FfSn7Cy7Y= |
That hash was computed by taking the exact text between this site's own <script> and </script> tags, hashing it with SHA-256, and base64-encoding the result — not copied from a tutorial. A policy of script-src 'self' 'sha256-6oplrr6UXKzzd9XzJqaE3iLEpvinpRxIv3FfSn7Cy7Y=' would allow exactly this script to run and nothing else inline, which is the actual goal: not "no CSP" and not "allow all inline script," but an allowlist of one specific, known, unchanging block.
Why hashes, not nonces, for a static site
Nonce-based CSP is the more common recommendation in general security writing, and for a server-rendered app it's usually the right call — the server mints a fresh random value on every response and stamps it onto both the CSP header and the script tag, so a cached or replayed old page can't reuse a stolen nonce. A static site has no server generating per-request anything. Every visitor gets the identical HTML file, so a nonce baked into that file would be the same for every single request — which defeats the entire point of a nonce being unpredictable per response. Hashes don't have this problem, because a static file's content is genuinely static; the hash is valid for as long as the content doesn't change, which for a static site is exactly the right lifetime.
How to get the hash for your own inline script
- Isolate the exact text between your script's opening and closing tags — whitespace and all, exactly as it appears in the file.
- Hash it with SHA-256 and base64-encode the digest.
- Prefix it with sha256- and add it to your script-src directive.
- Verify in the browser — deploy the policy, then check the console. A blocked script's CSP violation message includes the hash Chrome and Firefox actually expected, which is the fastest way to catch a mismatch without hand-computing it twice.
Sources and further reading
Primary documentation for the topics in this guide: