/
remediation-based-format-issues.ts
192 lines (169 loc) · 5.63 KB
/
remediation-based-format-issues.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
import * as _ from 'lodash';
import chalk from 'chalk';
import * as wrap from 'wrap-ansi';
import * as config from '../../../../lib/config';
import { TestOptions } from '../../../../lib/types';
import { RemediationResult, PatchRemediation,
DependencyUpdates, IssueData, SEVERITY, GroupedVuln } from '../../../../lib/snyk-test/legacy';
import { SEVERITIES } from '../../../../lib/snyk-test/common';
interface BasicVulnInfo {
title: string;
severity: SEVERITY;
isNew: boolean;
name: string;
version: string;
fixedIn: string[];
legalInstructions?: string;
}
export function formatIssuesWithRemediation(
vulns: GroupedVuln[],
remediationInfo: RemediationResult,
options: TestOptions,
): string[] {
const basicVulnInfo: {
[name: string]: BasicVulnInfo,
} = {};
for (const vuln of vulns) {
basicVulnInfo[vuln.metadata.id] = {
title: vuln.title,
severity: vuln.severity,
isNew: vuln.isNew,
name: vuln.name,
version: vuln.version,
fixedIn: vuln.fixedIn,
legalInstructions: vuln.legalInstructions,
};
}
const results = [chalk.bold.white('Remediation advice')];
const upgradeTextArray = constructUpgradesText(remediationInfo.upgrade, basicVulnInfo);
if (upgradeTextArray.length > 0) {
results.push(upgradeTextArray.join('\n'));
}
const patchedTextArray = constructPatchesText(remediationInfo.patch, basicVulnInfo);
if (patchedTextArray.length > 0) {
results.push(patchedTextArray.join('\n'));
}
const unfixableIssuesTextArray = constructUnfixableText(remediationInfo.unresolved);
if (unfixableIssuesTextArray.length > 0) {
results.push(unfixableIssuesTextArray.join('\n'));
}
return results;
}
export function getSeverityValue(severity: SEVERITY): number {
return SEVERITIES.find((s) => s.verboseName === severity)!.value;
}
function constructPatchesText(
patches: {
[name: string]: PatchRemediation;
},
basicVulnInfo: {
[name: string]: BasicVulnInfo;
},
): string[] {
if (!(Object.keys(patches).length > 0)) {
return [];
}
const patchedTextArray = [chalk.bold.green('\nPatchable issues:')];
for (const id of Object.keys(patches)) {
// todo: add vulnToPatch package name
const packageAtVersion = `${basicVulnInfo[id].name}@${basicVulnInfo[id].version}`;
const patchedText = `\n Patch available for ${chalk.bold.whiteBright(packageAtVersion)}\n`;
const thisPatchFixes =
formatIssue(
id,
basicVulnInfo[id].title,
basicVulnInfo[id].severity,
basicVulnInfo[id].isNew,
`${basicVulnInfo[id].name}@${basicVulnInfo[id].version}`,
basicVulnInfo[id].legalInstructions);
patchedTextArray.push(patchedText + thisPatchFixes);
}
return patchedTextArray;
}
function constructUpgradesText(
upgrades: DependencyUpdates,
basicVulnInfo: {
[name: string]: BasicVulnInfo;
},
): string[] {
if (!(Object.keys(upgrades).length > 0)) {
return [];
}
const upgradeTextArray = [chalk.bold.green('\nUpgradable Issues:')];
for (const upgrade of Object.keys(upgrades)) {
const upgradeDepTo = _.get(upgrades, [upgrade, 'upgradeTo']);
const vulnIds = _.get(upgrades, [upgrade, 'vulns']);
const upgradeText =
`\n Upgrade ${chalk.bold.whiteBright(upgrade)} to ${chalk.bold.whiteBright(upgradeDepTo)} to fix\n`;
const thisUpgradeFixes = vulnIds
.sort((a, b) => getSeverityValue(basicVulnInfo[a].severity) - getSeverityValue(basicVulnInfo[b].severity))
.map((id) => formatIssue(
id,
basicVulnInfo[id].title,
basicVulnInfo[id].severity,
basicVulnInfo[id].isNew,
`${basicVulnInfo[id].name}@${basicVulnInfo[id].version}`,
basicVulnInfo[id].legalInstructions))
.join('\n');
upgradeTextArray.push(upgradeText + thisUpgradeFixes);
}
return upgradeTextArray;
}
function constructUnfixableText(unresolved: IssueData[]) {
if (!(unresolved.length > 0)) {
return [];
}
const unfixableIssuesTextArray = [chalk.bold.white('\nIssues with no direct upgrade or patch:')];
for (const issue of unresolved) {
const extraInfo = issue.fixedIn && issue.fixedIn.length
? `\n This issue was fixed in versions: ${chalk.bold(issue.fixedIn.join(', '))}`
: '\n No upgrade or patch available';
const packageNameAtVersion = chalk.bold
.whiteBright(`\n ${issue.packageName}@${issue.version}\n`);
unfixableIssuesTextArray
.push(packageNameAtVersion +
formatIssue(
issue.id,
issue.title,
issue.severity,
issue.isNew,
issue.legalInstructions) + `${extraInfo}`);
}
return unfixableIssuesTextArray;
}
function formatIssue(
id: string,
title: string,
severity: SEVERITY,
isNew: boolean,
legalInstructions?: string,
vulnerableModule?: string): string {
const severitiesColourMapping = {
low: {
colorFunc(text) {
return chalk.blueBright(text);
},
},
medium: {
colorFunc(text) {
return chalk.yellowBright(text);
},
},
high: {
colorFunc(text) {
return chalk.redBright(text);
},
},
};
const newBadge = isNew ? ' (new)' : '';
const name = vulnerableModule ? ` in ${chalk.bold(vulnerableModule)}` : '';
const wrapLegalText = wrap(`${legalInstructions}`, 100);
const formatLegalText = wrapLegalText.split('\n').join('\n ');
return severitiesColourMapping[severity].colorFunc(
` ✗ ${chalk.bold(title)}${newBadge} [${titleCaseText(severity)} Severity]`,
) + `[${config.ROOT}/vuln/${id}]` + name
+ (legalInstructions ? `${chalk.bold('\n Legal instructions')}:\n ${formatLegalText}` : '');
}
function titleCaseText(text) {
return text[0].toUpperCase() + text.slice(1);
}