Strict Content Security Policy (CSP) blocks inline script
See original GitHub issueWe’re in the process on GOV.UK Pay of implementing CSP in our apps and the inline script here gets blocked.
We can whitelist it with the hash of it’s contents which works until it gets changed, which I am sure doesn’t happen too often. So maybe this is fine, it secure at least as it means, we‘re not trusting GOV.UK Frontend without explicitly checking here. But it would also mean if no one noticed, this could get blocked and break some functionality.
But I wondered if we could wrap it a block so if we wanted to we could override it we could.
Option 1
{% block inlineJsCheck %}
<script>document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
{% endblock %}
Option 2
<script nonce="{{ inlineJsCspNonce }}">document.body.className = ((document.body.className) ? document.body.className + ' js-enabled' : 'js-enabled');</script>
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (11 by maintainers)
Top Results From Across the Web
CSP Allow Inline Scripts - Content Security Policy
When you enable CSP, it will block inline scripts, but there are some ways that you can allow inline scripts and still use...
Read more >CSP: script-src - HTTP - MDN Web Docs - Mozilla
The HTTP Content-Security-Policy (CSP) script-src directive ... You can use a nonce-source to only allow specific inline script blocks:.
Read more >Strict CSP - Content Security Policy
Strict CSP. Content Security Policy can help protect your application from XSS, but in order for it to be effective you need to...
Read more >Mitigate cross-site scripting (XSS) with a strict Content Security ...
Learn how to deploy a CSP based on script nonces or hashes as a defense-in-depth against cross-site scripting.
Read more >Content Security Policy blocks inline execution #9502 - GitHub
The Content Security Policy (CSP) prevents cross-site scripting attacks by blocking inline execution of scripts and style sheets.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jonheslop I think GOV.UK Frontend should aim to support the most proper way of doing this, if that’s adding a hash we should consider adding something to our documentation. What do you think?
Updating this issue as we now have more information on this. Subresource Integrity (SRI) has an impact on HTTP/2 coalescing, which in turn can cause web performance issues. Personal blog post about it here and a RFC related to changes we made on GOV.UK