-
Notifications
You must be signed in to change notification settings - Fork 259
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
Nextjs 15 strict-dynamic CSP doesn't add Nonce to Clerk Script #4455
Comments
I had this exact same issue. Turns out it's expected and you need to add |
Thanks, but it is said in the docs that this is not recommended: https://clerk.com/docs/references/nextjs/rendering-modes#make-auth-data-available-globally-with-clerk-provider-dynamic-not-recommended The dynamic prop does some more stuff that should not be needed when just wanting to pass down the nonce...? |
I think it has to do with clerk v6 moving from default dynamic rendering to default static rendering. If it's static rendered, then it can't get the nonce (because it's rendered at build time, not request time). Here's a PR that would have "fixed" the issue, but would also break static rendering For my particular use case I'm fine with having I think the documentation around this probably needs updating to provide better examples of having the provider around just the sidebar/header parts of the app |
Hi all, yes the Per the Next.js docs, your app must use dynamic rendering if a nonce is configured in this way (ref: https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy#adding-a-nonce-with-middleware) |
Hello everyone, I'm experiencing the same issue, and it's quite problematic in my case. Adding dynamic hasn’t helped much for me. Instead of making the RootLayout component async, which, by the way, is not recommended, I moved the ClerkProvider to a separate asynchronous component.: export default async function ClerkProviderWrapper({ children }: ClerkProviderWrapperProps) {
const nonce = await headers().then((headers) => headers.get('x-nonce') ?? '')
return (
<ClerkProvider
nonce={nonce}
localization={plPL}
appearance={{
variables: {
colorBackground: 'white',
colorInputBackground: '#ffb1b1',
colorText: '#09090a',
colorShimmer: '#e8b8b1',
},
}}
>
{children}
</ClerkProvider>
)
} I also followed the Clerk documentation to add the logic in my middleware.ts file, and that part works for me. However, the issue is that now all my pages have to be dynamic, which is not ideal. Out of the 19 pages in my app, 8 can be statically generated, and making everything dynamic could significantly increase my costs—especially since Vercel lambdas can be expensive. Is there an actual fix for this, or am I missing something? If possible, could you update the documentation? It’s currently outdated, as it still shows headers() as a synchronous function. I really appreciate all your hard work, especially with the recent v6 update—thank you for that! I'm looking forward to a quick fix or additional guidance in the docs. |
@wujekbizon A nonce is, by design, intended to only be used once. If you're using a CSP that contains a nonce generated in middleware, your application has to be dynamically rendered. This is a limitation of the framework, not something that is imposed by Clerk. Docs updates coming, thanks for calling that out! |
Preliminary Checks
I have reviewed the documentation: https://clerk.com/docs
I have searched for existing issues: https://github.com/clerk/javascript/issues
I have not already reached out to Clerk support via email or Discord (if you have, no need to open an issue here)
This issue is not a question, general help request, or anything other than a bug report directly related to Clerk. Please ask questions in our Discord community: https://clerk.com/discord.
Reproduction
https://github.com/lasseklovstad/clerk-csp-issue
Publishable key
pk_test_bG9naWNhbC1nYXRvci0xMi5jbGVyay5hY2NvdW50cy5kZXYk
Description
I tried folowing this guide to include dynamic csp headers: https://clerk.com/docs/security/clerk-csp#implementing-a-strict-dynamic-csp
Steps to reproduce:
npm install
andnpm run dev
I have hardcoded the clerk domain in the csp header for
connect-src
inmiddleware.ts
but that is not relevant for this issue. I have also tried adding the nonce explicit in the layout.tsx:But it still doesn't work.
Expected behavior:
The nonce should be added to the clerk script, but doesn't seem to work on nextjs 15. I tested on a Nextjs 14 version and that seemed to work.
Actual behavior:
I get csp error in chrome:
Environment
The text was updated successfully, but these errors were encountered: