fix(web): forward csp nonce to theme script

This commit is contained in:
yyh 2026-05-09 12:11:07 +08:00
parent 14cefd4674
commit 2d3133c654
No known key found for this signature in database
2 changed files with 14 additions and 8 deletions

View File

@ -5,7 +5,6 @@ import { Provider as JotaiProvider } from 'jotai/react'
import { ThemeProvider } from 'next-themes'
import { NuqsAdapter } from 'nuqs/adapters/next/app'
import AmplitudeProvider from '@/app/components/base/amplitude'
import { IS_PROD } from '@/config'
import { TanstackQueryInitializer } from '@/context/query-client'
import { getDatasetMap } from '@/env'
import { getLocaleOnServer } from '@/i18n-config/server'
@ -34,7 +33,7 @@ const LocaleLayout = async ({
}) => {
const locale = await getLocaleOnServer()
const datasetMap = getDatasetMap()
const nonce = IS_PROD ? (await headers()).get('x-nonce') ?? undefined : undefined
const nonce = (await headers()).get('x-nonce') ?? undefined
return (
<html lang={locale ?? 'en'} className="h-full" suppressHydrationWarning>

View File

@ -18,15 +18,16 @@ const wrapResponseWithXFrameOptions = (response: NextResponse, pathname: string)
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl
const requestHeaders = new Headers(request.headers)
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
const isWhiteListEnabled = !!env.NEXT_PUBLIC_CSP_WHITELIST && process.env.NODE_ENV === 'production'
if (!isWhiteListEnabled)
if (!isWhiteListEnabled) {
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
return wrapResponseWithXFrameOptions(response, pathname)
}
const whiteList = `${env.NEXT_PUBLIC_CSP_WHITELIST} ${NECESSARY_DOMAIN}`
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
@ -60,6 +61,12 @@ export function proxy(request: NextRequest) {
contentSecurityPolicyHeaderValue,
)
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
response.headers.set(
'Content-Security-Policy',
contentSecurityPolicyHeaderValue,