nextjs - 💡(How to fix) Fix Docs: CSP guide causes DevTools inline style violations in development when using proxy-based nonce setup [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86188Fetched 2026-04-08 02:12:06
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Timeline (top)
closed ×1commented ×1issue_type_added ×1labeled ×1

Root Cause

From what I can tell, this is happening because the recommended strict CSP:

Code Example

Applying inline style violates the following Content Security Policy directive 
'style-src 'self' 'nonce-Yjc1MDhiY2UtYzg4Ny00OTc0LWI5ZWEtZWJmMTA5NjNmMDBj''. 
Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') 
is required to enable inline execution. The action has been blocked.

at devtool-style-inject.js
at dev-overlay.browser.tsx
...

---

script-src 'self' 'nonce-{nonce}' 'strict-dynamic';
style-src 'self' 'nonce-{nonce}';
RAW_BUFFERClick to expand / collapse

What is the documentation issue?

Hi team,

I’m following the Content Security Policy (CSP) guide for the App Router:

https://nextjs.org/docs/app/guides/content-security-policy

I’ve configured CSP in proxy.ts exactly as described in the docs (using a nonce and setting the Content-Security-Policy header from the proxy). I also tested a few variations of the policy (with and without strict-dynamic, different style-src and script-src settings, etc.).

However, in development mode I’m consistently seeing CSP violations coming from the Next.js DevTools / overlay and related dev scripts – not from my own application code.

For example, I get repeated errors like:

Applying inline style violates the following Content Security Policy directive 
'style-src 'self' 'nonce-Yjc1MDhiY2UtYzg4Ny00OTc0LWI5ZWEtZWJmMTA5NjNmMDBj''. 
Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') 
is required to enable inline execution. The action has been blocked.

at devtool-style-inject.js
at dev-overlay.browser.tsx
...

And similar messages for inline styles/scripts originating from:

  • devtool-style-inject.js
  • dev-overlay.browser.tsx
  • app-next-turbopack.ts
  • React dev/runtime files
  • Sometimes even from browser extensions (e.g. inpage.js)

From what I can tell, this is happening because the recommended strict CSP:

script-src 'self' 'nonce-{nonce}' 'strict-dynamic';
style-src 'self' 'nonce-{nonce}';

is not compatible with the way the DevTools and dev overlay inject inline styles/scripts in development — they don’t receive the nonce, so they are correctly blocked by CSP.

Is there any context that might help us understand?

What I think would help

Right now, the guide doesn’t clearly talk about how this interacts with development mode. As a developer, it’s a bit surprising to:

  • Follow the official CSP guide,
  • Set it up via proxy.ts with nonces,
  • And then immediately get flooded with CSP errors from the Next.js dev overlay / tooling.

I think the documentation could be clearer in one of these ways:

  1. Explicitly recommend enabling strict CSP only in production, e.g.:

    • Show examples that wrap the CSP logic in an if (process.env.NODE_ENV === 'production') check in proxy.ts,
    • And recommend testing CSP in production mode (next build && next start) rather than next dev.
  2. Or, alternatively, provide a dev-friendly CSP example, where:

    • Production uses the strict nonce-based policy from the guide,
    • Development relaxes style-src / script-src with unsafe-inline / unsafe-eval specifically to allow Next.js DevTools and overlay to function without constant CSP violations.

Something along those lines in the docs would prevent developers from being surprised by these warnings and make the intended usage pattern (prod vs dev) much clearer.


If it helps, I can share my current proxy.ts setup and a minimal reproduction, but the core issue seems to be the mismatch between strict CSP + nonces and how the DevTools inject inline styles/scripts in development.

Thanks!

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/app/guides/content-security-policy

extent analysis

TL;DR

To resolve the Content Security Policy (CSP) violations in development mode, consider relaxing the CSP rules for development environments by adding 'unsafe-inline' to the style-src and script-src directives.

Guidance

  • The issue arises from the strict CSP configuration not being compatible with how Next.js DevTools inject inline styles and scripts in development mode.
  • To mitigate this, you can modify your proxy.ts to conditionally apply the strict CSP policy only in production environments, using a check like if (process.env.NODE_ENV === 'production').
  • Alternatively, you can relax the CSP rules for development by adding 'unsafe-inline' to the style-src and script-src directives, but be aware this reduces security.
  • Consider testing your CSP configuration in production mode (next build && next start) rather than next dev to avoid these issues.

Example

if (process.env.NODE_ENV === 'production') {
  // Apply strict CSP policy
  res.setHeader(
    'Content-Security-Policy',
    `script-src 'self' 'nonce-${nonce}'; style-src 'self' 'nonce-${nonce}';`
  );
} else {
  // Relax CSP for development
  res.setHeader(
    'Content-Security-Policy',
    `script-src 'self' 'nonce-${nonce}' 'unsafe-inline'; style-src 'self' 'nonce-${nonce}' 'unsafe-inline';`
  );
}

Notes

  • Modifying the CSP to include 'unsafe-inline' for development environments reduces the security benefits of using a strict CSP.
  • It's essential to weigh the trade-offs between security and development convenience.

Recommendation

Apply a workaround by relaxing the CSP rules for development environments to minimize disruptions during development, with the understanding that this approach should be revisited for production deployment to ensure optimal security.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING