Repository navigation
Description
With the widespread adoption of the Content Security Policy header and the known security risks associated with using unsafe-inline, Styled Components should provide an easier way to apply a nonce to its inline styles so unsafe-inline is not required.
I have examined some of the suggested workarounds, but none of them are straight forward or applicable to my use case, and I think a simple way to configure a nonce at runtime with a value provided from any server would provide much better nonce support in Styled Components.
Use Case
In my case I am not using Styled Components directly in my app, but it is being used by another library that I am using (Looker), so the solutions suggested here are not easily implemented.
Suggested Solution
I suggest a way to globally configure the nonce that Styled Components uses for all style tags appended to the document. In my use case a unique nonce value from the server is assigned to a variable on the window object on every request. When the React application initializes, that nonce variable could be assigned to a nonce configuration object that Styled Components uses for all subsequent style tags.
I have looked at the nonce support using the webpack_nonce variable, but this has some problems
- not all applications will be using the webpack bundler (Vite for example)
- I don't think webpack_nonce is intended to be part of webpack's public api (it is intended to be used internally by its own loaders), so it is probably not stable for use, which could be related to the next point
- It does not work as intended (I was able to get it to work with this work around)
I think it would be relatively easy to add a nonce API that builds off of this util, but allows the nonce to defined at runtime, something like this:
// index.js
import nonceConfig from 'styled-components'
// ...
nonceConfig.setNonce(window.myNonce)
// nonce.ts
+ import nonceConfig from 'styleComponents'
declare let __webpack_nonce__: string;
export default function getNonce() {
+ const configuredNonce = nonceConfig.getNonce()
+ if (configuredNonce) {
+ return configuredNonce
+ }
return typeof __webpack_nonce__ !== 'undefined' ? __webpack_nonce__ : null;
}
I would be happy to make a PR to implement this if the maintainers agree.
Thank for you consideration
Activity
Varnius commentedon Mar 8, 2024
Voting this up!
The proposed solution would work, though our use case is a bit different - it's a library that our clients add to their page in order to render a UI widget. The library uses
styled-componentsand some of the clients want to add a nonce value to the style block in order to apply proper CSP. We currently use a patched version ofstyled-componentsbut it's a hassle when we want to update the version in use.barbalex commentedon Mar 15, 2024
I just ran into this building an electron app. Electron wants devs to define a csp (https://www.electronjs.org/docs/latest/tutorial/security#7-define-a-content-security-policy).
Which failed in my case because the app is using styled-components and thus this error occurs:
pawelmidur commentedon Aug 13, 2024
https://emotion.sh/docs/@emotion/cache#nonce
bayasdev commentedon Sep 16, 2024
+1
gregtalarico commentedon Oct 9, 2024
+1
omatviiv commentedon Oct 24, 2024
But webpack only bundles once, its not a runtime tool. And nonce has to be highly dynamic and random value, it has to be used only once and thrown away. I don't understand how webpack could help to solve this issue.
ccreighton-apptio commentedon Oct 30, 2024
@omatviiv this is the current implementation of
getNonce:so styled-components is already looking for a value to set, but doesn't provide any way to properly configure this value.
omatviiv commentedon Oct 31, 2024
@ccreighton-apptio thank you I successfully managed to configure styled-components with
__webpack_nonce__.It was quite confusing, I tried to set
__webpack_nonce__in different ways so that nonce is guaranteed to be random on every subsequent request to the server but styled-components didn't see it.The only way that worked for me was to set webpack_nonce into ejs template:
and then at the very top of react application on ui to do the following:
Actually thats what webpack documentation actually says to set it in the entry file.
I suspect its so confusing because of the nature of these webpack special variables - they are not really a variable in any javascript scope but an instruction to webpack to set or inject some value and then to do with that value whatever webpack does... (something similar to webpack's
__filename).So I definitelly vode +1 to make it more strait forward.
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support
feat: add first-class CSP nonce support (#5693)
quantizor commentedon Mar 19, 2026
This fix is available in
[email protected]. Please try it and report any issues: