Replies: 2 comments 3 replies
-
I'm unclear if what follows will solve your problem, as I'm only working in development mode at this point, but take a look, because it does work for me. My method is to generate the nonce in next.config.js and set it as an environment variable within the app. It can then be retrieved in _document.js, where it needs to be passed to This works for Next.js 12. I haven't yet cracked how to make it work for Next.js 13, as next.config.js:
_document.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.
-
Describe the feature you'd like to request
I would like for it to be possible to implement a strict content security policy with Next.js
In a strict CSP implementation, a nonce value will be generated with each request server side and injected into the policy in the response headers, style and script tags.
Usually, it flows like this.
The client makes a request to the server
The server responds with some headers including a CSP header that might look like this
This nonce value should also be passed to the client so it can inject into the tags that require it, note that this nonce value has to be unique on each request.

In order to have Webpack inject this nonce value into the style tags when it runs
insertStyleElement
, we need to include on the top of the entry file
__webpack_nonce__ = "OWI0NDlhMTItMTIwNC00ODI0LTlmOWYtZGQ3YjZhNjNlMTQz"
The issue I'm having is that when the page loads or reloads the value on the server side will always be the same so the response header nonce will always be the same but the value assigned to
__webpack_nonce__
is dynamic .I've created this repo which has a basic setup and demonstrates the issue with the nonce not being refreshed server side.
https://github.com/RaffysWeb/csp-demo
Webpack integration
https://webpack.js.org/guides/csp/
Basic repo with desired behaviour
https://github.com/RaffysWeb/csp-demo
Strict CSP policy information
https://csp.withgoogle.com/docs/strict-csp.html
Describe the solution you'd like
Ideally, we would have some sort of middleware that would generate a unique nonce value on each request so we can inject it into different pages.
nonce_generator.js
_app.js
_document.js
some-component.jsx
Describe alternatives you've considered
I considered using an
unsafe-hash
approach and generating and injecting hashes at run time.This is not ideal since we will not be able to differentiate between a legitimate script or style element or a malicious stored cross-site scripting payload greatly diminishing the efficacy of the CSP
Beta Was this translation helpful? Give feedback.
All reactions