--- name: csp-config-generator description: This skill should be used when the user requests to generate, create, or configure Content Security Policy (CSP) headers for Next.js applications to prevent XSS attacks and control resource loading. It analyzes the application to determine appropriate CSP directives and generates configuration via next.config or middleware. Trigger terms include CSP, Content Security Policy, security headers, XSS protection, generate CSP, configure CSP, strict CSP, nonce-based CSP, CSP directives. --- # CSP Config Generator To generate a strict Content Security Policy configuration for Next.js applications, follow these steps systematically. ## Step 1: Analyze Application Resources Identify all resource types used in the application. ### Discover External Resources Use Grep to find external resource references: **Scripts**: ``` - " `${name} ${values.join(' ')}`) .join('; ') } export const config = { matcher: [ { source: '/((?!api|_next/static|_next/image|favicon.ico).*)', missing: [ { type: 'header', key: 'next-router-prefetch' }, { type: 'header', key: 'purpose', value: 'prefetch' }, ], }, ], } ``` ### Update Root Layout with Nonce ```typescript // app/layout.tsx import { getNonce } from '@/lib/csp/nonce' export default function RootLayout({ children, }: { children: React.ReactNode }) { const nonce = getNonce() return ( {/* Head content will use nonce */} {children} ) } ``` ### Update Script Tags ```typescript // Use Next.js Script component with nonce import Script from 'next/script' import { getNonce } from '@/lib/csp/nonce' export function AnalyticsScript() { const nonce = getNonce() return (