-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
139 lines (128 loc) · 3.52 KB
/
main.js
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const chalk = require('chalk');
const { exec } = require('child_process');
const yargs = require('yargs');
const { table } = require('table');
const fs = require('fs');
const { mapSeverityToColor, parseJson } = require('./utils');
const { argv } = yargs
.config('config')
.options({
a: {
alias: 'advisories',
default: [],
describe: 'Whitelisted advisory ids',
type: 'array'
},
ad: {
alias: '__advisories',
default: '',
describe: 'Whitelisted advisory reasons'
},
p: {
alias: 'pretty-print',
default: false,
describe: 'If set, pretty prints the logged output'
},
r: {
alias: 'registry',
default: undefined,
describe: 'The registry to resolve packages by name and version'
},
s: {
alias: 'severity',
default: 'moderate',
describe: 'Exit level of set severity vulnerabilities or higher',
choices: ['low', 'moderate', 'high', 'critical']
},
w: {
alias: 'whitelist',
default: [],
describe: 'Whitelisted module names',
type: 'array'
},
wd: {
alias: '__whitelist',
default: '',
describe: 'Whitelisted module reasons'
}
})
.help('help');
const advisories = argv.a.length > 0 ? `-a ${argv.a.join(' ')}` : '';
const advisoryReasons =
!!argv.ad &&
argv.a.length &&
Object.keys(argv.ad)
.map(a => ({ a, r: argv.ad[a] }))
.filter(Boolean);
const hasLogs = argv.l;
const isPretty = argv.p;
const registry = argv.r && `--registry ${argv.r}`;
const severity = `--${argv.s}`;
const showNotFound = `--show-not-found=false`;
const whitelist = argv.w.length > 0 ? `-w ${argv.w.join(' ')}` : '';
const whitelistReasons =
!!argv.wd &&
argv.w.length &&
Object.keys(argv.wd)
.map(w => ({ w, r: argv.wd[w] }))
.filter(Boolean);
const audit = exec(`audit-ci ${severity} ${advisories} ${whitelist} ${showNotFound} ${registry}`);
let tableData = [];
let errorData = [];
audit.stdout.on('data', data => {
const parsedData = parseJson(data.toString('utf8'));
if (isPretty) {
const { advisory, resolution } = parsedData;
if (advisory && resolution) {
const advisoryData = [
[chalk`{${mapSeverityToColor(advisory.severity)} ${advisory.severity} }`, advisory.title],
['Package', advisory.module_name],
['Path', resolution.path.split('>').join(' > ')],
['Patched in', advisory.patched_versions],
['More info', advisory.url]
];
tableData.push(advisoryData);
}
} else {
if (parsedData) {
console.log(data.toString('utf8'), '\n');
}
}
});
audit.stderr.on('data', data => {
const parsedData = data.toString('utf8');
if (isPretty) {
errorData.push([parsedData]);
} else {
console.log(parsedData);
}
});
audit.on('close', code => {
if (isPretty) {
const { dataTableConfig, errorTableConfig } = require('./utils');
tableData.forEach(tableToRender => {
console.log(table(tableToRender, dataTableConfig));
});
if (errorData.length > 0) {
console.log(table(errorData, errorTableConfig));
}
}
advisoryReasons &&
advisoryReasons.length &&
console.log(
chalk`{yellow These are the provided descriptions on why the advisories have been whitelisted:\n}`,
...advisoryReasons.map(({ a, r }) => `${a}: ${r}\n`)
);
whitelistReasons &&
whitelistReasons.length &&
console.log(
chalk`{yellow These are the provided descriptions on why the modules have been whitelisted:\n}`,
...whitelistReasons.map(({ w, r }) => `${w}: ${r}\n`)
);
if (code === 0) {
console.log(chalk`{green ✔ Passed the security audit.\n}`);
} else {
console.log(chalk`{red ✘ Please resolve the existing vulnerabilities.\n}`);
process.exit(1);
}
});