When I first started implementing Content Security Policy (CSP) at work, I thought allowing JavaScript and CSS would be straightforward. I quickly discovered that it wasn't.
Questions started appearing one after another:
Why is my inline script blocked even though it's my own code?
Why doesn't adding a nonce to
<link>work?Why is my SHA-256 hash ignored?
What's the difference between the
integrityattribute and a CSP hash?When should I use a nonce? When should I use a hash?
Most articles explain these concepts separately. In this article, I'd like to share the practical lessons I learned while implementing CSP in a real ASP.NET Core application and explain when each mechanism should be used.
Why Do We Need Nonces and Hashes?
In the previous article, we saw that CSP blocks every script or stylesheet unless it has been explicitly authorized.
Consider the following page:
<script>
alert("Hello");
</script>With this CSP:
Content-Security-Policy:
script-src 'self';The browser blocks the script.
Why?
Because CSP cannot distinguish between:
your legitimate inline script
an attacker who successfully injected JavaScript through an XSS vulnerability
From the browser's point of view, they're both simply inline code.
That's exactly why CSP introduced nonces and hashes: they allow you to explicitly tell the browser, "This piece of code is trusted."
Two Different Ways to Trust Inline Code
CSP offers two mechanisms.
The first is a nonce.
script-src 'self' 'nonce-AbCdEf123';
<script nonce="AbCdEf123">
console.log("Safe");
</script>The browser compares the nonce in the CSP header with the nonce on the <script> element. If they match, the script executes.
The second mechanism is a hash.
script-src 'sha256-xxxxxx...';Instead of comparing a random value, the browser computes the SHA-256 hash of the inline script itself.
If the computed hash matches the one declared in the CSP header, the script is allowed.
Nonce vs Hash
Although both authorize inline content, they solve different problems.
| Feature | Nonce | Hash |
|---|---|---|
| Generated | Every request | Once per content version |
| Changes | Every request | Whenever content changes |
| Best for | Dynamic pages | Static inline code |
A Common Misunderstanding
One of the first mistakes I made was assuming that adding a nonce everywhere would secure every resource.
For example:
<link
rel="stylesheet"
href="/css/site.css"
nonce="12345">It looks reasonable.
Unfortunately, browsers completely ignore it.
The CSP specification only allows nonces on inline
<script>and<style>blocks.
This means:
<style nonce="abc">works.
But:
<link rel="stylesheet"
href="/site.css"
nonce="abc">does absolutely nothing.
The stylesheet is authorized by its URL, not by a nonce.
What Actually Allows External Files?
For external resources, CSP simply checks whether the resource's URL matches one of the allowed sources.
style-src 'self';allows
<link rel="stylesheet"
href="/css/site.css">because the file comes from the same origin.
Similarly,
script-src 'self'
https://cdnjs.cloudflare.com;allows
<script src="https://cdnjs.cloudflare.com/...">No nonce is involved.

Join the conversation! Your thoughts help the community grow.