// Loads every page of a site in headless Chromium and records Content-Security-Policy violations, // then checks that the three interactive tools still work under the enforced policy. // Usage: BASE=http://127.0.0.1:8788 NODE_PATH=/path/to/node_modules node csp_check.mjs import { createRequire } from 'node:module'; const require = createRequire(process.env.PW_DIR ? process.env.PW_DIR + '/' : import.meta.url); const { chromium } = require('playwright'); const BASE = process.env.BASE || 'http://127.0.0.1:8788'; const sitemap = await (await fetch(BASE + '/sitemap-0.xml')).text(); const paths = [...sitemap.matchAll(/https?:\/\/[^/]+(\/[^<]*)<\/loc>/g)].map((m) => m[1]); const browser = await chromium.launch(); let violations = 0; const page = await browser.newPage(); const seen = []; page.on('console', (m) => { const t = m.text(); if (/Content Security Policy|Refused to/i.test(t)) seen.push(t); }); page.on('pageerror', (e) => seen.push('pageerror: ' + e.message)); for (const p of paths) { seen.length = 0; const r = await page.goto(BASE + p, { waitUntil: 'networkidle' }); const n = seen.length; violations += n; console.log(`${n === 0 ? 'ok ' : 'FAIL'} ${r.status()} ${p}${n ? ' <- ' + seen.join(' | ').slice(0, 200) : ''}`); } // Interactive tools under the enforced policy const checks = [ ['/tools/spf-dmarc-checker/', async () => { await page.fill('#rec', 'v=spf1 +all'); return (await page.textContent('#out')).includes('+all'); }], ['/tools/security-headers-checker/', async () => { await page.fill('#hdr', 'x-content-type-options: nosniff'); return (await page.textContent('#out')).includes('Strict-Transport-Security'); }], ['/tools/github-actions-cron/', async () => { const i = await page.$('input[type=text]'); await i.fill('*/5 * * * *'); await page.waitForTimeout(200); return (await page.textContent('body')).length > 0; }], ['/tools/subnet-calculator/', async () => (await page.textContent('body')).includes('/')], ]; for (const [p, fn] of checks) { seen.length = 0; await page.goto(BASE + p, { waitUntil: 'networkidle' }); const ok = await fn(); violations += seen.length; console.log(`${ok && !seen.length ? 'ok ' : 'FAIL'} tool works: ${p}`); if (!ok) violations++; } await browser.close(); console.log(`\n${paths.length} pages, ${violations} violation(s)/failure(s)`); process.exit(violations ? 1 : 0);