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.

Add __webpack__nonce to style tags

See original GitHub issue

I already had a brief talk with @mxstbr about this a couple of weeks ago: https://twitter.com/mxstbr/status/824626370760364032

When trying to enforce strict rules about which content is allowed on a specific page, a Content Security Policy (CSP) is the way to go.

Styled-Components currently put all of their Styles as an inline style to the DOM. Therefore, a CSP would need to contain: style-src unsafe-inline. As the name already puts: This is unsafe and not the recommended way of securing your app.

Luckily, there is the concept of nonce attributes. Style/Script tags with a whitelisted nonce can be specified in the CSP. If Styled-Components would set this nonce, we could get rid of the unsafe-inline setting.

Luckily, WebPack is already supporting the concept of a global __webpack_nonce__ variable. Once set, all dynamically injected code created by WebPack will have a nonce attribute with the correct value.

What needs Styled-Components to do?

If the webpack_nonce variable is set, pass the value to every generated style tag inside the nonce attribute of the style tag.

Unluckily, the __webpack_nonce__ is not well documented and kind of a myth. See Google https://www.google.at/search?q=webpack_nonce__&oq=webpack_nonce__&aqs=chrome..69i57.4663j0j7&sourceid=chrome&ie=UTF-8#q=__webpack_nonce__ and GitHub: https://github.com/webpack/webpack/search?utf8=✓&q=__webpack_nonce__&type=

Further readings:

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:22
  • Comments:34 (4 by maintainers)

github_iconTop GitHub Comments

14reactions
theKasheycommented, May 11, 2020

@wzijden - it all depends on your nonce settings. But it short - in a strict mode you have to provide nonce and SSR is often needed to do so. I mean - how without SSR you will generate and provide nonce? However styled-components themselves are not bound to SSR. It’s all about pure HTML.

And question - why one is actually need CSP for styles? CSP for scripts or frames is actually a really good idea, but styles? You Aren’t Gonna Need It.

12reactions
fdevcommented, Nov 25, 2020
Read more comments on GitHub >

github_iconTop Results From Across the Web

Set nonce to style tag by code in Webpack
html and insert nonce in it. My current work is to build a script (e.g. MyJS.js ) to create some components that can...
Read more >
Content Security Policies
Webpack is capable of adding a nonce to all scripts that it loads. To activate this feature, set a __webpack_nonce__ variable and include...
Read more >
Content Security Policy: How to create an Iron-Clad nonce ...
Nonce and the failure of existing Webpack modules. It is vital that the nonce is uniquely generated for each page load. It cannot...
Read more >
nonce - HTML: HyperText Markup Language - MDN Web Docs
The nonce attribute is useful to allowlist specific elements, such as a particular inline script or style elements. It can help you to...
Read more >
CSP Allow Inline Styles
One of the easiest ways to allow style tags when using CSP is to use a nonce. A nonce is just a random,...
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