From e732e0e6393a14a786bf6ce87fa3da32a2d470f8 Mon Sep 17 00:00:00 2001 From: Trevi Awater Date: Tue, 10 Nov 2026 22:24:31 +0100 Subject: [PATCH] feat: CSP en security.txt (#4) De domeinaudit (internet.nl-criteria) mist op pocketgraph.app een afgedwongen Content-Security-Policy en een security.txt. De Cloudflare-zonekant staat in resoftware/resoftware-iac#472; dit regelt de landing zelf. scripts/generate-headers.js draait na astro build en schrijft dist/_headers met een strikte CSP: inline scripts en styles via SHA-256-hashes, geen 'unsafe-inline'. Externe bronnen: rybbit.resoftware.work (script + connect) en Google Fonts (style + font). Claude-Session: https://claude.ai/code/session_01GsYFZ4cfxji4XFLURUpmyt Co-authored-by: Claude Opus 5.5 --- landing/.gitignore | 3 + landing/package.json | 2 +- landing/public/.well-known/security.txt | 4 + landing/scripts/generate-headers.js | 132 ++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 landing/public/.well-known/security.txt create mode 100644 landing/scripts/generate-headers.js diff --git a/landing/.gitignore b/landing/.gitignore index 16d54bb..23a3a5c 100644 --- a/landing/.gitignore +++ b/landing/.gitignore @@ -22,3 +22,6 @@ pnpm-debug.log* # jetbrains setting folder .idea/ + +# wrangler pages dev +.wrangler/ diff --git a/landing/package.json b/landing/package.json index a7ea89d..69204f0 100644 --- a/landing/package.json +++ b/landing/package.json @@ -7,7 +7,7 @@ }, "scripts": { "dev": "astro dev", - "build": "astro build", + "build": "astro build && node scripts/generate-headers.js dist", "preview": "astro preview", "astro": "astro" }, diff --git a/landing/public/.well-known/security.txt b/landing/public/.well-known/security.txt new file mode 100644 index 0000000..0cced63 --- /dev/null +++ b/landing/public/.well-known/security.txt @@ -0,0 +1,4 @@ +Contact: mailto:t.awater@resoftware.nl +Expires: 2027-11-10T00:00:00.000Z +Preferred-Languages: nl, en +Canonical: https://pocketgraph.app/.well-known/security.txt diff --git a/landing/scripts/generate-headers.js b/landing/scripts/generate-headers.js new file mode 100644 index 0000000..1bca69f --- /dev/null +++ b/landing/scripts/generate-headers.js @@ -0,0 +1,132 @@ +#!/usr/bin/env node +/** + * Security headers generator for Cloudflare Pages + * + * Runs after the build and writes /_headers with a Content-Security-Policy. + * Inline scripts are allowed by their SHA-256 hash instead of 'unsafe-inline'; + * the hashes are recomputed from the built HTML on every build. + * + * Styles: when the HTML still has inline `style` attributes (which cannot be + * hashed), the enforced policy allows 'unsafe-inline' for styles only, and a + * Report-Only policy with the strict style rules shows in the browser console + * what would break. Once the console is clean, set STRICT_STYLES to true. + * + * Fails the build on `on*="…"` attributes: the enforced CSP blocks them. + */ + +import { createHash } from 'crypto'; +import { readFileSync, readdirSync, statSync, writeFileSync } from 'fs'; +import { join, relative } from 'path'; + +const DIST_DIR = process.argv[2] || 'dist'; +const STRICT_STYLES = true; + +// External origins the site actually uses +const ORIGINS = { + // Rybbit analytics + script: ['https://rybbit.resoftware.work'], + style: ['https://fonts.googleapis.com'], + font: ['https://fonts.gstatic.com'], + img: [], + media: [], + connect: ['https://rybbit.resoftware.work'], + frame: [], + formAction: [], +}; + +function getHtmlFiles(dir) { + const files = []; + for (const entry of readdirSync(dir)) { + const fullPath = join(dir, entry); + if (statSync(fullPath).isDirectory()) { + files.push(...getHtmlFiles(fullPath)); + } else if (entry.endsWith('.html')) { + files.push(fullPath); + } + } + return files; +} + +function sha256(content) { + return `'sha256-${createHash('sha256').update(content, 'utf8').digest('base64')}'`; +} + +const scriptHashes = new Set(); +const styleHashes = new Set(); +const handlerViolations = []; +let styleAttributes = 0; + +for (const file of getHtmlFiles(DIST_DIR)) { + const html = readFileSync(file, 'utf8'); + + for (const [, attrs, body] of html.matchAll(/]*)>([\s\S]*?)<\/script>/gi)) { + if (/\bsrc\s*=/i.test(attrs) || body.length === 0) continue; + if (/type\s*=\s*["']?application\/(ld\+)?json/i.test(attrs)) continue; + scriptHashes.add(sha256(body)); + } + + for (const [, body] of html.matchAll(/]*>([\s\S]*?)<\/style>/gi)) { + styleHashes.add(sha256(body)); + } + + styleAttributes += [...html.matchAll(/<[a-z][a-z0-9-]*\b[^>]*\sstyle\s*=\s*["']/gi)].length; + + for (const [match] of html.matchAll(/<[a-z][a-z0-9-]*\b[^>]*\son[a-z]+\s*=\s*["'][^>]*>/gi)) { + handlerViolations.push(`${relative(DIST_DIR, file)}: ${match.slice(0, 120)}`); + } +} + +if (handlerViolations.length > 0) { + console.error('Inline on* event handlers found; these are blocked by the CSP:'); + for (const violation of handlerViolations) console.error(` ${violation}`); + console.error('Use addEventListener in a