-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathupdate_security_release.js
More file actions
295 lines (263 loc) · 10.3 KB
/
update_security_release.js
File metadata and controls
295 lines (263 loc) · 10.3 KB
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
import {
checkoutOnSecurityReleaseBranch,
checkRemote,
commitAndPushVulnerabilitiesJSON,
validateDate,
pickReport,
getReportSeverity,
getSummary,
SecurityRelease
} from './security-release/security-release.js';
import fs from 'node:fs';
import auth from './auth.js';
import Request from './request.js';
import nv from '@pkgjs/nv';
export default class UpdateSecurityRelease extends SecurityRelease {
async sync() {
checkRemote(this.cli, this.repository);
const content = this.readVulnerabilitiesJSON();
const credentials = await auth({
github: true,
h1: true
});
const req = new Request(credentials);
for (let i = 0; i < content.reports.length; ++i) {
const report = content.reports[i];
const { data } = await req.getReport(report.id);
const reportSeverity = getReportSeverity(data);
const summaryContent = getSummary(data);
const link = `https://hackerone.com/reports/${report.id}`;
let prURL = report.prURL;
if (data.relationships.custom_field_values.data.length) {
prURL = data.relationships.custom_field_values.data[0].attributes.value;
}
content.reports[i] = {
...report,
title: data.attributes.title,
cveIds: data.attributes.cve_ids,
severity: reportSeverity,
summary: summaryContent ?? report.summary,
link,
prURL
};
}
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
const commitMessage = 'chore: git node security --sync';
commitAndPushVulnerabilitiesJSON([vulnerabilitiesJSONPath],
commitMessage, { cli: this.cli, repository: this.repository });
this.cli.ok('Synced vulnerabilities.json with HackerOne');
}
async updateReleaseDate(releaseDate) {
const { cli } = this;
try {
validateDate(releaseDate);
} catch (error) {
cli.error('Invalid date format. Please use the format yyyy/mm/dd.');
process.exit(1);
}
// checkout on the next-security-release branch
checkoutOnSecurityReleaseBranch(cli, this.repository);
// update the release date in the vulnerabilities.json file
const updatedVulnerabilitiesFiles = await this.updateJSONReleaseDate(releaseDate, { cli });
const commitMessage = `chore: update the release date to ${releaseDate}`;
commitAndPushVulnerabilitiesJSON(updatedVulnerabilitiesFiles,
commitMessage, { cli, repository: this.repository });
cli.ok('Done!');
}
async updateJSONReleaseDate(releaseDate) {
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
content.releaseDate = releaseDate;
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
this.cli.ok(`Updated the release date in vulnerabilities.json: ${releaseDate}`);
return [vulnerabilitiesJSONPath];
}
async addReport(reportId) {
const credentials = await auth({
github: true,
h1: true
});
const req = new Request(credentials);
// checkout on the next-security-release branch
checkoutOnSecurityReleaseBranch(this.cli, this.repository);
// get h1 report
const { data: report } = await req.getReport(reportId);
const entry = await pickReport(report, { cli: this.cli, req });
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
content.reports.push(entry);
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
this.cli.ok(`Updated vulnerabilities.json with the report: ${entry.id}`);
const commitMessage = `chore: added report ${entry.id} to vulnerabilities.json`;
commitAndPushVulnerabilitiesJSON(vulnerabilitiesJSONPath,
commitMessage, { cli: this.cli, repository: this.repository });
this.cli.ok('Done!');
}
removeReport(reportId) {
const { cli } = this;
// checkout on the next-security-release branch
checkoutOnSecurityReleaseBranch(cli, this.repository);
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
const found = content.reports.some((report) => report.id === reportId);
if (!found) {
cli.error(`Report with id ${reportId} not found in vulnerabilities.json`);
process.exit(1);
}
content.reports = content.reports.filter((report) => report.id !== reportId);
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
this.cli.ok(`Updated vulnerabilities.json with the report: ${reportId}`);
const commitMessage = `chore: remove report ${reportId} from vulnerabilities.json`;
commitAndPushVulnerabilitiesJSON(vulnerabilitiesJSONPath,
commitMessage, { cli, repository: this.repository });
cli.ok('Done!');
}
async requestCVEs() {
const credentials = await auth({
github: true,
h1: true
});
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
const { reports } = content;
const req = new Request(credentials);
const programId = await this.getNodeProgramId(req);
const cves = await this.promptCVECreation(req, reports, programId);
this.assignCVEtoReport(cves, reports);
this.updateVulnerabilitiesJSON(content);
this.updateHackonerReportCve(req, reports);
}
assignCVEtoReport(cves, reports) {
for (const cve of cves) {
const report = reports.find(report => report.id === cve.reportId);
report.cveIds = [cve.cve_identifier];
report.patchedVersions = cve.patchedVersions;
}
}
async updateHackonerReportCve(req, reports) {
for (const report of reports) {
const { id, cveIds } = report;
this.cli.startSpinner(`Updating report ${id} with CVEs ${cveIds}..`);
const body = {
data: {
type: 'report-cves',
attributes: {
cve_ids: cveIds
}
}
};
const response = await req.updateReportCVE(id, body);
if (response.errors) {
this.cli.error(`Error updating report ${id}`);
this.cli.error(JSON.stringify(response.errors, null, 2));
}
this.cli.stopSpinner(`Done updating report ${id} with CVEs ${cveIds}..`);
}
}
async promptCVECreation(req, reports, programId) {
const supportedVersions = (await nv('supported'));
const cves = [];
for (const report of reports) {
const { id, summary, title, affectedVersions, cveIds, link } = report;
// skip if already has a CVE
// risky because the CVE associated might be
// mentioned in the report and not requested by Node
if (cveIds?.length) continue;
let severity = report.severity;
if (!severity.cvss_vector_string || !severity.weakness_id) {
try {
const h1Report = await req.getReport(id);
if (!h1Report.data.relationships.severity?.data.attributes.cvss_vector_string) {
throw new Error('No severity found');
}
severity = {
weakness_id: h1Report.data.relationships.weakness?.data.id,
cvss_vector_string:
h1Report.data.relationships.severity?.data.attributes.cvss_vector_string,
rating: h1Report.data.relationships.severity?.data.attributes.rating
};
} catch (error) {
this.cli.error(`Couldnt not retrieve severity from report ${id}, skipping...`);
continue;
}
}
const { cvss_vector_string, weakness_id } = severity;
const create = await this.cli.prompt(
`Request a CVE for: \n
Title: ${title}\n
Link: ${link}\n
Affected versions: ${affectedVersions.join(', ')}\n
Vector: ${cvss_vector_string}\n
Summary: ${summary}\n`,
{ defaultAnswer: true });
if (!create) continue;
const { h1AffectedVersions, patchedVersions } =
await this.calculateVersions(affectedVersions, supportedVersions);
const body = {
data: {
type: 'cve-request',
attributes: {
team_handle: 'nodejs-team',
versions: h1AffectedVersions,
metrics: [
{
vectorString: cvss_vector_string
}
],
auto_submit_on_publicly_disclosing_report: true,
references: ['https://nodejs.org/en/blog/vulnerability'],
report_id: report.id,
weakness_id: Number(weakness_id),
description: report.summary,
vulnerability_discovered_at: new Date().toISOString()
}
}
};
const { data } = await req.requestCVE(programId, body);
if (data.errors) {
this.cli.error(`Error requesting CVE for report ${id}`);
this.cli.error(JSON.stringify(data.errors, null, 2));
continue;
}
const { cve_identifier } = data.attributes;
cves.push({ cve_identifier, reportId: id, patchedVersions });
}
return cves;
}
async getNodeProgramId(req) {
const programs = await req.getPrograms();
const { data } = programs;
for (const program of data) {
const { attributes } = program;
if (attributes.handle === 'nodejs') {
return program.id;
}
}
}
async calculateVersions(affectedVersions, supportedVersions) {
const h1AffectedVersions = [];
const patchedVersions = [];
for (const affectedVersion of affectedVersions) {
const major = affectedVersion.split('.')[0];
const latest = supportedVersions.find((v) => v.major === Number(major)).version;
const version = await this.cli.prompt(
`What is the affected version (<=) for release line ${affectedVersion}?`,
{ questionType: 'input', defaultAnswer: latest });
const nextPatchVersion = parseInt(version.split('.')[2]) + 1;
const patchedVersion = await this.cli.prompt(
`What is the patched version (>=) for release line ${affectedVersion}?`,
{ questionType: 'input', defaultAnswer: nextPatchVersion });
patchedVersions.push(patchedVersion);
h1AffectedVersions.push({
vendor: 'nodejs',
product: 'node',
func: '<=',
version,
versionType: 'semver',
affected: true
});
}
return { h1AffectedVersions, patchedVersions };
}
}