-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaudit-gate.js
More file actions
80 lines (65 loc) · 1.78 KB
/
Copy pathaudit-gate.js
File metadata and controls
80 lines (65 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const { execSync } = require('child_process');
const FAIL_SEVERITIES = new Set(['critical']);
function parseAuditOutput(output) {
const counts = {
critical: 0,
high: 0,
moderate: 0,
low: 0,
info: 0,
};
for (const line of output.split('\n')) {
if (!line.trim()) {
continue;
}
try {
const entry = JSON.parse(line);
if (entry.type === 'auditSummary' && entry.data?.vulnerabilities) {
return { ...counts, ...entry.data.vulnerabilities };
}
if (entry.type === 'auditAdvisory' && entry.data?.advisory?.severity) {
const { severity } = entry.data.advisory;
if (severity in counts) {
counts[severity] += 1;
}
}
} catch (error) {
// Ignore non-JSON lines from yarn audit output.
}
}
return counts;
}
function printSummary(counts) {
console.log('Yarn audit vulnerability summary (dependencies only):');
console.log(` critical: ${counts.critical}`);
console.log(` high: ${counts.high}`);
console.log(` moderate: ${counts.moderate}`);
console.log(` low: ${counts.low}`);
console.log(` info: ${counts.info}`);
}
function main() {
let output = '';
try {
output = execSync('yarn audit --groups dependencies --json', {
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
maxBuffer: 50 * 1024 * 1024,
});
} catch (error) {
output = `${error.stdout || ''}${error.stderr || ''}`;
}
const counts = parseAuditOutput(output);
printSummary(counts);
const hasBlockingVulnerabilities = [...FAIL_SEVERITIES].some(
(severity) => counts[severity] > 0,
);
if (hasBlockingVulnerabilities) {
console.error(
'\nAudit gate failed: critical severity vulnerabilities must be resolved.',
);
process.exit(1);
}
console.log('\nAudit gate passed: no critical severity vulnerabilities.');
process.exit(0);
}
main();