#!/usr/bin/env node /** * pnpm security:gate — the security suites that need no built site (docs/security.md → "Security suites"): * 1. dependency audit: `pnpm audit --prod` must show no high or critical advisory (the plugin's own production * dependency tree; dev tooling is reported, not failed); * 2. static rules over the plugin, its scripts and the reference site, every hit judged in * test/security-patterns.allow.json (test/securityPatterns.test.ts), and the per-class tests * (test/securityClasses.test.ts); * 3. optional, `--zap `: an OWASP ZAP baseline scan (passive, spiders 1 minute) of a running site, ONLY when * the image `ghcr.io/zaproxy/zaproxy:stable` is already on this machine — the scan runs offline (`-I` keeps * warnings from failing; failures fail). Pull the image once yourself: `docker pull ghcr.io/zaproxy/zaproxy:stable`. * The built-site part (attack probes, closed doors) is `pnpm e2e:built`. * Options: --offline (skip the audit, it needs the registry), --zap . */ import { spawnSync } from 'node:child_process' import path from 'node:path' import { fileURLToPath } from 'node:url' const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') const args = process.argv.slice(2) const zapAt = args.indexOf('--zap') const zapUrl = zapAt >= 0 ? args[zapAt + 1] : null const results = [] const step = (label, command, commandArgs, { allowFail = false } = {}) => { console.log(`\n[security:gate] ${label}`) const r = spawnSync(command, commandArgs, { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }) results.push({ label, ok: r.status === 0 || allowFail }) return r } if (!args.includes('--offline')) { const audit = step('dependency audit (pnpm audit --prod)', 'pnpm', ['audit', '--prod', '--json', '--ignore-workspace'], { allowFail: true }) let v = null try { v = JSON.parse(audit.stdout).metadata?.vulnerabilities ?? null } catch { // no JSON: the registry did not answer } if (!v) { console.error(` pnpm audit gave no result: ${(audit.stderr || audit.stdout).trim().split('\n')[0]}`) results.at(-1).ok = false } else { console.log(` production tree: ${v.critical ?? 0} critical, ${v.high ?? 0} high, ${v.moderate ?? 0} moderate, ${v.low ?? 0} low`) results.at(-1).ok = !(v.critical || v.high) const dev = spawnSync('pnpm', ['audit', '--json', '--ignore-workspace'], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 }) try { const d = JSON.parse(dev.stdout).metadata?.vulnerabilities console.log(` with dev tooling (reported, not failed): ${d.critical ?? 0} critical, ${d.high ?? 0} high, ${d.moderate ?? 0} moderate, ${d.low ?? 0} low`) } catch { // ignore } } } const tests = step('static rules + per-class tests (vitest)', 'pnpm', ['exec', 'vitest', 'run', 'test/securityPatterns.test.ts', 'test/securityClasses.test.ts']) process.stdout.write(tests.stdout.split('\n').filter((l) => /Tests |✓|×|FAIL|NOT JUDGED|MORE THAN/.test(l)).join('\n') + '\n') if (tests.status !== 0) {process.stdout.write(tests.stdout.slice(-4000))} if (zapUrl) { const image = 'ghcr.io/zaproxy/zaproxy:stable' const has = spawnSync('docker', ['image', 'inspect', image], { encoding: 'utf8' }) if (has.status !== 0) { console.log(`\n[security:gate] ZAP baseline skipped: ${image} is not on this machine (docker pull it once)`) } else { const target = zapUrl.replace('localhost', 'host.docker.internal').replace('127.0.0.1', 'host.docker.internal') const zap = step(`OWASP ZAP baseline of ${zapUrl}`, 'docker', ['run', '--rm', '--add-host=host.docker.internal:host-gateway', image, 'zap-baseline.py', '-t', target, '-m', '1', '-I', '--autooff']) process.stdout.write(zap.stdout.split('\n').filter((l) => /^(?:FAIL|WARN|PASS):|FAIL-NEW|WARN-NEW/.test(l)).slice(0, 80).join('\n') + '\n') } } console.log('\n[security:gate] ' + results.map((r) => `${r.ok ? '✓' : '✗'} ${r.label}`).join('\n[security:gate] ')) process.exit(results.every((r) => r.ok) ? 0 : 1)