forked from snyk/snyk-to-html
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snyk-to-html.js
executable file
·169 lines (149 loc) · 4.21 KB
/
snyk-to-html.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env node
var fs = require("fs");
var Handlebars = require("handlebars");
var marked = require('marked');
var moment = require('moment');
var argv = require('minimist')(process.argv.slice(2));
var output, template, source;
if(argv['t']){ //template
template = argv['t']; //grab the next item
} else {
template = __dirname + "/template/test-report.hbs";
}
if(argv['i']){ //input source
source = argv['i']; //grab the next item
}
if(argv['o']){ //output destination
output = argv['o']; //grab the next item
}
var htmlTemplate = fs.readFileSync(template, 'utf8');
var hbTemplate = Handlebars.compile(htmlTemplate);
var severityMap = {low: 0, medium: 1, high: 2};
function metadataForVuln(vuln) {
return {
id: vuln.id,
title: vuln.title,
name: vuln.name,
info: vuln.info || 'No information available.',
severity: vuln.severity,
severityValue: severityMap[vuln.severity],
description: vuln.description || 'No description available.',
};
}
function groupVulns(vulns) {
var result = {};
if (!vulns || typeof vulns.length === 'undefined') {
return result;
}
for (var i = 0; i < vulns.length; i++) {
if (!result[vulns[i].id]) {
result[vulns[i].id] = {};
result[vulns[i].id].list = [];
result[vulns[i].id].metadata = metadataForVuln(vulns[i]);
}
result[vulns[i].id].list.push(vulns[i]);
}
return result;
}
function generateTemplate(data) {
data.vulnerabilities = groupVulns(data.vulnerabilities);
return hbTemplate(data);
}
function onDataCallback(data) {
var template = generateTemplate(data);
if (output) {
fs.writeFile(output, template, function(err) {
if (err) {
return console.log(err);
}
console.log('Vulnerability snapshot saved at ' + output);
});
} else {
console.log(template);
}
}
function readInputFromFile(source) {
fs.readFile(source, 'utf8', function(err, data) {
if (err) throw err;
onDataCallback(JSON.parse(data));
});
}
function readInputFromStdin() {
var data = '';
process.stdin.setEncoding('utf8');
process.stdin.on('readable', function() {
var chunk = process.stdin.read();
if (chunk !== null) {
data += chunk;
}
});
process.stdin.on('end', function() {
onDataCallback(JSON.parse(data));
});
}
function run() {
if (source) {
readInputFromFile(source, onDataCallback);
} else {
readInputFromStdin();
}
}
run();
// handlebar helpers
Handlebars.registerHelper('markdown', function (source) {
return marked(source);
});
Handlebars.registerHelper('moment', function (date, format) {
return moment.utc(date).format(format);
});
Handlebars.registerHelper('isDoubleArray', function (data, options) {
return Array.isArray(data[0]) ? options.fn(data) : options.inverse(data);
});
Handlebars.registerHelper('if_eq', function (a, b, opts) {
return (a === b) ? opts.fn(this) : opts.inverse(this);
});
Handlebars.registerHelper('count', function (data) {
return data && data.length;
});
Handlebars.registerHelper('dump', function (data, spacer) {
return JSON.stringify(data, null, spacer || null);
});
Handlebars.registerHelper('if_any', function () { // important: not an arrow fn
var args = [].slice.call(arguments);
var opts = args.pop();
return args.some(function(v) {return !!v;}) ?
opts.fn(this) :
opts.inverse(this);
});
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==': {
return (v1 == v2) ? options.fn(this) // jshint ignore:line
: options.inverse(this);
}
case '===': {
return (v1 === v2) ? options.fn(this) : options.inverse(this);
}
case '<': {
return (v1 < v2) ? options.fn(this) : options.inverse(this);
}
case '<=': {
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
}
case '>': {
return (v1 > v2) ? options.fn(this) : options.inverse(this);
}
case '>=': {
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
}
case '&&': {
return (v1 && v2) ? options.fn(this) : options.inverse(this);
}
case '||': {
return (v1 || v2) ? options.fn(this) : options.inverse(this);
}
default: {
return options.inverse(this);
}
}
});