Client-side nonce support
See original GitHub issueI am trying to craft an intelligent Content-Security-Policy while using styled-component. The suggested solution to this seems to be to use SSR to set the webpack_nonce variable, ensuring that webpack inlines in in all created style-tags. However, I cant find a solution to this problem when you are not using SSR.
Without knowing the internals of styled-components, it seems to me that it should be possible to specify a nonce in a non-ssr environemt. Something like:
import { setNonce } from "styled-components"
setNonce(myNonce)
This would allow users to craft their own solutions that is independent from both SSR and webpack. In my use case it would be trivial to inject a unique nonce with every page request, even though SSR is not feasible.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:14
- Comments:17 (3 by maintainers)
Top Results From Across the Web
nonce versus custom data. is it nonce= or nonce- on the client ...
so the phrase 'nonce' is really irrelevant and I've just created a rule that client-side all script must match the attribute ...
Read more >How to generate nonce from client-side Javascript - dev
I am using <script type="text/discourse-plugin" version="0.8.9"> in my instance's <head> and it provides me access to the api object.
Read more >Developers - Client-side nonce support - - Bountysource
Client-side nonce support ... I am trying to craft an intelligent Content-Security-Policy while using styled-component. The suggested solution to this seems to be ......
Read more >Client-side caching when using CSP with nonces in nginx
I'm using nginx's expires directive; its etag directive as well as the Last-Modified header (if I understand correctly) are on by default.
Read more >Are server nonce and client nonce in SSL equal?
The nonces on the server and client side are created independently from each other. This means they are most likely not the same....
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
Disclaimer: I may not be 100% aware of all the Content-Security-Policy directives implications.
From what I understand of the notion of the
nonce
attribute, it only makes sense if it is used once (e.g. generated per request, using server-side rendering)If it is statically defined at build-time and served with the same value on every request, there is nothing preventing a malicious person to add that attribute since that person would easily be able to know it (unless I completely misunderstand its purpose)?
However, to have styled-components working on a locked down CSP, on a client-side only environment, it may be possible to use the
hash
policy, which works in a very similar way, with the same goal: whitelist some part of the inline scripts/styles served with your HTML page, but instead of being attributed a one-shot token by the server, it consists in a hash of that <style> content itself.I think the scenario would look like (at build time):
style-src
directive of the CSPDoes
styled-components
currently provide a way to get those hashes at build time?More informations about
nonce
andhash
: https://developers.google.com/web/fundamentals/security/csp/#if_you_absolutely_must_use_itI think you should be able to set
window.__webpack_nonce__
on the client-side and we will pick it up, maybe? If we don’t, we definitely should! /cc @kitten let’s build that into #2357 maybe?