-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vulnerability.ts
223 lines (220 loc) · 7.95 KB
/
vulnerability.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
import { PackageRule, RenovateConfig } from '../../../config';
import { NO_VULNERABILITY_ALERTS } from '../../../constants/error-messages';
import * as datasourceMaven from '../../../datasource/maven';
import { id as npmId } from '../../../datasource/npm';
import * as datasourceNuget from '../../../datasource/nuget';
import * as datasourcePypi from '../../../datasource/pypi';
import * as datasourceRubygems from '../../../datasource/rubygems';
import { logger } from '../../../logger';
import { platform } from '../../../platform';
import { SecurityAdvisory } from '../../../types';
import { sanitizeMarkdown } from '../../../util/markdown';
import * as allVersioning from '../../../versioning';
import * as mavenVersioning from '../../../versioning/maven';
import * as npmVersioning from '../../../versioning/npm';
import * as pep440Versioning from '../../../versioning/pep440';
import * as rubyVersioning from '../../../versioning/ruby';
import * as semverVersioning from '../../../versioning/semver';
type Datasource = string;
type DependencyName = string;
type FileName = string;
type VulnerableRequirements = string;
type CombinedAlert = Record<
FileName,
Record<
Datasource,
Record<
DependencyName,
Record<
VulnerableRequirements,
{
advisories: SecurityAdvisory[];
fileType?: string;
firstPatchedVersion?: string;
}
>
>
>
>;
export async function detectVulnerabilityAlerts(
input: RenovateConfig
): Promise<RenovateConfig> {
if (!input?.vulnerabilityAlerts) {
return input;
}
if (input.vulnerabilityAlerts.enabled === false) {
logger.debug('Vulnerability alerts are disabled');
return input;
}
const alerts = await platform.getVulnerabilityAlerts();
if (!alerts.length) {
logger.debug('No vulnerability alerts found');
if (input.vulnerabilityAlertsOnly) {
throw new Error(NO_VULNERABILITY_ALERTS);
}
return input;
}
const config = { ...input };
const versionings: Record<string, string> = {
maven: mavenVersioning.id,
npm: npmVersioning.id,
nuget: semverVersioning.id,
pypi: pep440Versioning.id,
rubygems: rubyVersioning.id,
};
const combinedAlerts: CombinedAlert = {};
for (const alert of alerts) {
try {
if (alert.dismissReason) {
continue; // eslint-disable-line no-continue
}
if (!alert.securityVulnerability.firstPatchedVersion) {
logger.debug(
{ alert },
'Vulnerability alert has no firstPatchedVersion - skipping'
);
continue; // eslint-disable-line no-continue
}
const datasourceMapping: Record<string, string> = {
MAVEN: datasourceMaven.id,
NPM: npmId,
NUGET: datasourceNuget.id,
PIP: datasourcePypi.id,
RUBYGEMS: datasourceRubygems.id,
};
const datasource =
datasourceMapping[alert.securityVulnerability.package.ecosystem];
const depName = alert.securityVulnerability.package.name;
const fileName = alert.vulnerableManifestPath;
const fileType = alert.vulnerableManifestFilename;
const firstPatchedVersion =
alert.securityVulnerability.firstPatchedVersion.identifier;
const advisory = alert.securityAdvisory;
let { vulnerableRequirements } = alert;
// istanbul ignore if
if (!vulnerableRequirements.length) {
if (datasource === datasourceMaven.id) {
vulnerableRequirements = `(,${firstPatchedVersion})`;
} else {
vulnerableRequirements = `< ${firstPatchedVersion}`;
}
}
if (datasource === datasourcePypi.id) {
vulnerableRequirements = vulnerableRequirements.replace(/^= /, '== ');
}
combinedAlerts[fileName] ||= {};
combinedAlerts[fileName][datasource] ||= {};
combinedAlerts[fileName][datasource][depName] ||= {};
combinedAlerts[fileName][datasource][depName][
vulnerableRequirements
] ||= {
advisories: [],
};
const alertDetails =
combinedAlerts[fileName][datasource][depName][vulnerableRequirements];
alertDetails.advisories.push(advisory);
const version = allVersioning.get(versionings[datasource]);
if (version.isVersion(firstPatchedVersion)) {
if (
!alertDetails.firstPatchedVersion ||
version.isGreaterThan(
firstPatchedVersion,
alertDetails.firstPatchedVersion
)
) {
alertDetails.firstPatchedVersion = firstPatchedVersion;
}
} else {
logger.debug('Invalid firstPatchedVersion: ' + firstPatchedVersion);
}
alertDetails.fileType = fileType;
} catch (err) {
logger.warn({ err }, 'Error parsing vulnerability alert');
}
}
const alertPackageRules: PackageRule[] = [];
config.remediations = {} as never;
for (const [fileName, files] of Object.entries(combinedAlerts)) {
for (const [datasource, dependencies] of Object.entries(files)) {
for (const [depName, currentValues] of Object.entries(dependencies)) {
for (const [matchCurrentVersion, val] of Object.entries(
currentValues
)) {
let prBodyNotes: string[] = [];
try {
prBodyNotes = ['### GitHub Vulnerability Alerts'].concat(
val.advisories.map((advisory) => {
let content = '#### ';
let heading: string;
if (advisory.identifiers.some((id) => id.type === 'CVE')) {
heading = advisory.identifiers
.filter((id) => id.type === 'CVE')
.map((id) => id.value)
.join(' / ');
} else {
heading = advisory.identifiers
.map((id) => id.value)
.join(' / ');
}
if (advisory.references.length) {
heading = `[${heading}](${advisory.references[0].url})`;
}
content += heading;
content += '\n\n';
// eslint-disable-next-line no-loop-func
content += sanitizeMarkdown(advisory.description);
return content;
})
);
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error generating vulnerability PR notes');
}
const allowedVersions =
datasource === datasourcePypi.id
? `==${val.firstPatchedVersion}`
: val.firstPatchedVersion;
let matchRule: PackageRule = {
matchDatasources: [datasource],
matchPackageNames: [depName],
matchCurrentVersion,
matchFiles: [fileName],
};
const supportedRemediationFileTypes = ['package-lock.json'];
if (
config.transitiveRemediation &&
supportedRemediationFileTypes.includes(val.fileType)
) {
config.remediations[fileName] ||= [];
const currentVersion = matchCurrentVersion.replace('=', '').trim();
const newVersion = allowedVersions;
const remediation = {
datasource,
depName,
currentVersion,
newVersion,
prBodyNotes,
};
config.remediations[fileName].push(remediation);
// Disable the package rule as all vulnerabilities will be remediated via the lock file
matchRule.enabled = false;
} else {
// Remediate only direct dependencies
matchRule = {
...matchRule,
allowedVersions,
prBodyNotes,
isVulnerabilityAlert: true,
force: {
...config.vulnerabilityAlerts,
},
};
}
alertPackageRules.push(matchRule);
}
}
}
}
logger.debug({ alertPackageRules }, 'alert package rules');
config.packageRules = (config.packageRules || []).concat(alertPackageRules);
return config;
}