From e4d6f05ba39f3d5ac84ac4d17034db7640bf70e1 Mon Sep 17 00:00:00 2001
From: Joseph-1-Duro
Date: Mon, 29 Jun 2026 18:01:32 +0100
Subject: [PATCH] feat: add CSP nonce-based script allowlist to block injected
scripts
---
src/app/layout.tsx | 10 ++++++----
src/middleware.ts | 5 ++++-
src/middleware/csp.ts | 14 +++++---------
3 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index 9eca3786..c07702de 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -1,5 +1,5 @@
import type { Metadata } from 'next';
-import { cookies } from 'next/headers';
+import { cookies, headers } from 'next/headers';
import { Geist, Geist_Mono } from 'next/font/google';
import Script from 'next/script';
import './globals.css';
@@ -49,9 +49,10 @@ export default async function RootLayout({
}: Readonly<{
children: React.ReactNode;
}>) {
- const cookieStore = await cookies();
+ const [cookieStore, headersList] = await Promise.all([cookies(), headers()]);
const themeCookie = cookieStore.get('theme');
const defaultTheme = themeCookie ? themeCookie.value : 'system';
+ const cspNonce = headersList.get('x-csp-nonce') ?? '';
// Read persisted locale to server-render the correct lang/dir on —
// avoids a hydration flash for RTL users.
@@ -80,7 +81,7 @@ export default async function RootLayout({
return (
-
+
)}
{process.env.NEXT_PUBLIC_ANALYTICS_ID && (
-