forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_security.js
More file actions
325 lines (281 loc) · 9.93 KB
/
prepare_security.js
File metadata and controls
325 lines (281 loc) · 9.93 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
import fs from 'node:fs';
import path from 'node:path';
import auth from './auth.js';
import Request from './request.js';
import {
NEXT_SECURITY_RELEASE_BRANCH,
NEXT_SECURITY_RELEASE_FOLDER,
checkoutOnSecurityReleaseBranch,
commitAndPushVulnerabilitiesJSON,
validateDate,
promptDependencies,
getSupportedVersions,
pickReport,
SecurityRelease
} from './security-release/security-release.js';
import _ from 'lodash';
export default class PrepareSecurityRelease extends SecurityRelease {
title = 'Next Security Release';
async start() {
const credentials = await auth({
github: true,
h1: true
});
this.req = new Request(credentials);
const releaseDate = await this.promptReleaseDate();
if (releaseDate !== 'TBD') {
validateDate(releaseDate);
}
const createVulnerabilitiesJSON = await this.promptVulnerabilitiesJSON();
let securityReleasePRUrl;
const content = await this.buildDescription(releaseDate, securityReleasePRUrl);
if (createVulnerabilitiesJSON) {
securityReleasePRUrl = await this.startVulnerabilitiesJSONCreation(releaseDate, content);
}
this.cli.ok('Done!');
}
async cleanup() {
const credentials = await auth({
github: true,
h1: true
});
this.req = new Request(credentials);
const vulnerabilityJSON = this.readVulnerabilitiesJSON();
this.cli.info('Closing and request disclosure to HackerOne reports');
await this.closeAndRequestDisclosure(vulnerabilityJSON.reports);
this.cli.info('Closing pull requests');
// For now, close the ones with Security Release label
await this.closePRWithLabel('Security Release');
const updateFolder = await this.cli.prompt(
`Would you like to update the next-security-release folder to ${
vulnerabilityJSON.releaseDate}?`,
{ defaultAnswer: true });
if (updateFolder) {
this.updateReleaseFolder(
vulnerabilityJSON.releaseDate.replaceAll('/', '-')
);
const securityReleaseFolder = path.join(process.cwd(), 'security-release');
commitAndPushVulnerabilitiesJSON(
securityReleaseFolder,
'chore: change next-security-release folder',
{ cli: this.cli, repository: this.repository }
);
}
this.cli.info(`If the PR is ready (CI is green): merge pull request with:
- git checkout main
- git merge ${NEXT_SECURITY_RELEASE_BRANCH} --no-ff -m "chore: add latest security release"
- git push origin main`);
this.cli.ok('Done!');
}
async startVulnerabilitiesJSONCreation(releaseDate, content) {
// checkout on the next-security-release branch
checkoutOnSecurityReleaseBranch(this.cli, this.repository);
// choose the reports to include in the security release
const reports = await this.chooseReports();
const depUpdates = await this.getDependencyUpdates();
const deps = _.groupBy(depUpdates, 'name');
// create the vulnerabilities.json file in the security-release repo
const filePath = await this.createVulnerabilitiesJSON(reports, deps, releaseDate);
// review the vulnerabilities.json file
const review = await this.promptReviewVulnerabilitiesJSON();
if (!review) {
this.cli.info(`To push the vulnerabilities.json file run:
- git add ${filePath}
- git commit -m "chore: create vulnerabilities.json for next security release"
- git push -u origin ${NEXT_SECURITY_RELEASE_BRANCH}
- open a PR on ${this.repository.owner}/${this.repository.repo}`);
return;
};
// commit and push the vulnerabilities.json file
const commitMessage = 'chore: create vulnerabilities.json for next security release';
commitAndPushVulnerabilitiesJSON(filePath,
commitMessage,
{ cli: this.cli, repository: this.repository });
const createPr = await this.promptCreatePR();
if (!createPr) return;
// create pr on the security-release repo
return this.createPullRequest(content);
}
promptCreatePR() {
return this.cli.prompt(
'Create the Next Security Release PR?',
{ defaultAnswer: true });
}
async getSecurityIssueTemplate() {
const url = 'https://raw.githubusercontent.com/nodejs/node/main/doc/contributing/security-release-process.md';
try {
// fetch document from nodejs/node main so we dont need to keep a copy
const response = await fetch(url);
const body = await response.text();
// remove everything before the Planning section
const index = body.indexOf('## Planning');
if (index !== -1) {
return body.substring(index);
}
return body;
} catch (error) {
this.cli.error(`Could not retrieve the security issue template from ${url}`);
}
}
async promptReleaseDate() {
const nextWeekDate = new Date();
nextWeekDate.setDate(nextWeekDate.getDate() + 7);
// Format the date as YYYY/MM/DD
const formattedDate = nextWeekDate.toISOString().slice(0, 10).replace(/-/g, '/');
return this.cli.prompt(
'Enter target release date in YYYY/MM/DD format (TBD if not defined yet):', {
questionType: 'input',
defaultAnswer: formattedDate
});
}
async promptVulnerabilitiesJSON() {
return this.cli.prompt(
'Create the vulnerabilities.json?',
{ defaultAnswer: true });
}
async promptCreateRelaseIssue() {
return this.cli.prompt(
'Create the Next Security Release issue?',
{ defaultAnswer: true });
}
async promptReviewVulnerabilitiesJSON() {
return this.cli.prompt(
'Please review vulnerabilities.json and press enter to proceed.',
{ defaultAnswer: true });
}
async buildDescription() {
const template = await this.getSecurityIssueTemplate();
return template;
}
async chooseReports() {
this.cli.info('Getting triaged H1 reports...');
const reports = await this.req.getTriagedReports();
const selectedReports = [];
for (const report of reports.data) {
const rep = await pickReport(report, { cli: this.cli, req: this.req });
if (!rep) continue;
selectedReports.push(rep);
}
return selectedReports;
}
async createVulnerabilitiesJSON(reports, dependencies, releaseDate) {
this.cli.separator('Creating vulnerabilities.json...');
const file = JSON.stringify({
releaseDate,
reports,
dependencies
}, null, 2);
const folderPath = path.join(process.cwd(), NEXT_SECURITY_RELEASE_FOLDER);
try {
fs.accessSync(folderPath);
} catch (error) {
fs.mkdirSync(folderPath, { recursive: true });
}
const fullPath = path.join(folderPath, 'vulnerabilities.json');
fs.writeFileSync(fullPath, file);
this.cli.ok(`Created ${fullPath} `);
return fullPath;
}
async createPullRequest(content) {
const { owner, repo } = this.repository;
const response = await this.req.createPullRequest(
this.title,
content ?? 'List of vulnerabilities to be included in the next security release',
{
owner,
repo,
base: 'main',
head: 'next-security-release'
}
);
const url = response?.html_url;
if (url) {
this.cli.ok(`Created: ${url}`);
return url;
}
if (response?.errors) {
for (const error of response.errors) {
this.cli.error(error.message);
}
} else {
this.cli.error(response);
}
process.exit(1);
}
async getDependencyUpdates() {
const deps = [];
this.cli.log('\n');
this.cli.separator('Dependency Updates');
const updates = await this.cli.prompt('Are there dependency updates in this security release?',
{
defaultAnswer: true,
questionType: 'confirm'
});
if (!updates) return deps;
const supportedVersions = await getSupportedVersions();
let asking = true;
while (asking) {
const dep = await promptDependencies(this.cli);
if (!dep) {
asking = false;
break;
}
const name = await this.cli.prompt(
'What is the name of the dependency that has been updated?', {
defaultAnswer: '',
questionType: 'input'
});
const versions = await this.cli.prompt(
'Which release line does this dependency update affect?', {
defaultAnswer: supportedVersions,
questionType: 'input'
});
try {
const res = await this.req.getPullRequest(dep);
const { html_url, title } = res;
deps.push({
name,
url: html_url,
title,
affectedVersions: versions.split(',').map((v) => v.replace('v', '').trim())
});
this.cli.separator();
} catch (error) {
this.cli.error('Invalid PR url. Please provide a valid PR url.');
this.cli.error(error);
}
}
return deps;
}
async closeAndRequestDisclosure(jsonReports) {
this.cli.startSpinner('Closing HackerOne reports');
for (const report of jsonReports) {
this.cli.updateSpinner(`Closing report ${report.id}...`);
await this.req.updateReportState(
report.id,
'resolved',
'Closing as resolved'
);
this.cli.updateSpinner(`Requesting disclosure to report ${report.id}...`);
await this.req.requestDisclosure(report.id);
}
this.cli.stopSpinner('Done closing H1 Reports and requesting disclosure');
}
async closePRWithLabel(labels) {
if (typeof labels === 'string') {
labels = [labels];
}
const url = 'https://github.com/nodejs-private/node-private/pull';
this.cli.startSpinner('Closing GitHub Pull Requests...');
// At this point, GitHub does not provide filters through their REST API
const prs = await this.req.getPullRequest(url);
for (const pr of prs) {
if (pr.labels.some((l) => labels.includes(l.name))) {
this.cli.updateSpinner(`Closing Pull Request: ${pr.number}`);
await this.req.closePullRequest(pr.number,
{ owner: 'nodejs-private', repo: 'node-private' });
}
}
this.cli.stopSpinner('Closed GitHub Pull Requests.');
}
}