/
format-test-results.ts
359 lines (322 loc) · 10.6 KB
/
format-test-results.ts
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import {
Options,
OutputDataTypes,
SupportedProjectTypes,
TestOptions,
} from '../../types';
import {
getReachabilityJson,
summariseReachableVulns,
} from '../format-reachability';
import {
GroupedVuln,
SEVERITY,
TestResult,
VulnMetaData,
} from '../../snyk-test/legacy';
import chalk from 'chalk';
import * as config from '../../config';
const cloneDeep = require('lodash.clonedeep');
const orderBy = require('lodash.orderby');
import * as analytics from '../../analytics';
import { formatIssuesWithRemediation } from '../remediation-based-format-issues';
import { formatIssues } from '../legacy-format-issue';
import { formatDockerBinariesIssues } from '../docker';
import { createSarifOutputForContainers } from '../sarif-output';
import { createSarifOutputForIac } from '../iac-output';
import { isNewVuln, isVulnFixable } from '../../vuln-helpers';
import { jsonStringifyLargeObject } from '../../json';
import { createSarifOutputForOpenSource } from '../open-source-sarif-output';
import { getSeverityValue } from '../get-severity-value';
import { showFixTip } from '../show-fix-tip';
import {
DockerFileAnalysisErrorCode,
facts as dockerFacts,
} from 'snyk-docker-plugin/dist';
import { ScanResult } from '../../ecosystems/types';
export function formatJsonOutput(jsonData, options: Options) {
const jsonDataClone = cloneDeep(jsonData);
if (options['group-issues']) {
jsonDataClone.vulnerabilities = Object.values(
(jsonDataClone.vulnerabilities || []).reduce((acc, vuln): Record<
string,
any
> => {
vuln.from = [vuln.from].concat(acc[vuln.id]?.from || []);
vuln.name = [vuln.name].concat(acc[vuln.id]?.name || []);
acc[vuln.id] = vuln;
return acc;
}, {}),
);
}
if (jsonDataClone.vulnerabilities) {
jsonDataClone.vulnerabilities.forEach((vuln) => {
if (vuln.reachability) {
vuln.reachability = getReachabilityJson(vuln.reachability);
}
});
}
return jsonDataClone;
}
export function extractDataToSendFromResults(
results,
jsonData,
options: Options,
): OutputDataTypes {
let sarifData = {};
let stringifiedSarifData = '';
if (options.sarif || options['sarif-file-output']) {
if (options.iac) {
sarifData = createSarifOutputForIac(results);
} else if (options.docker) {
sarifData = createSarifOutputForContainers(results);
} else {
sarifData = createSarifOutputForOpenSource(results);
}
stringifiedSarifData = jsonStringifyLargeObject(sarifData);
}
let stringifiedJsonData = '';
if (options.json || options['json-file-output']) {
stringifiedJsonData = jsonStringifyLargeObject(
formatJsonOutput(jsonData, options),
);
}
const dataToSend = options.sarif ? sarifData : jsonData;
const stringifiedData = options.sarif
? stringifiedSarifData
: stringifiedJsonData;
return {
stdout: dataToSend, // this is for the human-readable stdout output and is set (but not used) even if --json or --sarif is set
stringifiedData, // this will be used to display either the Snyk or SARIF format JSON to stdout if --json or --sarif is set
stringifiedJsonData, // this will be used for the --json-file-output=<file.json> option
stringifiedSarifData, // this will be used for the --sarif-file-output=<file.json> option
};
}
export function createErrorMappedResultsForJsonOutput(results) {
const errorMappedResults = results.map((result) => {
// add json for when thrown exception
if (result instanceof Error) {
return {
ok: false,
error: result.message,
path: (result as any).path,
};
}
return result;
});
return errorMappedResults;
}
export function getDisplayedOutput(
res: TestResult,
options: Options & TestOptions,
testedInfoText: string,
localPackageTest: any,
projectType: SupportedProjectTypes,
meta: string,
prefix: string,
multiProjAdvice: string,
dockerAdvice: string,
): string {
const vulnCount = res.vulnerabilities && res.vulnerabilities.length;
const singleVulnText = res.licensesPolicy ? 'issue' : 'vulnerability';
const multipleVulnsText = res.licensesPolicy ? 'issues' : 'vulnerabilities';
// Text will look like so:
// 'found 232 vulnerabilities, 404 vulnerable paths.'
let vulnCountText =
`found ${res.uniqueCount} ` +
(res.uniqueCount === 1 ? singleVulnText : multipleVulnsText);
// Docker is currently not supported as num of paths is inaccurate due to trimming of paths to reduce size.
if (options.showVulnPaths && !options.docker) {
vulnCountText += `, ${vulnCount} vulnerable `;
if (vulnCount === 1) {
vulnCountText += 'path.';
} else {
vulnCountText += 'paths.';
}
} else {
vulnCountText += '.';
}
const reachableVulnsText =
options.reachableVulns && vulnCount > 0
? ` ${summariseReachableVulns(res.vulnerabilities)}`
: '';
const summary =
testedInfoText +
', ' +
chalk.red.bold(vulnCountText) +
chalk.blue.bold(reachableVulnsText);
const fixTip = showFixTip(projectType, res, options);
const fixAdvice = fixTip ? `\n\n${fixTip}` : '';
const dockerfileWarning = getDockerfileWarning(res.scanResult);
const dockerSuggestion = getDockerSuggestionText(options, config);
const vulns = res.vulnerabilities || [];
const groupedVulns = groupVulnerabilities(vulns);
const sortedGroupedVulns: GroupedVuln[] = orderBy(
groupedVulns,
['metadata.severityValue', 'metadata.name'],
['asc', 'desc'],
);
const filteredSortedGroupedVulns = sortedGroupedVulns.filter(
(vuln) => vuln.metadata.packageManager !== 'upstream',
);
const binariesSortedGroupedVulns = sortedGroupedVulns.filter(
(vuln) => vuln.metadata.packageManager === 'upstream',
);
let groupedVulnInfoOutput;
if (res.remediation) {
analytics.add('actionableRemediation', true);
groupedVulnInfoOutput = formatIssuesWithRemediation(
filteredSortedGroupedVulns,
res.remediation,
options,
);
} else {
analytics.add('actionableRemediation', false);
groupedVulnInfoOutput = filteredSortedGroupedVulns.map((vuln) =>
formatIssues(vuln, options),
);
}
const groupedDockerBinariesVulnInfoOutput =
res.docker && binariesSortedGroupedVulns.length
? formatDockerBinariesIssues(
binariesSortedGroupedVulns,
res.docker.binariesVulns,
options,
)
: [];
let body =
groupedVulnInfoOutput.join('\n\n') +
'\n\n' +
groupedDockerBinariesVulnInfoOutput.join('\n\n') +
'\n\n' +
meta;
if (res.remediation) {
body = summary + body + fixAdvice;
} else {
body = body + '\n\n' + summary + fixAdvice;
}
const ignoredIssues = '';
const dockerCTA = dockerUserCTA(options);
return (
prefix +
body +
multiProjAdvice +
ignoredIssues +
dockerAdvice +
dockerfileWarning +
dockerSuggestion +
dockerCTA
);
}
export function dockerUserCTA(options) {
if (options.isDockerUser) {
return '\n\nFor more free scans that keep your images secure, sign up to Snyk at https://dockr.ly/3ePqVcp';
}
return '';
}
function getDockerSuggestionText(options, config): string {
if (!options.docker || options.isDockerUser) {
return '';
}
let dockerSuggestion = '';
if (config && config.disableSuggestions !== 'true') {
const optOutSuggestions =
'\n\nTo remove this message in the future, please run `snyk config set disableSuggestions=true`';
if (!options.file) {
dockerSuggestion +=
chalk.bold.white(
'\n\nPro tip: use `--file` option to get base image remediation advice.' +
`\nExample: $ snyk test --docker ${options.path} --file=path/to/Dockerfile`,
) + optOutSuggestions;
} else if (!options['exclude-base-image-vulns']) {
dockerSuggestion +=
chalk.bold.white(
'\n\nPro tip: use `--exclude-base-image-vulns` to exclude from display Docker base image vulnerabilities.',
) + optOutSuggestions;
}
}
return dockerSuggestion;
}
function getDockerfileWarning(scanResult: ScanResult | undefined): string {
if (!scanResult) {
return '';
}
const fact = scanResult.facts.find(
(fact) => fact.type === 'dockerfileAnalysis',
);
if (!fact) {
return '';
}
const dockerfileAnalysisFact = fact as dockerFacts.DockerfileAnalysisFact;
if (!dockerfileAnalysisFact.data.error) {
return '';
}
let userMessage = chalk.yellow(
'\n\nWarning: Unable to analyse Dockerfile provided through `--file`.',
);
switch (dockerfileAnalysisFact.data.error.code) {
case DockerFileAnalysisErrorCode.BASE_IMAGE_NAME_NOT_FOUND:
userMessage += chalk.yellow(
'\n Dockerfile must begin with a FROM instruction. This may be after parser directives, comments, and globally scoped ARGs.',
);
break;
case DockerFileAnalysisErrorCode.BASE_IMAGE_NON_RESOLVABLE:
userMessage += chalk.yellow(
'\n Dockerfile must have default values for all ARG instructions.',
);
break;
}
return userMessage;
}
export function groupVulnerabilities(
vulns,
): {
[vulnId: string]: GroupedVuln;
} {
return vulns.reduce((map, curr) => {
if (!map[curr.id]) {
map[curr.id] = {};
map[curr.id].list = [];
map[curr.id].metadata = metadataForVuln(curr);
map[curr.id].isIgnored = false;
map[curr.id].isPatched = false;
// Extra added fields for ease of handling
map[curr.id].title = curr.title;
map[curr.id].note = curr.note;
map[curr.id].severity = curr.severity as SEVERITY;
map[curr.id].originalSeverity = curr.originalSeverity as SEVERITY;
map[curr.id].isNew = isNewVuln(curr);
map[curr.id].name = curr.name;
map[curr.id].version = curr.version;
map[curr.id].fixedIn = curr.fixedIn;
map[curr.id].dockerfileInstruction = curr.dockerfileInstruction;
map[curr.id].dockerBaseImage = curr.dockerBaseImage;
map[curr.id].nearestFixedInVersion = curr.nearestFixedInVersion;
map[curr.id].legalInstructionsArray = curr.legalInstructionsArray;
map[curr.id].reachability = curr.reachability;
}
map[curr.id].list.push(curr);
if (!map[curr.id].isFixable) {
map[curr.id].isFixable = isVulnFixable(curr);
}
if (!map[curr.id].note) {
map[curr.id].note = !!curr.note;
}
return map;
}, {});
}
function metadataForVuln(vuln): VulnMetaData {
return {
id: vuln.id,
title: vuln.title,
description: vuln.description,
type: vuln.type,
name: vuln.name,
info: vuln.info,
severity: vuln.severity,
severityValue: getSeverityValue(vuln.severity),
isNew: isNewVuln(vuln),
version: vuln.version,
packageManager: vuln.packageManager,
};
}