Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/audit-ignore.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
40 changes: 40 additions & 0 deletions .github/scripts/audit-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Fails when `npm audit` (production dependencies, lockfile only) reports a HIGH/CRITICAL advisory that is not
* listed in .github/audit-ignore.json. Run from the package directory:
* node <repo>/.github/scripts/audit-check.js <path-to-audit-ignore.json>
* Add an advisory id (GHSA-...) to the ignore file ONLY when no patched version exists, and say why in the commit.
*/
const {execSync} = require('child_process');
const fs = require('fs');

const ignoreFile = process.argv[2];
const ignored = new Set(ignoreFile && fs.existsSync(ignoreFile) ? JSON.parse(fs.readFileSync(ignoreFile, 'utf8')) : []);

let out;
try {
out = execSync('npm audit --package-lock-only --omit=dev --json', {encoding: 'utf8', maxBuffer: 64 * 1024 * 1024});
} catch (e) {
out = e.stdout; // npm audit exits non-zero when it finds anything
}
const report = JSON.parse(out);
if (report.error) {
console.error('npm audit failed:', report.error.summary || JSON.stringify(report.error));
process.exit(2);
}

const advisories = new Map();
for (const vuln of Object.values(report.vulnerabilities || {})) {
for (const via of vuln.via) {
if (typeof via !== 'object') continue; // string = transitive of another package
const id = (via.url || '').split('/').pop();
advisories.set(id || `${via.name}:${via.title}`, {id, name: via.name, severity: via.severity, title: via.title, range: via.range});
}
}
const bad = [...advisories.values()].filter((a) => ['high', 'critical'].includes(a.severity) && !ignored.has(a.id));
const skipped = [...advisories.values()].filter((a) => ['high', 'critical'].includes(a.severity) && ignored.has(a.id));
for (const a of skipped) console.log(`ignored (no fix available): ${a.id} ${a.name} - ${a.title}`);
if (bad.length) {
for (const a of bad) console.error(`::error::${a.severity.toUpperCase()} ${a.id} ${a.name} ${a.range} - ${a.title}`);
process.exit(1);
}
console.log(`ok: no unignored high/critical advisories in production dependencies (${advisories.size} total advisories)`);
31 changes: 31 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Dependency audit

# Fails on HIGH/CRITICAL advisories in production dependencies. Dev-only advisories (eslint,
# build tooling) are ignored via --omit=dev. Advisories with NO patched version can be listed in
# .github/audit-ignore.json (see .github/scripts/audit-check.js). Reads package-lock.json only: no
# install and no secrets, so it is safe on GitHub-hosted runners. Not a PR check on purpose: a
# Dependabot PR that fixes only part of the findings must still be mergeable.
on:
schedule:
- cron: '17 6 * * 1' # Mondays 06:17 UTC, so a new advisory fails the repo even with no pushes
workflow_dispatch:

permissions:
contents: read

jobs:
audit:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
dir: ["."]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 22
- name: Audit production dependencies
working-directory: ${{ matrix.dir }}
run: node "$GITHUB_WORKSPACE/.github/scripts/audit-check.js" "$GITHUB_WORKSPACE/.github/audit-ignore.json"
Loading
Loading