forked from nodejs/node-core-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity-release.js
More file actions
280 lines (243 loc) · 8.71 KB
/
security-release.js
File metadata and controls
280 lines (243 loc) · 8.71 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
import { runSync } from '../run.js';
import nv from '@pkgjs/nv';
import fs from 'node:fs';
import path from 'node:path';
export const NEXT_SECURITY_RELEASE_BRANCH = 'next-security-release';
export const NEXT_SECURITY_RELEASE_FOLDER = 'security-release/next-security-release';
export const NEXT_SECURITY_RELEASE_REPOSITORY = {
owner: 'nodejs-private',
repo: 'security-release'
};
export const PLACEHOLDERS = {
releaseDate: '%RELEASE_DATE%',
vulnerabilitiesPRURL: '%VULNERABILITIES_PR_URL%',
preReleasePrivate: '%PRE_RELEASE_PRIV%',
postReleasePrivate: '%POS_RELEASE_PRIV%',
affectedLines: '%AFFECTED_LINES%',
annoucementDate: '%ANNOUNCEMENT_DATE%',
slug: '%SLUG%',
affectedVersions: '%AFFECTED_VERSIONS%',
impact: '%IMPACT%',
vulnerabilities: '%VULNERABILITIES%',
reports: '%REPORTS%',
author: '%AUTHOR%',
dependencyUpdates: '%DEPENDENCY_UPDATES%',
downloads: '%DOWNLOADS%'
};
export function checkRemote(cli, repository) {
const remote = runSync('git', ['ls-remote', '--get-url', 'origin']).trim();
const { owner, repo } = repository;
const securityReleaseOrigin = [
`https://github.com/${owner}/${repo}.git`,
`[email protected]:${owner}/${repo}.git`
];
if (!securityReleaseOrigin.includes(remote)) {
cli.error(`Wrong repository! It should be ${securityReleaseOrigin}`);
process.exit(1);
}
}
export function checkoutOnSecurityReleaseBranch(cli, repository) {
checkRemote(cli, repository);
const currentBranch = runSync('git', ['branch', '--show-current']).trim();
cli.info(`Current branch: ${currentBranch} `);
if (currentBranch !== NEXT_SECURITY_RELEASE_BRANCH) {
runSync('git', ['checkout', '-B', NEXT_SECURITY_RELEASE_BRANCH]);
cli.ok(`Checkout on branch: ${NEXT_SECURITY_RELEASE_BRANCH} `);
};
}
export function commitAndPushVulnerabilitiesJSON(filePath, commitMessage, { cli, repository }) {
checkRemote(cli, repository);
if (Array.isArray(filePath)) {
for (const path of filePath) {
runSync('git', ['add', path]);
}
} else {
runSync('git', ['add', filePath]);
}
const staged = runSync('git', ['diff', '--name-only', '--cached']).trim();
if (!staged) {
cli.ok('No changes to commit');
return;
}
runSync('git', ['commit', '-m', commitMessage]);
try {
runSync('git', ['push', '-u', 'origin', NEXT_SECURITY_RELEASE_BRANCH]);
} catch (error) {
cli.warn('Rebasing...');
// try to pull rebase and push again
runSync('git', ['pull', 'origin', NEXT_SECURITY_RELEASE_BRANCH, '--rebase']);
runSync('git', ['push', '-u', 'origin', NEXT_SECURITY_RELEASE_BRANCH]);
}
cli.ok(`Pushed commit: ${commitMessage} to ${NEXT_SECURITY_RELEASE_BRANCH}`);
}
export async function getSupportedVersions() {
const supportedVersions = (await nv('supported'))
.map((v) => `${v.versionName}.x`)
.join(',');
return supportedVersions;
}
export function getSummary(report) {
const summaryList = report?.relationships?.summaries?.data;
if (!summaryList?.length) return;
const summaries = summaryList.filter((summary) => summary?.attributes?.category === 'team');
if (!summaries?.length) return;
return summaries?.[0].attributes?.content;
}
export function getVulnerabilitiesJSON(cli) {
const vulnerabilitiesJSONPath = path.join(process.cwd(),
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');
cli.startSpinner(`Reading vulnerabilities.json from ${vulnerabilitiesJSONPath}..`);
const file = JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf-8'));
cli.stopSpinner(`Done reading vulnerabilities.json from ${vulnerabilitiesJSONPath}`);
return file;
}
export function getVulnerabilitiesJSONPath() {
const vulnerabilitiesJSONPath = path.join(process.cwd(),
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');
return vulnerabilitiesJSONPath;
}
export function validateDate(releaseDate) {
const value = new Date(releaseDate).valueOf();
if (Number.isNaN(value) || value < 0) {
throw new Error('Invalid date format');
}
}
export function formatDateToYYYYMMDD(date) {
// Get year, month, and day
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
const day = String(date.getDate()).padStart(2, '0');
// Concatenate year, month, and day with slashes
return `${year}/${month}/${day}`;
}
export function promptDependencies(cli) {
return cli.prompt('Enter the link to the dependency update PR (leave empty to exit): ', {
defaultAnswer: '',
questionType: 'input'
});
}
export async function createIssue(title, content, repository, { cli, req }) {
const data = await req.createIssue(title, content, repository);
if (data.html_url) {
cli.ok(`Created: ${data.html_url}`);
return data.html_url;
} else {
cli.error(data);
process.exit(1);
}
}
export function getReportSeverity(report) {
const {
relationships: { severity, weakness }
} = report;
const reportSeverity = {
rating: severity?.data?.attributes?.rating || '',
cvss_vector_string: severity?.data?.attributes?.cvss_vector_string || '',
weakness_id: weakness?.data?.id || ''
};
return reportSeverity;
}
export async function pickReport(report, { cli, req }) {
const {
id, attributes: { title, cve_ids },
relationships: { reporter, custom_field_values }
} = report;
const link = `https://hackerone.com/reports/${id}`;
const reportSeverity = getReportSeverity(report);
cli.separator();
cli.info(`Report: ${link} - ${title} (${reportSeverity?.rating})`);
const include = await cli.prompt(
'Would you like to include this report to the next security release?',
{ defaultAnswer: true });
if (!include) {
return;
}
const versions = await cli.prompt('Which active release lines this report affects?', {
questionType: 'input',
defaultAnswer: await getSupportedVersions()
});
let prURL = '';
let patchAuthors = [];
if (custom_field_values.data.length) {
prURL = custom_field_values.data[0].attributes.value;
const { user } = await req.getPullRequest(prURL);
patchAuthors = [user.login];
} else {
patchAuthors = await cli.prompt(
'Add github username of the authors of the patch (split by comma if multiple)', {
questionType: 'input',
defaultAnswer: ''
});
if (!patchAuthors) {
patchAuthors = [];
} else {
patchAuthors = patchAuthors.split(',').map((p) => p.trim());
}
}
const summaryContent = getSummary(report);
return {
id,
title,
cveIds: cve_ids,
severity: reportSeverity,
summary: summaryContent ?? '',
patchAuthors,
prURL,
affectedVersions: versions.split(',').map((v) => v.replace('v', '').trim()),
link,
reporter: reporter.data.attributes.username
};
}
export class SecurityRelease {
constructor(cli, repository = NEXT_SECURITY_RELEASE_REPOSITORY) {
this.cli = cli;
this.repository = repository;
}
readVulnerabilitiesJSON(vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath()) {
const exists = fs.existsSync(vulnerabilitiesJSONPath);
if (!exists) {
this.cli.error(`The file vulnerabilities.json does not exist at ${vulnerabilitiesJSONPath}`);
process.exit(1);
}
return JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf8'));
}
getVulnerabilitiesJSONPath() {
return path.join(process.cwd(),
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');
}
updateReleaseFolder(releaseDate) {
const folder = path.join(process.cwd(),
NEXT_SECURITY_RELEASE_FOLDER);
const newFolder = path.join(process.cwd(), 'security-release', releaseDate);
fs.renameSync(folder, newFolder);
return newFolder;
}
updateVulnerabilitiesJSON(content) {
try {
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
this.cli.startSpinner(`Updating vulnerabilities.json from ${vulnerabilitiesJSONPath}...`);
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
commitAndPushVulnerabilitiesJSON(vulnerabilitiesJSONPath,
'chore: updated vulnerabilities.json',
{ cli: this.cli, repository: this.repository });
this.cli.stopSpinner(`Done updating vulnerabilities.json from ${vulnerabilitiesJSONPath}`);
} catch (error) {
this.cli.error('Error updating vulnerabilities.json');
this.cli.error(error);
}
}
getAffectedVersions(content) {
const affectedVersions = new Set();
for (const report of Object.values(content.reports)) {
for (const affectedVersion of report.affectedVersions) {
affectedVersions.add(affectedVersion);
}
}
const parseToNumber = str => +(str.match(/[\d.]+/g)[0]);
return Array.from(affectedVersions)
.sort((a, b) => {
return parseToNumber(a) > parseToNumber(b) ? -1 : 1;
})
.join(', ');
}
}