-
Notifications
You must be signed in to change notification settings - Fork 66
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
nonce is missing when rendering a <NuxtIsland> component during SSR #494
Comments
Hey @P4sca1 Thanks for reporting it. As you have already did some reasearch, maybe you would be interested in implementing a fix for that? :) Also CC @huang-julien |
The last time I wasn't able to fix the issue, due to missing information about how NuxtIsland rendering works internally. I think rendering a page during SSR with NuxtIsland components shares the ssrContext. I will investigate further using a minimal reproduction and work on a fix. |
@Baroshem I was able to add a test case, which reproduces the behavior. I can confirm that the root cause is, that Nuxt calls the I updated the nonce generation so that nonces are only generated if they do not already exist in the context. There may be other places in the code, where additional care needs to be taken if a context is shared between multiple requests / renders. When rendering the page with a server-only component, the CSP header already includes a script-src entry with a resolved nonce. However, already resolved nonces are filtered away in this line: |
I just noticed that the The NuxtIsland component will be rendered first. Rules are created for a path similar to |
Before implementing a fix, I created a discussion in the nuxt repo to verify if context sharing is expected behavior: |
I think we would need opinion from @vejja about it :) |
Will look at it |
<!--- Provide a general summary of your changes in the title above --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (a non-breaking change which fixes an issue) - [x] New feature (a non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Description <!--- Describe your changes in detail --> <!--- Why is this change required? What problem does it solve? --> <!--- If it resolves an open issue, please link to the issue here. For example "Resolves: #137" --> Closes #494 This PR introduces support for Nuxt Server Components (a.k.a Islands). ## Checklist: <!--- Put an `x` in all the boxes that apply. --> <!--- If your change requires a documentation PR, please link it appropriately --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes (if not applicable, please state why)
I'm proposing a very simple fix in PR #500 It is only a tentative proposal at this stage, because I do not use Islands myself, so I'm not very knowledgeable of all potential edge cases. @P4sca1 would you like to test the fix, and also add some tests to verify if it works ? |
That is a very valid remark. At first sight it looks like it's a legacy from the old code base when we didn't have the security context. I will need to double-check but this might have become superfluous and we should probably remove. Update: So it is code legacy, but still I'd like to keep it because it ensures that nobody can set a constant nonce via options |
It rings a bell. I remember I had a hard time working around what the context was supposed to be, and I figured out it's not the same thing if the request is coming from the client (browser) or if it's a server-generated request. Would love to know the answer of the Nuxt core team also |
Hey @vejja, thank you for the update. I already worked on a PR for this issue and published it in #502. Changes are similar to your PR. Additional things that are included:
My PR does not include the dependency updates, the changes to |
Hey @P4sca1 |
Released in 2.0.0 :) |
Version
nuxt-security: 2.0.0-rc.9
nuxt: 3.12.3
Reproduction Link
https://stackblitz.com/edit/github-5zfscl?file=pages%2Findex.vue
Steps to reproduce
Check the network log for the initial document request. You will see that the nonce- is missing from the script-src.
What is Expected?
The nonce- should be there. You can remove the
<ServerComponent />
and reload the page. The nonce will be there again.What is actually happening?
I added some loggings and it seems like the rendering of the page and the server component share the nonce. The nonce is then removed when rendering the page, because it already exists.
The text was updated successfully, but these errors were encountered: