Repository navigation
Description
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=%E2%9C%93&q=__webpack_nonce__&type=
Further readings:
- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
- https://developers.google.com/web/fundamentals/security/csp/
- https://csp.withgoogle.com/docs/index.html
Activity
mxstbr commentedon Jun 8, 2017
This seems like a very reasonable thing to implement!
@sokra @TheLarkInn can you confirm that
__webpack_nonce__will be supported in the future? Can you point us to any docs about it/is it documented at all?sokra commentedon Jun 8, 2017
I guess there are no docs for it yet. But if you add docs for it it probably stays supported.
Here is the PR for the feature: webpack/webpack#3210
arahansen commentedon Aug 18, 2017
Unfortunately, nonce attributes are not supported in ie11
Is there any known workaround for avoiding such CSP violations as this in ie11?
elzii commentedon Sep 27, 2017
I would love more discussion on this. PCI Compliance audits can mandate the need to have a CSP that prohibits using awesome libraries like this, or worse, having to remove them to pass compliance :-/
arahansen commentedon Sep 28, 2017
@elzii The discussion here resolved with the fact that ie 11 doesn't support CSP restrictions anyways. So even if your server restricts inline styles, ie11 will still render them, and you will not see any errors in the browser.
mindhaq commentedon Feb 1, 2018
I would also love to see more discussion on this. My current setup with a strict
Content-Security-Policyeffectively prevents the use of styled components.Inserting a static nonce with webpack seems like a bad idea as well. If all style elements have the same nonce everywhere everytime, this would defeat the whole purpose of having a nonce.
mxstbr commentedon Feb 1, 2018
We implemented support for
__webpack-nonce__back in July, what else are you looking for @mindhaq?johannesnagl commentedon Feb 1, 2018
simonpkerr commentedon Mar 27, 2018
@johannesnagl can you point me at that working solution? I can't get this working.
I've added webpack_nonce to my entry point index.js like this...
this means the nonce generated on the server and injected into the template can be picked up by the react app entry point. The nonce is getting picked up from the style tag, but webpack isn't picking up the webpack_nonce value.
johannesnagl commentedon Mar 27, 2018
This is our implementation!
In our layout file, which is served via PHP, we have the following, simplified code (above the script tag with the webpack-bundle of course):
simonpkerr commentedon Mar 27, 2018
that looks pretty similar to what I've got. Does the webpack_nonce value need to be set before any imports? I'm getting an error.
The nonce shown in the image below is the one in the entry point file that references __webpack_nonce__. So its getting picked up in the entry file.
mxstbr commentedon Mar 27, 2018
I think it has to be before any imports. Since imports are hoisted, you'll have to create a separate file (
import-nonce.js) and import that before anything else that imports styled-components.7 remaining items
TheoMer commentedon Jun 8, 2019
@johannesnagl Are there any examples of this working with next.js? I am absolutely stuck at #887 (comment)
johannesnagl commentedon Jun 8, 2019
@TheoMer sorry, don't have any experience with next.js!
TheoMer commentedon Jul 15, 2019
@mxstbr Any ideas on implementing this with next.js?
sebastiaandegeus commentedon Sep 30, 2019
I still cannot get this to work. I have the following html:
And the following header:
I will get a lot of these errors:
I don't understand why it is not working because the nonces are the same. It only works when I use
'unsafe-inline'forstyleSrc.sebastiaandegeus commentedon Oct 1, 2019
I managed to solve my problem by adding this to the html:
Notice the nonce on the script tag as well. You may need this if you also used a csp directive that disallows inline scripts.
DylanPiercey commentedon Jan 31, 2020
@sebastiaandegeus doesn't the above negate the purpose of the nonce? Any scripts / styles injected client side would probably be able to read
window.__webpack_none__in the above right?sebastiaandegeus commentedon Feb 3, 2020
@DylanPiercey
I am not 100% sure, this is my understanding of it:
It seems that stealing the nonce is already a point of discussion in the spec:
w3c/webappsec-csp#65
w3c/webappsec-csp#98
As far as I understand injected scripts would not be executed if the nonce is not set. For example a comment in your comment section that contains a script tag. It will not be executed if the nonce is wrong and it cannot get the nonce because it is blocked right away.
If the attacker already has a way of executing their javascript code to steal the nonce then it's already too late. Blocking it will not help anymore.
I was digging through the code and found this: https://github.com/styled-components/styled-components/blob/147b0e9a1f10786551b13fd27452fcd5c678d5e0/packages/styled-components/src/utils/nonce.js
Which is used here to add a nonce to the stylesheet:
styled-components/packages/styled-components/src/sheet/dom.js
Line 34 in 4bf44fc
This is happening client side so it has to get the nonce from somewhere, which at that point had to be made available in a javascript variable or DOM.
Maybe one of the maintainers can shed some light on this issue?
If I implemented it wrong and/or insecure please let us know how to do it correctly.
DylanPiercey commentedon Feb 3, 2020
@sebastiaandegeus yeah I think it makes sense. In order to read the nonce from the global variable the page would have already have to have been exploited.
wzijden commentedon Apr 28, 2020
Could you at least add a section to your docs about the caveats of this library in combination with a strict CSP? I had to search and read for hours and I still don't completely get what is supported and what isn't. It now seems to me you can only get it working with server side rendering. Is that correct?
theKashey commentedon May 11, 2020
@wzijden - it all depends on your nonce settings. But it short - in a strict mode you have to provide
nonceand SSR is often needed to do so. I mean - how without SSR you will generate and providenonce?However
styled-componentsthemselves 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.
sasha-3ap commentedon May 28, 2020
For example, we use nginx and inject nonce in CSP headers and meta tag with property csp-nonce which is then used by material-ui.
fdev commentedon Nov 25, 2020
To the next person trying to get this to work: the code suggested by @johannesnagl in #887 (comment) and @simonpkerr in #887 (comment) does work, but not in development.
gordonkristan commentedon Mar 2, 2021
This may be an odd case that only applies to us, but I want to point out how I got it working. The styled components code to get the nonce normally looks like this:
But after looking at the real source and not just the sourcemap, I saw this:
After some digging, it seems that webpack will replace references to
__webpack_nonce__with__webpack_require__.nc. I'm not 100% sure why that is and I don't want to dive into too much detail, but I can say that adding this code to the top of your first module will fix the issue:Remember that it has to be in a module, because
__webpack_require__isn't available outside of a module.EDIT: I want to point out that this is the same solution that simonpkerr demonstrated. The difference is that we want to set
window.__webpack_nonce__outside of a module. In our case, our PHP code is setting the nonce value. If you want to set the nonce value from inside a module then the code that simonpkerr posted will work.ljacho commentedon Mar 11, 2021
@gordonkristan can you elaborate on this please? can you bring example similar like @simonpkerr
thank you for your help!
alex-r89 commentedon Apr 28, 2021
Sorry to comment on a closed issue, but has anyone had any luck using this with NextJS and styled-components? I am getting Refused to apply inline styles errors when using styled-components with my content-security-policy.
My solution so far is listed below, but the nonce does not appear to be working.
NextJS + styled-components nonce issue