Nonce or hash for inline styles (for Content-Security-Policy) #18451
Replies: 7 comments 22 replies
-
I am facing exactly the described issue. While this seems to be hard to do with next.js, it was no problem to achieve with our, much older, custom webpack setup. |
Beta Was this translation helpful? Give feedback.
-
Can you confirm that nonce changes on every page refresh?
…Sent from my iPhone
On May 11, 2021, at 01:20, Sophie Koonin ***@***.***> wrote:
getInitialProps in _app.js. If that's not an option for you, the CSP-header solution may not work.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHu
|
Beta Was this translation helpful? Give feedback.
-
@sophiekoonin I tried your above solution to set nonce. There's something I don't quite understand though, it works perfectly fine in dev, but whenever I try the production environment on my machine (nothing fancy, npm run build && npm run start), I always end with: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client. I know it's not directly related, but I would appreciate any insights. |
Beta Was this translation helpful? Give feedback.
-
Any updates here with Next 12? What's the best way to have a good CSP without 'unsafe-inline' and 'unsafe-eval' |
Beta Was this translation helpful? Give feedback.
-
Check out this blog solution as it may help:
https://reesmorris.co.uk/blog/implementing-proper-csp-nextjs-styled-components
…On Thu, Jan 6, 2022 at 4:46 PM Dan Stroot ***@***.***> wrote:
Any updates here with Next 12? What's the best way to have a good CSP
without 'unsafe-inline' and 'unsafe-eval'
—
Reply to this email directly, view it on GitHub
<#18451 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC6AI6FGHDORMW7C755TZX3UUYZWFANCNFSM43U2WTYA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
SOLUTION FOR CLIENT RENDERED APPS Got this working with middleware and getInitialProps. You only have to SSR the pages/_middleware.js
pages/_app.js
|
Beta Was this translation helpful? Give feedback.
-
Hey folks, wanted to swing back here with an update. After digging through many different issues and discussions, I've made a new page in the documentation (PR) specifically for Content Security Policy and nonces. This docs page:
Further, we've patched some bugs and made improvements to Really hope this helps out, thank you all 🙏 I'll be closing this discussion out. To continue the discussion, please go here. |
Beta Was this translation helpful? Give feedback.
-
Feature request
Is your feature request related to a problem? Please describe.
Recent versions of Next are using inline styles, which break our apps because we block
style-src: unsafe-inline
in ourContent-Security-Policy
header. In fact, the default behaviour of any CSP is to block unsafe inline styles. While the risk may be less than with unsafe inline scripts, there is still a risk: see this StackOverflow answer.We have extremely high security requirements at my company so it's not really a matter of easily being able to disable
unsafe-inline
, and I'm aware that the future intention is to move more of the styling inline. If we can't upgrade Next because of this, we'll either be stuck on an old version and potentially vulnerable that way, or we'll have to consider alternatives (which I really don't want to do!).Describe the solution you'd like
Inline styles should have a hash or nonce which should be exposed as a global variable that we can inject into our CSP. A nonce is probably the easiest way forward. It will have to change on every request, so we'd need to have a way of generating this on the server side.
I can see that the
Head
component innext/document
accepts anonce
prop but this doesn't appear to apply to Next-generated inline styles.Describe alternatives you've considered
I'm not sure there are any real alternatives here given the intention to move most of the critical styling inline.
Beta Was this translation helpful? Give feedback.
All reactions