Note: Disallowing inline styles and inline scripts is one of the biggest security wins CSP provides. However, if you absolutely have to use it, there are a few mechanisms that will allow them.
To allow inline scripts and inline event handlers, 'unsafe-inline'
, a nonce-source or a hash-source that matches the inline block can be specified.
The above Content Security Policy will allow inline <script>
elements
<script>
const inline = 1;
</script>
You can use a nonce-source to only allow specific inline script blocks:
You will have to set the same nonce on the <script>
element:
<script nonce="2726c7f26c">
const inline = 1;
</script>
Alternatively, you can create hashes from your inline scripts. CSP supports sha256, sha384 and sha512.
When generating the hash, don't include the <script>
tags and note that capitalization and whitespace matter, including leading or trailing whitespace.
<script>
const inline = 1;
</script>