Content Security Policy in app directory with preventing inline scripts #49348
-
SummaryI'm using app directory and I'd like to implement Content Security Policy that prevents inline scripts. What I noticed Next.js generates below code inside html (probably due to #30994). This inline scripts prevents from implementing Content Security Policy.
One solution would be to add nonce and it looks like Next.js code supports this ( ).This Is this correct? Shouldn't it read from next.config headers property where CSP can be specified? The workaround for above issue is to create middleware and set CSP header on the request. This works fine when I run locally as It looks like Static Rendering (which is default for app directory) during the build outputs these inline scripts without nonce (which is obvious as there is no request processing) causing that page is not working because CSP header blocks execution of inline scripts (without nonce). Is there a working solution to set Content Security Policy in app directory that prevents inline scripts? Or any other workaround for this problem? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
For dynamically rendered pages, this is possible using I'm not sure what the status of statically-rendered pages is - but also I don't think that statically-rendered pages can ever properly use See this comment for implementation for dynamically rendered pages: |
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.
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:
nonce
with Middlewarenonce
in a route withheaders()
unsafe
nonce
Middleware from running on prefetches / static assetsFurther, we've patched some bugs and made improvements to
nonce
handling in Next.js that will be available in the latestcanary
version (for those of you time traveling from the future, upgrade to Next.js 13.5). We also …