/
legacy-format-issue.ts
184 lines (163 loc) · 6.1 KB
/
legacy-format-issue.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
import * as _ from 'lodash';
import chalk from 'chalk';
import * as wrap from 'wrap-ansi';
import * as config from '../../../../lib/config';
import {Options, TestOptions} from '../../../../lib/types';
import {isLocalFolder} from '../../../../lib/detect';
import { WIZARD_SUPPORTED_PACKAGE_MANAGERS } from '../../../../lib/package-managers';
import { GroupedVuln } from '../../../../lib/snyk-test/legacy';
export function formatIssues(vuln: GroupedVuln, options: Options & TestOptions) {
const vulnID = vuln.list[0].id;
const packageManager = options.packageManager;
const localPackageTest = isLocalFolder(options.path);
const uniquePackages = _.uniq(
vuln.list.map((i) => {
if (i.from[1]) {
return i.from && i.from[1];
}
return i.from;
}))
.join(', ');
let version;
if (vuln.metadata.packageManager.toLowerCase() === 'upstream') {
version = vuln.metadata.version;
}
const vulnOutput = {
issueHeading: createSeverityBasedIssueHeading(
vuln.metadata.severity,
vuln.metadata.type,
vuln.metadata.name,
false,
),
introducedThrough: ' Introduced through: ' + uniquePackages,
description: ' Description: ' + vuln.title,
info: ' Info: ' + chalk.underline(config.ROOT + '/vuln/' + vulnID),
fromPaths: options.showVulnPaths
? createTruncatedVulnsPathsText(vuln.list) : '',
extraInfo: vuln.note ? chalk.bold('\n Note: ' + vuln.note) : '',
remediationInfo: vuln.metadata.type !== 'license' && localPackageTest
? createRemediationText(vuln, packageManager)
: '',
fixedIn: options.docker ? createFixedInText(vuln) : '',
dockerfilePackage: options.docker ? dockerfileInstructionText(vuln) : '',
legalInstructions: vuln.legalInstructions ? chalk.bold('\n Legal instructions:\n '
+ wrap(vuln.legalInstructions, 100).split('\n').join('\n ')) : '',
};
return (
`${vulnOutput.issueHeading}\n` +
`${vulnOutput.description}\n` +
`${vulnOutput.info}\n` +
`${vulnOutput.introducedThrough}\n` +
vulnOutput.fromPaths +
// Optional - not always there
vulnOutput.remediationInfo +
vulnOutput.dockerfilePackage +
vulnOutput.fixedIn +
vulnOutput.extraInfo +
vulnOutput.legalInstructions
);
}
function createSeverityBasedIssueHeading(severity, type, packageName, isNew) {
// Example: ✗ Medium severity vulnerability found in xmldom
const vulnTypeText = type === 'license' ? 'issue' : 'vulnerability';
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.bold.blue(text);
},
},
medium: {
colorFunc(text) {
return chalk.bold.yellow(text);
},
},
high: {
colorFunc(text) {
return chalk.bold.red(text);
},
},
};
return severitiesColourMapping[severity].colorFunc(
'✗ ' + titleCaseText(severity) + ' severity ' + vulnTypeText
+ ' found in ' + chalk.underline(packageName)) +
chalk.bold.magenta(isNew ? ' (new)' : '');
}
function titleCaseText(text) {
return text[0].toUpperCase() + text.slice(1);
}
function dockerfileInstructionText(vuln) {
if (vuln.dockerfileInstruction) {
return `\n Introduced in your Dockerfile by '${ vuln.dockerfileInstruction }'`;
}
if (vuln.dockerBaseImage) {
return `\n Introduced by your base image (${ vuln.dockerBaseImage })`;
}
return '';
}
function createTruncatedVulnsPathsText(vulnList) {
const numberOfPathsToDisplay = 3;
const fromPathsArray = vulnList.map((i) => i.from);
const formatedFromPathsArray = fromPathsArray.map((i) => {
const fromWithoutBaseProject = i.slice(1);
// If more than one From path
if (fromWithoutBaseProject.length) {
return i.slice(1).join(' > ');
}
// Else issue is in the core package
return i;
});
const notShownPathsNumber = fromPathsArray.length - numberOfPathsToDisplay;
const shouldTruncatePaths = fromPathsArray.length > 3;
const truncatedText = `\n and ${notShownPathsNumber} more...`;
const formattedPathsText = formatedFromPathsArray
.slice(0, numberOfPathsToDisplay)
.join('\n From: ');
if (fromPathsArray.length > 0) {
return ' From: ' + formattedPathsText + (shouldTruncatePaths ? truncatedText : '');
}
}
function createFixedInText(vuln: any): string {
if (vuln.nearestFixedInVersion) {
return chalk.bold('\n Fixed in: ' + vuln.nearestFixedInVersion);
} else if (vuln.fixedIn && vuln.fixedIn.length > 0) {
return chalk.bold('\n Fixed in: ' + vuln.fixedIn.join(', '));
}
return '';
}
function createRemediationText(vuln, packageManager) {
let wizardHintText = '';
if (WIZARD_SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)) {
wizardHintText = 'Run `snyk wizard` to explore remediation options.';
}
if (vuln.isFixable === true) {
const upgradePathsArray = _.uniq(vuln.list.map((v) => {
const shouldUpgradeItself = !!v.upgradePath[0];
const shouldUpgradeDirectDep = !!v.upgradePath[1];
if (shouldUpgradeItself) {
// If we are testing a library/package like express
// Then we can suggest they get the latest version
// Example command: snyk test express@3
const selfUpgradeInfo = (v.upgradePath.length > 0)
? ` (triggers upgrades to ${ v.upgradePath.join(' > ')})`
: '';
const testedPackageName = v.upgradePath[0].split('@');
return `You've tested an outdated version of ${testedPackageName[0]}.` +
+ ` Upgrade to ${v.upgradePath[0]}${selfUpgradeInfo}`;
}
if (shouldUpgradeDirectDep) {
const formattedUpgradePath = v.upgradePath.slice(1).join(' > ');
const upgradeTextInfo = (v.upgradePath.length)
? ` (triggers upgrades to ${formattedUpgradePath})`
: '';
return `Upgrade direct dependency ${v.from[1]} to ${v.upgradePath[1]}${upgradeTextInfo}`;
}
return 'Some paths have no direct dependency upgrade that' +
` can address this issue. ${wizardHintText}`;
}));
return chalk.bold(`\n Remediation: \n ${upgradePathsArray.join('\n ')}`);
}
if (vuln.fixedIn && vuln.fixedIn.length > 0) {
return createFixedInText(vuln);
}
return '';
}