forked from hunt3rkillerz/Snyk-To-Sarif-JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snyk-to-sarif.js
333 lines (284 loc) · 9.38 KB
/
snyk-to-sarif.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env node
const fs = require('fs');
const { argv } = require('process');
const yargs = require('yargs');
//Verbosity Setting
let verbose = false
// Snyk severity
// https://support.snyk.io/hc/en-us/articles/4403987394961-Severity-levels
const SEVERITY = {
LOW: "low",
MEDIUM: "medium",
HIGH: "high",
CRITICAL: "critical"
}
// SARIF level
// https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json
const LEVEL = {
NONE: "none",
NOTE: "note",
WARNING: "warning",
ERROR: "error"
}
// Convert severity (snyk) to level (SARIF)
function severityToLevel(severity) {
switch (severity) {
case SEVERITY.LOW:
return LEVEL.NOTE;
case SEVERITY.MEDIUM:
return LEVEL.WARNING;
case SEVERITY.HIGH:
return LEVEL.ERROR;
case SEVERITY.CRITICAL:
return LEVEL.ERROR;
}
}
// Packages vulnerability and rule data into the main SARIF object structure
async function createSarif(vulnerabilities, rules) {
return {
$schema: "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json",
version: "2.1.0",
runs: [
{
tool: {
driver: {
name: "Snyk",
rules: Object.keys(rules).map(function (key) {
// Retreiving all entries in the dictionary
return rules[key];
}),
}
},
results: vulnerabilities,
}
]
}
}
async function convertIACProj(projectData) {
const ruleList = {};
const vulnList = [];
const vulnArr = projectData.infrastructureAsCodeIssues;
const affectedFile = projectData.targetFile;
// Scans a Snyk IaC File
for (let i = 0; i < vulnArr.length; i++) {
const vulnerability = vulnArr[i];
// Check if this is wanted as part of the results
if (vulnerability.isIgnored) {
continue;
}
let severityInRuleList = "warning";
let tags = [
"security",
// OWASP Top Ten 2017 Category A6 - Security Misconfiguration
"CWE-1032"
];
if (vulnerability.severity == "high") {
severityInRuleList = "error";
}
const formattedText = "## Overview\n"
+ vulnerability.iacDescription.issue
+ "\n\n## Impact\n\n" + vulnerability.iacDescription.impact
+ "\n\n## Remediation\n\n" + vulnerability.iacDescription.resolve;
ruleList[vulnerability.id] = {
id: vulnerability.id,
shortDescription: {
text: vulnerability.title
},
fullDescription: {
text: vulnerability.iacDescription.issue
},
help: {
markdown: formattedText,
text: "",
},
defaultConfiguration: {
level: severityInRuleList
},
properties: {
tags: tags
},
}
vulnList.push({
ruleId: vulnerability.id,
level: severityToLevel(vulnerability.severity),
message: {
text: vulnerability.title
},
locations: [
{
"physicalLocation": {
"artifactLocation": {
"uri": affectedFile,
},
"region": {
"startLine": vulnerability.lineNumber,
"startColumn": 1
}
}
}
],
});
}
return {
vulnerabilities: vulnList,
rules: ruleList,
};
}
// Converts Snyk Project data into a set of "Rules" and "Vulnerability" objects using SARIF templates
async function convertProject(projectData) {
// Check for IaC projects
if (projectData.infrastructureAsCodeIssues) {
return await convertIACProj(projectData)
}
const ruleList = {};
const vulnList = [];
const vulnArr = projectData.vulnerabilities;
const affectedFile = projectData.displayTargetFile;
// Scans a Snyk Dependencies File
for (let i = 0; i < vulnArr.length; i++) {
const vulnerability = vulnArr[i];
let severityInRuleList = "warning";
let tags = [
"security"
];
// Make sure the vuln does have identifiers
if (vulnerability.identifiers) {
if ("CWE" in vulnerability.identifiers) {
tags = vulnerability.identifiers.CWE.concat(tags);
}
}
if (vulnerability.severity == "high") {
severityInRuleList = "error";
}
ruleList[vulnerability.id] = {
id: vulnerability.id,
shortDescription: {
text: vulnerability.title + " - " + vulnerability.packageName
},
fullDescription: {
text: "The dependency " /
+ vulnerability.packageName
+ " introduces a "
+ vulnerability.title
+ " vulnerability",
},
help: {
markdown: vulnerability.description,
text: "",
},
defaultConfiguration: {
level: severityInRuleList
},
properties: {
tags: tags
},
}
vulnList.push({
ruleId: vulnerability.id,
level: severityToLevel(vulnerability.severity),
message: {
text: "This adds a vulnerable dependency "
+ vulnerability.packageName
+ " which introduces a "
+ vulnerability.severity
+ " severity security flaw",
},
locations: [
{
"physicalLocation": {
"artifactLocation": {
"uri": affectedFile,
}
}
}
],
});
}
return {
vulnerabilities: vulnList,
rules: ruleList,
};
}
async function squashMultiProject(snykData) {
let mergedRules = {};
let vulnerabilityList = [];
// Process the data
for (let i = 0; i < snykData.length; i++) {
// Grab the vulns and rules out of the project
const { vulnerabilities, rules } = await convertProject(snykData[i]);
// Merge in the vulns into our list
vulnerabilityList = vulnerabilityList.concat(vulnerabilities);
// Merge the rules in, ignoring duplicates
mergedRules = Object.assign({}, mergedRules, rules);
}
return await createSarif(vulnerabilityList, mergedRules);
}
function printHelp() {
console.log(`
Snyk to Sarif Options:
-i, --input | Allows an input snyk json file to be specified by the user
-o, --output | Allows a sarif output file path to be specified by the user
-v, --verbose | Prints additional debug information
-h, --help | Prints the help prompt
`);
}
async function run(data, outputFile) {
let snykJson, sarifData;
try {
snykJson = JSON.parse(data);
} catch (e) {
if (verbose) {
console.error(e);
}
console.error("The input data does not appear to be valid");
return;
}
if (Array.isArray(snykJson)) {
sarifData = await squashMultiProject(snykJson);
} else {
const { vulnerabilities, rules } = await convertProject(snykJson);
sarifData = await createSarif(vulnerabilities, rules);
}
if (outputFile) {
fs.writeFileSync(outputFile, JSON.stringify(sarifData));
} else {
console.log(JSON.stringify(sarifData));
}
}
async function processCommandLineArgs() {
let snykFile;
// Get command line args
const argv = yargs(process.argv.slice(2)).argv;
const inputFile = argv.i || argv.input;
const outputFile = argv.o || argv.output;
// Print the help prompt and end the program
if (argv.h != undefined || argv.help != undefined) {
printHelp()
return
}
// Toggle Verbosity
if (argv.v != undefined || argv.verbose != undefined) {
verbose = true;
}
// Checking if an input file is provided
if (inputFile) {
if (fs.existsSync(inputFile)) {
snykFile = fs.readFileSync(inputFile, 'utf8');
} else {
console.log("Provided input file does not exist");
return
}
run(snykFile, outputFile);
} else {
let data = ""
process.stdin.setEncoding('utf8');
process.stdin.on('data', function (chunk) {
data += chunk;
pipeData = true;
});
process.stdin.on('end', function () {
run(data, outputFile);
});
}
}
processCommandLineArgs();