-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
2 cases where nonce
isn't applied for Content-Security-Policy
#21925
Comments
Any word on this issue? It's been more than a month :( I could understand not doing the |
I am also suffering from this issue, in fact the nonce doesn't seem to be working at all. However I believe you need to pass the nonce into your CSP header, for example:
I am unsure how you would generate the nonce in This apparently works, however I cant get it working. Refreshing the page multiple times will cause a So although your implementation may be wrong, I believe passing the nonce into the CSP header wont fix the issue. |
@alex-r89 Although, it won't change anything. Even with the As for the link you posted, it's unfortunately only for SSR (server-side rendering). It won't work for SSG (static site generation). Generating the I can always use |
@TheThirdRace ah yes, that makes sense. Have you managed to fix the issue using a Indeed, I am also now running into this issue and have noticed a number of people have spent a long time trying to get this working, to avail. |
@alex-r89 As mentioned, I did use the Even though I "fixed" those troubles, it doesn't matter in the end. I still can't generate a |
any news about this ? i also noticed, using a custom headers in next.config with
the html output dont renders the nonce value from tested on |
@moigithub But yeah, the 2 styles tags still don't have the |
im viewing it on firefox, didnt check on chrome |
@moigithub If you're using a CSP, it's 100% sure that a If you're NOT using a CSP, how the heck is the browser is supposed to know if that Glad to know Firefox is also hiding the |
Please verify that your issue can be recreated with Why was this issue marked with the
|
Closing this issue:
While I'm not exactly sure the new |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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. |
What version of Next.js are you using?
10.0.6
What version of Node.js are you using?
12.18.2
What browser are you using?
Chrome
What operating system are you using?
Windows 10 (latest)
How are you deploying your application?
Vercel
Describe the Bug
What's the bug?
When trying to use a
Content-Security-Policy
withstyle-src 'self' 'nonce-r@nd0m'
, there are 2 cases where thenonce
is not applied.Because of these, we're forced to relax the
Content-Security-Policy
to useinline-unsafe
.Case 1:
fouc
inline<style>
tagNot sure what
fouc
is (flash of unstyled content?), but those styles don't have thenonce
:Workaround
Because those styles are "fixed", it's possible to use the
sha-
tag to pass to theContent-Security-Policy
and avoid usinginline-unsafe
.This is far from ideal given any changes on NextJs side implies a change in the
Content-Security-Policy
. A permanent solution is needed.Case 2: Next/Image inline
style=
propThe use of
styles=
prop directly on the wrapper elements and the img element themselves is simply incompatible with theContent-Security-Policy
.I've been vocal lately about how much the inline styles are completely screwing us over for styling the Next/Image component. The
Content-Security-Policy
is is just another reason not to rely on inline styles.The styling of Next/Image needs to be refactored, it's way too restrictive and problematic. See #18585, I made 3 comments there that explains pretty much all the current problems with styling, including the
Content-Security-Policy
.Workaround
None whatsoever.
We're stuck using
inline-unsafe
in ourContent-Security-Policy
.Expected Behavior
Case 1:
fouc
inline<style>
tagThere are examples out there where you can simply pass the
nonce
to the<Head>
component:I would expect the
nonce
to be applied to those 2<style>
tags like it is to all the other<style>
tags from NextJs.Case 2: Next/Image inline
style=
propNo use of
styles=
prop directly on the wrapper elements and the img element themselves.I don't see anything else that could solve the problem for both the
Content-Security-Policy
and the styling problems experienced by most developers.This might require a refactor of how
Next/Image
actually works. Maybe have a component that only manage the nuts and bolts (lazy loading, placeholder, srcset, etc.), not the styling itself. Have another component take care of styling if need be, as long as it's not inline...To Reproduce
Run a normal NextJs project.
In
next.config.js
:Add a Next/Image to your content:
Open the console in dev tools and refresh the page, you'll see the errors of styles being blocked
Edit: On 2021-10-22
I've read in another issue that the 2
<style>
tags for "fouc" are only added in DEV. This means the CSP problem is not so bad given it doesn't happen in PROD.As for the inline styles in
next/image
, I think they could be moved tostyled Jsx
method and work with the CSP. I haven't tried it myself because there was many other problems with usingnext/image
. That's why I made my own implementation using Chakra-ui, which bypassed all the inline style problems. That's why I'm thinking it should be possible to move all inline-styles tostyled-jsx
as NextJs include it by default and it does the same as EmotionJs does for Chakra.The one thing I haven't solved is to create a
nonce
in SSG. My current thinking is to use a Cloudflare Worker with theHTMLRewriter
plugin to overwrite thenonce
on the static file just before it's served to the browser. I'm 99% confident this would work.This also begs the question if NextJs could do the same with its platform?
Technically, they already do
fallback
modes for pages andincremental
rebuilds so they could probably use the same techniques to replace anonce
with a unique value on each load.In any case, as long as
next/image
is still using inline styles, this ticket stays valid.The text was updated successfully, but these errors were encountered: