@@ -11,12 +11,16 @@ import { NextRequest, NextResponse } from 'next/server'
1111 */
1212export function proxy ( request : NextRequest ) {
1313 const nonce = Buffer . from ( crypto . randomUUID ( ) ) . toString ( 'base64' )
14- const isDev = process . env . NODE_ENV === 'development'
14+ // Some Next.js runtimes (or local dev setups) may not provide NODE_ENV reliably.
15+ // Treat anything that's not explicitly production as "dev-like" so local debugging
16+ // doesn't break CSP for inline styles.
17+ const isProduction = process . env . NODE_ENV === 'production'
18+ const isDevLike = ! isProduction
1519 const proto =
1620 request . headers . get ( 'x-forwarded-proto' ) ?? request . nextUrl . protocol . replace ( ':' , '' )
1721 const isHttps = proto === 'https'
1822 // Only add upgrade-insecure-requests when actually served over HTTPS.
19- const upgradeInsecure = ! isDev && isHttps ? 'upgrade-insecure-requests;' : ''
23+ const upgradeInsecure = ! isDevLike && isHttps ? 'upgrade-insecure-requests;' : ''
2024
2125 // Build CSP header with nonce for script and style sources
2226 // Development: Allow unsafe-eval for Next.js hot reload, unsafe-inline for styles (no nonce for styles)
@@ -26,11 +30,15 @@ export function proxy(request: NextRequest) {
2630 // In dev, we skip style nonce to allow HMR style injection.
2731 const cspHeader = `
2832 default-src 'self';
29- script-src 'self' 'nonce-${ nonce } ' 'strict-dynamic' https://www.googletagmanager.com ${ isDev ? "'unsafe-eval'" : '' } ;
30- style-src 'self' ${ isDev ? "'unsafe-inline'" : `'nonce-${ nonce } '` } https://fonts.googleapis.com;
33+ script-src 'self' 'nonce-${ nonce } ' 'strict-dynamic' https://www.googletagmanager.com ${
34+ isDevLike ? "'unsafe-eval'" : ''
35+ } ;
36+ style-src 'self' ${ isDevLike ? "'unsafe-inline'" : `'nonce-${ nonce } '` } https://fonts.googleapis.com;
3137 font-src 'self' https://fonts.gstatic.com;
3238 img-src 'self' data: https: https://www.google-analytics.com;
33- connect-src 'self' https://www.google-analytics.com https://*.google-analytics.com https://www.googletagmanager.com https://auth.pingone.com ${ isDev ? 'ws://localhost:* http://localhost:*' : '' } ;
39+ connect-src 'self' https://www.google-analytics.com https://*.google-analytics.com https://www.googletagmanager.com https://auth.pingone.com ${
40+ isDevLike ? 'ws://localhost:* http://localhost:*' : ''
41+ } ;
3442 frame-src 'none';
3543 child-src 'none';
3644 worker-src 'self';
0 commit comments