-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vulnerability.js
157 lines (155 loc) · 5.16 KB
/
vulnerability.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
const versioning = require('../../../versioning');
module.exports = {
detectVulnerabilityAlerts,
};
async function detectVulnerabilityAlerts(input) {
if (!(input && 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 combinedAlerts = {};
for (const alert of alerts) {
try {
if (alert.dismissReason) {
continue; // eslint-disable-line no-continue
}
if (!alert.securityVulnerability.firstPatchedVersion) {
logger.info(
{ alert },
'Vulnerability alert has no firstPatchedVersion - skipping'
);
continue; // eslint-disable-line no-continue
}
const datasourceMapping = {
MAVEN: 'maven',
NPM: 'npm',
NUGET: 'nuget',
PIP: 'pypi',
RUBYGEMS: 'bundler',
};
const datasource =
datasourceMapping[alert.securityVulnerability.package.ecosystem];
if (!combinedAlerts[datasource]) {
combinedAlerts[datasource] = {};
}
const depName = alert.securityVulnerability.package.name;
if (!combinedAlerts[datasource][depName]) {
combinedAlerts[datasource][depName] = {
advisories: [],
fileNames: [],
};
}
combinedAlerts[datasource][depName].advisories.push(
alert.securityAdvisory
);
const fileName = alert.vulnerableManifestFilename;
if (!combinedAlerts[datasource][depName].fileNames.includes(fileName)) {
combinedAlerts[datasource][depName].fileNames.push(fileName);
}
const firstPatchedVersion =
alert.securityVulnerability.firstPatchedVersion.identifier;
const versionSchemes = {
maven: 'maven',
npm: 'npm',
nuget: 'semver',
pip_requirements: 'pep440',
rubygems: 'ruby',
};
const versionScheme = versioning.get(versionSchemes[datasource]);
if (versionScheme.isVersion(firstPatchedVersion)) {
if (combinedAlerts[datasource][depName].firstPatchedVersion) {
if (
versionScheme.isGreaterThan(
firstPatchedVersion,
combinedAlerts[datasource][depName].firstPatchedVersion
)
) {
combinedAlerts[datasource][
depName
].firstPatchedVersion = firstPatchedVersion;
}
} else {
combinedAlerts[datasource][
depName
].firstPatchedVersion = firstPatchedVersion;
}
} else {
logger.info('Invalid firstPatchedVersion: ' + firstPatchedVersion);
}
} catch (err) {
logger.warn({ err }, 'Error parsing vulnerability alert');
}
}
const alertPackageRules = [];
for (const [datasource, dependencies] of Object.entries(combinedAlerts)) {
for (const [depName, val] of Object.entries(dependencies)) {
let prBodyNotes = [];
try {
prBodyNotes = ['### GitHub Vulnerability Alerts'].concat(
val.advisories.map(advisory => {
let content = '#### ';
let heading;
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';
content += advisory.description;
return content;
})
);
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error generating vulnerability PR notes');
}
let matchCurrentVersion;
if (datasource === 'maven') {
matchCurrentVersion = `(,${val.firstPatchedVersion})`;
} else {
matchCurrentVersion = `< ${val.firstPatchedVersion}`;
}
const matchRule = {
datasources: [datasource],
packageNames: [depName],
matchCurrentVersion,
prBodyNotes,
force: {
...config.vulnerabilityAlerts,
vulnerabilityAlert: true,
branchTopic: `${datasource}-${depName}-vulnerability`,
},
};
alertPackageRules.push(matchRule);
const allowedRule = JSON.parse(JSON.stringify(matchRule));
delete allowedRule.matchCurrentVersion;
delete allowedRule.force;
if (datasource === 'npm') {
allowedRule.allowedVersions = `^${val.firstPatchedVersion}`;
} else {
allowedRule.allowedVersions = `>= ${val.firstPatchedVersion}`;
}
alertPackageRules.push(allowedRule);
}
}
config.packageRules = (config.packageRules || []).concat(alertPackageRules);
return config;
}