95 lines
5.1 KiB
JavaScript
95 lines
5.1 KiB
JavaScript
#!/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` must show no moderate, high or critical advisory, in the production tree AND
|
||
* the dev tooling (since 1.18.0; lows are reported). `--audit-level high` fails only on high and critical again
|
||
* (`low` fails on lows too); `--prod-only` judges the production tree and only reports the dev tooling;
|
||
* 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 <url>`: 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), --audit-level <low|moderate|high|critical> (default
|
||
* moderate), --prod-only, --zap <url>.
|
||
*/
|
||
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 levelAt = args.indexOf('--audit-level')
|
||
const LEVELS = ['low', 'moderate', 'high', 'critical']
|
||
const level = levelAt >= 0 ? args[levelAt + 1] : 'moderate'
|
||
if (!LEVELS.includes(level)) {
|
||
console.error(`[security:gate] --audit-level must be one of ${LEVELS.join(', ')}, got ${JSON.stringify(level)}`)
|
||
process.exit(2)
|
||
}
|
||
const prodOnly = args.includes('--prod-only')
|
||
/** The severities that fail the gate: the chosen level and everything above it. */
|
||
const failing = LEVELS.slice(LEVELS.indexOf(level))
|
||
const fails = (v) => failing.some((s) => (v?.[s] ?? 0) > 0)
|
||
const counts = (v) => `${v.critical ?? 0} critical, ${v.high ?? 0} high, ${v.moderate ?? 0} moderate, ${v.low ?? 0} low`
|
||
|
||
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, fails on ${failing.join(' / ')}${prodOnly ? ', production tree only' : ''})`, '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: ${counts(v)}`)
|
||
results.at(-1).ok = !fails(v)
|
||
const dev = spawnSync('pnpm', ['audit', '--json', '--ignore-workspace'], { cwd: root, encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 })
|
||
let d = null
|
||
try {
|
||
d = JSON.parse(dev.stdout).metadata?.vulnerabilities ?? null
|
||
} catch {
|
||
// no JSON: the registry did not answer
|
||
}
|
||
if (!d) {
|
||
console.error(' pnpm audit (with dev tooling) gave no result')
|
||
if (!prodOnly) {results.at(-1).ok = false}
|
||
} else {
|
||
console.log(` with dev tooling${prodOnly ? ' (reported, not failed: --prod-only)' : ''}: ${counts(d)}`)
|
||
if (!prodOnly && fails(d)) {results.at(-1).ok = false}
|
||
}
|
||
}
|
||
}
|
||
|
||
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)
|