question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strict Content Security Policy (CSP) blocks inline script

See original GitHub issue

We’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:closed
  • Created 4 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

2reactions
NickColleycommented, Nov 20, 2019

@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?

1reaction
Nooshucommented, Mar 3, 2020

I’ve also heard that SRI is now considered and anti-pattern and may even impact perf, but I’ve yet to see any concrete evidence of that.

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found