-
Notifications
You must be signed in to change notification settings - Fork 27.3k
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
Missing nonce attribute in preinitialized scripts causing CSP violations in app router #54055
Comments
cc @gnoff @danieltott @timneutkens seems like CSP nonces are currently not working with Next.js in versions newer than |
This comment was marked as spam.
This comment was marked as spam.
@rinvii can confirm this as well. Although for the example app, I would add |
Related: #53928 |
Fixes #54055. A bug recently introduced in #53705 made it so that we were now preinitializing some of our scripts slightly better, but in doing so, we failed to pass in a nonce. This broke nonce-based CSP usage. The fix was to add the `nonce` to our `ReactDOM.preinit` calls. Manual testing shows that this change fixes the error and the nonce is now passed in as expected. Co-authored-by: Dan Ott <[email protected]>
Thanks for the #54059 PR, review, merge and followup tests @nbhargava @danieltott @shuding @gnoff 🙌 This was released in |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 19.6.0: Tue Jun 21 21:18:39 PDT 2022; root:xnu-6153.141.66~1/RELEASE_X86_64 Binaries: Node: 20.3.0 npm: 9.6.7 Yarn: 3.6.0 pnpm: 8.6.10 Relevant Packages: next: 13.4.16-canary.1 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.3 Next.js Config: output: N/A
Which area(s) of Next.js are affected? (leave empty if unsure)
App Router, Middleware / Edge (API routes, runtime)
Link to the code that reproduces this issue or a replay of the bug
https://github.com/rinvii/missing-nonce
To Reproduce
Set up a basic Next.js 13.4 App Router project.
middleware.ts
file in the root dirnpm run dev
Describe the Bug
When implementing a strict Content Security Policy via middleware as described in #43743 (comment), some script tags are missing a nonce value inside the head element.
I believe this bug was introduced in the PR #53705 where all but one of the required scripts for every page load are now currently preinitialized.
This seemed to have been resolved by the issues #43743 and #7486, but somehow reintroduced?
I noticed this when I was upgrading from 13.4.13 to 13.4.16. I managed to pinpoint this to 13.4.14-canary.2.
This happens both in dev and prod.
Expected Behavior
Inline scripts injected into the page should have a nonce attribute when using nonce for the CSP
script-src
directive.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
The text was updated successfully, but these errors were encountered: