Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
One-time setup that gathers your project's design context and saves it to CLAUDE.md for future sessions.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/detect-csp.mjs
1/**2* Scan a project tree for Content-Security-Policy signals and classify the3* shape so the agent knows which patch template to propose.4*5* Used at first-time `live.mjs` setup. Mechanical (grep-based) — no network,6* no dev server, no JS evaluation. The classification drives a user-facing7* consent prompt; the agent does the actual patch writing.8*9* Shapes are named by patch mechanism, not framework origin:10* - "append-arrays": CSP defined as structured directive arrays. Patch11* appends a dev-only localhost entry. Covers:12* - Monorepo helpers with additional*Src options13* (e.g. createBaseNextConfig for Next)14* - SvelteKit kit.csp.directives15* - nuxt-security module's contentSecurityPolicy16* - "append-string": CSP built as a literal value string. Patch splices17* a dev-only token into script-src and connect-src.18* Covers:19* - Inline Next.js headers() with CSP string20* - Nuxt routeRules / nitro.routeRules CSP headers21* - "middleware": CSP set dynamically in middleware.{ts,js}.22* Detected but not auto-patched in v1.23* - "meta-tag": <meta http-equiv="Content-Security-Policy"> in24* layout files. Detected but not auto-patched in v1.25* - null: no CSP signals found; no patch needed.26*/2728import fs from 'node:fs';29import path from 'node:path';3031const SKIP_DIRS = new Set([32'node_modules',33'.git',34'.next',35'.turbo',36'.svelte-kit',37'.nuxt',38'.astro',39'dist',40'build',41'out',42'.vercel',43]);4445const SCAN_EXTS = new Set(['.js', '.mjs', '.cjs', '.ts', '.mts', '.cts', '.tsx', '.jsx']);46const LAYOUT_EXTS = new Set(['.tsx', '.jsx', '.astro', '.vue', '.svelte', '.html']);47const MAX_DEPTH = 6;48const MAX_READ_BYTES = 64 * 1024;4950// append-arrays signals: CSP expressed as structured directive arrays51const MONOREPO_HELPER_SIGNALS = [52/\bbuildCSPConfig\b/,53/\bbuildSecurityHeaders\b/,54/\badditionalScriptSrc\b/,55/\badditionalConnectSrc\b/,56/\bcreateBaseNextConfig\b/,57];58const SVELTEKIT_CSP_SIGNALS = [59/\bkit\s*:/,60/\bcsp\s*:/,61/\bdirectives\s*:/,62];63const NUXT_SECURITY_SIGNALS = [64/['"]nuxt-security['"]/,65/\bcontentSecurityPolicy\b/,66];6768// append-string signals: CSP written as a literal value string69const INLINE_HEADER_SIGNALS = [70/["']Content-Security-Policy["']/i,71/\bscript-src\b/,72/\bconnect-src\b/,73];74const NUXT_ROUTE_RULES_SIGNALS = [75/\brouteRules\b/,76/Content-Security-Policy/i,77/\bscript-src\b/,78];7980const MIDDLEWARE_HINT = /headers\.set\(\s*["']Content-Security-Policy["']/i;81const META_TAG_HINT = /http-equiv\s*=\s*["']Content-Security-Policy["']/i;8283/**84* @param {string} cwd Project root.85* @returns {{ shape: string|null, signals: string[] }}86*/87export function detectCsp(cwd = process.cwd()) {88const hits = { appendArrays: [], appendString: [], middleware: [], metaTag: [] };8990walk(cwd, cwd, 0, (absPath, relPath, body) => {91const ext = path.extname(absPath);92const base = path.basename(absPath).toLowerCase();93const isConfig = (name) =>94new RegExp('(^|/)' + name + '\\.config\\.').test(relPath);9596// === append-arrays candidates ===9798// Monorepo CSP helper: packages/*/src/.../(config|security)/*99if (SCAN_EXTS.has(ext) &&100/packages\/[^/]+\/src\/.*(config|next-config|security)/.test(relPath) &&101MONOREPO_HELPER_SIGNALS.some((re) => re.test(body))) {102hits.appendArrays.push(relPath);103return;104}105106// SvelteKit kit.csp.directives107if (SCAN_EXTS.has(ext) && isConfig('svelte') &&108SVELTEKIT_CSP_SIGNALS.every((re) => re.test(body))) {109hits.appendArrays.push(relPath);110return;111}112113// Nuxt nuxt-security module114if (SCAN_EXTS.has(ext) && isConfig('nuxt') &&115NUXT_SECURITY_SIGNALS.every((re) => re.test(body))) {116hits.appendArrays.push(relPath);117return;118}119120// === append-string candidates ===121122// Inline headers in Next/Nuxt/SvelteKit/Astro/Vite config123if (SCAN_EXTS.has(ext) &&124/(^|\/)(next|nuxt|vite|astro|svelte)\.config\./.test(relPath) &&125INLINE_HEADER_SIGNALS.every((re) => re.test(body))) {126// Nuxt routeRules is a sub-shape of append-string; we already covered127// nuxt-security above via return, so any remaining Nuxt CSP match here128// is a route-rules / inline-headers case. Either way, same patch129// mechanism.130hits.appendString.push(relPath);131return;132}133134// === detect-only shapes ===135136if ((base === 'middleware.ts' || base === 'middleware.js' || base === 'middleware.mjs') &&137MIDDLEWARE_HINT.test(body)) {138hits.middleware.push(relPath);139}140141if (LAYOUT_EXTS.has(ext) && META_TAG_HINT.test(body)) {142hits.metaTag.push(relPath);143}144});145146// Priority: append-arrays > append-string > middleware > meta-tag.147// Structured patches are safer than string splices; runtime and HTML148// injection patches are less reliable and v1 doesn't auto-apply them.149if (hits.appendArrays.length > 0) {150return { shape: 'append-arrays', signals: hits.appendArrays };151}152if (hits.appendString.length > 0) {153return { shape: 'append-string', signals: hits.appendString };154}155if (hits.middleware.length > 0) {156return { shape: 'middleware', signals: hits.middleware };157}158if (hits.metaTag.length > 0) {159return { shape: 'meta-tag', signals: hits.metaTag };160}161return { shape: null, signals: [] };162}163164function walk(root, dir, depth, visit) {165if (depth > MAX_DEPTH) return;166let entries;167try { entries = fs.readdirSync(dir, { withFileTypes: true }); }168catch { return; }169170for (const entry of entries) {171const abs = path.join(dir, entry.name);172if (entry.isDirectory()) {173if (SKIP_DIRS.has(entry.name)) continue;174walk(root, abs, depth + 1, visit);175continue;176}177if (!entry.isFile()) continue;178const ext = path.extname(entry.name);179if (!SCAN_EXTS.has(ext) && !LAYOUT_EXTS.has(ext)) continue;180let body;181try {182const fd = fs.openSync(abs, 'r');183try {184const buf = Buffer.alloc(MAX_READ_BYTES);185const n = fs.readSync(fd, buf, 0, MAX_READ_BYTES, 0);186body = buf.slice(0, n).toString('utf-8');187} finally { fs.closeSync(fd); }188} catch { continue; }189visit(abs, path.relative(root, abs), body);190}191}192193// CLI mode194const _running = process.argv[1];195if (_running?.endsWith('detect-csp.mjs') || _running?.endsWith('detect-csp.mjs/')) {196const result = detectCsp(process.cwd());197console.log(JSON.stringify(result, null, 2));198}199