@node-core/utils 4.3.0 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/ncu-ci.js +6 -1
- package/components/git/security.js +120 -5
- package/lib/ci/run_ci.js +12 -2
- package/lib/github/templates/security-pre-release.md +30 -0
- package/lib/pr_checker.js +1 -0
- package/lib/prepare_security.js +157 -172
- package/lib/request.js +55 -0
- package/lib/security-announcement.js +76 -0
- package/lib/security-release/security-release.js +193 -0
- package/lib/security_blog.js +182 -0
- package/lib/update_security_release.js +274 -0
- package/package.json +1 -1
- package/lib/github/templates/next-security-release.md +0 -98
@@ -0,0 +1,193 @@
|
|
1
|
+
import { runSync } from '../run.js';
|
2
|
+
import nv from '@pkgjs/nv';
|
3
|
+
import fs from 'node:fs';
|
4
|
+
import path from 'node:path';
|
5
|
+
|
6
|
+
export const NEXT_SECURITY_RELEASE_BRANCH = 'next-security-release';
|
7
|
+
export const NEXT_SECURITY_RELEASE_FOLDER = 'security-release/next-security-release';
|
8
|
+
|
9
|
+
export const NEXT_SECURITY_RELEASE_REPOSITORY = {
|
10
|
+
owner: 'nodejs-private',
|
11
|
+
repo: 'security-release'
|
12
|
+
};
|
13
|
+
|
14
|
+
export const PLACEHOLDERS = {
|
15
|
+
releaseDate: '%RELEASE_DATE%',
|
16
|
+
vulnerabilitiesPRURL: '%VULNERABILITIES_PR_URL%',
|
17
|
+
preReleasePrivate: '%PRE_RELEASE_PRIV%',
|
18
|
+
postReleasePrivate: '%POS_RELEASE_PRIV%',
|
19
|
+
affectedLines: '%AFFECTED_LINES%',
|
20
|
+
annoucementDate: '%ANNOUNCEMENT_DATE%',
|
21
|
+
slug: '%SLUG%',
|
22
|
+
affectedVersions: '%AFFECTED_VERSIONS%',
|
23
|
+
openSSLUpdate: '%OPENSSL_UPDATES%',
|
24
|
+
impact: '%IMPACT%',
|
25
|
+
vulnerabilities: '%VULNERABILITIES%'
|
26
|
+
};
|
27
|
+
|
28
|
+
export function checkRemote(cli, repository) {
|
29
|
+
const remote = runSync('git', ['ls-remote', '--get-url', 'origin']).trim();
|
30
|
+
const { owner, repo } = repository;
|
31
|
+
const securityReleaseOrigin = [
|
32
|
+
`https://github.com/${owner}/${repo}.git`,
|
33
|
+
`git@github.com:${owner}/${repo}.git`
|
34
|
+
];
|
35
|
+
|
36
|
+
if (!securityReleaseOrigin.includes(remote)) {
|
37
|
+
cli.error(`Wrong repository! It should be ${securityReleaseOrigin}`);
|
38
|
+
process.exit(1);
|
39
|
+
}
|
40
|
+
}
|
41
|
+
|
42
|
+
export function checkoutOnSecurityReleaseBranch(cli, repository) {
|
43
|
+
checkRemote(cli, repository);
|
44
|
+
const currentBranch = runSync('git', ['branch', '--show-current']).trim();
|
45
|
+
cli.info(`Current branch: ${currentBranch} `);
|
46
|
+
|
47
|
+
if (currentBranch !== NEXT_SECURITY_RELEASE_BRANCH) {
|
48
|
+
runSync('git', ['checkout', '-B', NEXT_SECURITY_RELEASE_BRANCH]);
|
49
|
+
cli.ok(`Checkout on branch: ${NEXT_SECURITY_RELEASE_BRANCH} `);
|
50
|
+
};
|
51
|
+
}
|
52
|
+
|
53
|
+
export function commitAndPushVulnerabilitiesJSON(filePath, commitMessage, { cli, repository }) {
|
54
|
+
checkRemote(cli, repository);
|
55
|
+
|
56
|
+
if (Array.isArray(filePath)) {
|
57
|
+
for (const path of filePath) {
|
58
|
+
runSync('git', ['add', path]);
|
59
|
+
}
|
60
|
+
} else {
|
61
|
+
runSync('git', ['add', filePath]);
|
62
|
+
}
|
63
|
+
|
64
|
+
const staged = runSync('git', ['diff', '--name-only', '--cached']).trim();
|
65
|
+
if (!staged) {
|
66
|
+
cli.ok('No changes to commit');
|
67
|
+
return;
|
68
|
+
}
|
69
|
+
|
70
|
+
runSync('git', ['commit', '-m', commitMessage]);
|
71
|
+
|
72
|
+
try {
|
73
|
+
runSync('git', ['push', '-u', 'origin', NEXT_SECURITY_RELEASE_BRANCH]);
|
74
|
+
} catch (error) {
|
75
|
+
cli.warn('Rebasing...');
|
76
|
+
// try to pull rebase and push again
|
77
|
+
runSync('git', ['pull', 'origin', NEXT_SECURITY_RELEASE_BRANCH, '--rebase']);
|
78
|
+
runSync('git', ['push', '-u', 'origin', NEXT_SECURITY_RELEASE_BRANCH]);
|
79
|
+
}
|
80
|
+
cli.ok(`Pushed commit: ${commitMessage} to ${NEXT_SECURITY_RELEASE_BRANCH}`);
|
81
|
+
}
|
82
|
+
|
83
|
+
export async function getSupportedVersions() {
|
84
|
+
const supportedVersions = (await nv('supported'))
|
85
|
+
.map((v) => `${v.versionName}.x`)
|
86
|
+
.join(',');
|
87
|
+
return supportedVersions;
|
88
|
+
}
|
89
|
+
|
90
|
+
export async function getSummary(reportId, req) {
|
91
|
+
const { data } = await req.getReport(reportId);
|
92
|
+
const summaryList = data?.relationships?.summaries?.data;
|
93
|
+
if (!summaryList?.length) return;
|
94
|
+
const summaries = summaryList.filter((summary) => summary?.attributes?.category === 'team');
|
95
|
+
if (!summaries?.length) return;
|
96
|
+
return summaries?.[0].attributes?.content;
|
97
|
+
}
|
98
|
+
|
99
|
+
export function getVulnerabilitiesJSON(cli) {
|
100
|
+
const vulnerabilitiesJSONPath = path.join(process.cwd(),
|
101
|
+
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');
|
102
|
+
cli.startSpinner(`Reading vulnerabilities.json from ${vulnerabilitiesJSONPath}..`);
|
103
|
+
const file = JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf-8'));
|
104
|
+
cli.stopSpinner(`Done reading vulnerabilities.json from ${vulnerabilitiesJSONPath}`);
|
105
|
+
return file;
|
106
|
+
}
|
107
|
+
|
108
|
+
export function validateDate(releaseDate) {
|
109
|
+
const value = new Date(releaseDate).valueOf();
|
110
|
+
if (Number.isNaN(value) || value < 0) {
|
111
|
+
throw new Error('Invalid date format');
|
112
|
+
}
|
113
|
+
}
|
114
|
+
|
115
|
+
export function formatDateToYYYYMMDD(date) {
|
116
|
+
// Get year, month, and day
|
117
|
+
const year = date.getFullYear();
|
118
|
+
const month = String(date.getMonth() + 1).padStart(2, '0'); // Months are zero-based
|
119
|
+
const day = String(date.getDate()).padStart(2, '0');
|
120
|
+
|
121
|
+
// Concatenate year, month, and day with slashes
|
122
|
+
return `${year}/${month}/${day}`;
|
123
|
+
}
|
124
|
+
|
125
|
+
export function promptDependencies(cli) {
|
126
|
+
return cli.prompt('Enter the link to the dependency update PR (leave empty to exit): ', {
|
127
|
+
defaultAnswer: '',
|
128
|
+
questionType: 'input'
|
129
|
+
});
|
130
|
+
}
|
131
|
+
|
132
|
+
export async function createIssue(title, content, repository, { cli, req }) {
|
133
|
+
const data = await req.createIssue(title, content, repository);
|
134
|
+
if (data.html_url) {
|
135
|
+
cli.ok(`Created: ${data.html_url}`);
|
136
|
+
} else {
|
137
|
+
cli.error(data);
|
138
|
+
process.exit(1);
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
export async function pickReport(report, { cli, req }) {
|
143
|
+
const {
|
144
|
+
id, attributes: { title, cve_ids },
|
145
|
+
relationships: { severity, weakness, reporter }
|
146
|
+
} = report;
|
147
|
+
const link = `https://hackerone.com/reports/${id}`;
|
148
|
+
const reportSeverity = {
|
149
|
+
rating: severity?.data?.attributes?.rating || '',
|
150
|
+
cvss_vector_string: severity?.data?.attributes?.cvss_vector_string || '',
|
151
|
+
weakness_id: weakness?.data?.id || ''
|
152
|
+
};
|
153
|
+
|
154
|
+
cli.separator();
|
155
|
+
cli.info(`Report: ${link} - ${title} (${reportSeverity?.rating})`);
|
156
|
+
const include = await cli.prompt(
|
157
|
+
'Would you like to include this report to the next security release?',
|
158
|
+
{ defaultAnswer: true });
|
159
|
+
if (!include) {
|
160
|
+
return;
|
161
|
+
}
|
162
|
+
|
163
|
+
const versions = await cli.prompt('Which active release lines this report affects?', {
|
164
|
+
questionType: 'input',
|
165
|
+
defaultAnswer: await getSupportedVersions()
|
166
|
+
});
|
167
|
+
|
168
|
+
let patchAuthors = await cli.prompt(
|
169
|
+
'Add github username of the authors of the patch (split by comma if multiple)', {
|
170
|
+
questionType: 'input',
|
171
|
+
defaultAnswer: ''
|
172
|
+
});
|
173
|
+
|
174
|
+
if (!patchAuthors) {
|
175
|
+
patchAuthors = [];
|
176
|
+
} else {
|
177
|
+
patchAuthors = patchAuthors.split(',').map((p) => p.trim());
|
178
|
+
}
|
179
|
+
|
180
|
+
const summaryContent = await getSummary(id, req);
|
181
|
+
|
182
|
+
return {
|
183
|
+
id,
|
184
|
+
title,
|
185
|
+
cveIds: cve_ids,
|
186
|
+
severity: reportSeverity,
|
187
|
+
summary: summaryContent ?? '',
|
188
|
+
patchAuthors,
|
189
|
+
affectedVersions: versions.split(',').map((v) => v.replace('v', '').trim()),
|
190
|
+
link,
|
191
|
+
reporter: reporter.data.attributes.username
|
192
|
+
};
|
193
|
+
}
|
@@ -0,0 +1,182 @@
|
|
1
|
+
import fs from 'node:fs';
|
2
|
+
import path from 'node:path';
|
3
|
+
import _ from 'lodash';
|
4
|
+
import {
|
5
|
+
PLACEHOLDERS,
|
6
|
+
getVulnerabilitiesJSON,
|
7
|
+
checkoutOnSecurityReleaseBranch,
|
8
|
+
NEXT_SECURITY_RELEASE_REPOSITORY,
|
9
|
+
validateDate
|
10
|
+
} from './security-release/security-release.js';
|
11
|
+
|
12
|
+
export default class SecurityBlog {
|
13
|
+
repository = NEXT_SECURITY_RELEASE_REPOSITORY;
|
14
|
+
constructor(cli) {
|
15
|
+
this.cli = cli;
|
16
|
+
}
|
17
|
+
|
18
|
+
async createPreRelease() {
|
19
|
+
const { cli } = this;
|
20
|
+
|
21
|
+
// checkout on security release branch
|
22
|
+
checkoutOnSecurityReleaseBranch(cli, this.repository);
|
23
|
+
|
24
|
+
// read vulnerabilities JSON file
|
25
|
+
const content = getVulnerabilitiesJSON(cli);
|
26
|
+
// validate the release date read from vulnerabilities JSON
|
27
|
+
if (!content.releaseDate) {
|
28
|
+
cli.error('Release date is not set in vulnerabilities.json,' +
|
29
|
+
' run `git node security --update-date=YYYY/MM/DD` to set the release date.');
|
30
|
+
process.exit(1);
|
31
|
+
}
|
32
|
+
|
33
|
+
validateDate(content.releaseDate);
|
34
|
+
const releaseDate = new Date(content.releaseDate);
|
35
|
+
|
36
|
+
const template = this.getSecurityPreReleaseTemplate();
|
37
|
+
const data = {
|
38
|
+
annoucementDate: await this.getAnnouncementDate(cli),
|
39
|
+
releaseDate: this.formatReleaseDate(releaseDate),
|
40
|
+
affectedVersions: this.getAffectedVersions(content),
|
41
|
+
vulnerabilities: this.getVulnerabilities(content),
|
42
|
+
slug: this.getSlug(releaseDate),
|
43
|
+
impact: this.getImpact(content),
|
44
|
+
openSSLUpdate: await this.promptOpenSSLUpdate(cli)
|
45
|
+
};
|
46
|
+
const month = releaseDate.toLocaleString('en-US', { month: 'long' }).toLowerCase();
|
47
|
+
const year = releaseDate.getFullYear();
|
48
|
+
const fileName = `${month}-${year}-security-releases.md`;
|
49
|
+
const preRelease = this.buildPreRelease(template, data);
|
50
|
+
const file = path.join(process.cwd(), fileName);
|
51
|
+
fs.writeFileSync(file, preRelease);
|
52
|
+
cli.ok(`Pre-release announcement file created at ${file}`);
|
53
|
+
}
|
54
|
+
|
55
|
+
promptOpenSSLUpdate(cli) {
|
56
|
+
return cli.prompt('Does this security release containt OpenSSL updates?', {
|
57
|
+
defaultAnswer: true
|
58
|
+
});
|
59
|
+
}
|
60
|
+
|
61
|
+
formatReleaseDate(releaseDate) {
|
62
|
+
const options = {
|
63
|
+
weekday: 'long',
|
64
|
+
month: 'long',
|
65
|
+
day: 'numeric',
|
66
|
+
year: 'numeric'
|
67
|
+
};
|
68
|
+
return releaseDate.toLocaleDateString('en-US', options);
|
69
|
+
}
|
70
|
+
|
71
|
+
buildPreRelease(template, data) {
|
72
|
+
const {
|
73
|
+
annoucementDate,
|
74
|
+
releaseDate,
|
75
|
+
affectedVersions,
|
76
|
+
vulnerabilities,
|
77
|
+
slug,
|
78
|
+
impact,
|
79
|
+
openSSLUpdate
|
80
|
+
} = data;
|
81
|
+
return template.replaceAll(PLACEHOLDERS.annoucementDate, annoucementDate)
|
82
|
+
.replaceAll(PLACEHOLDERS.slug, slug)
|
83
|
+
.replaceAll(PLACEHOLDERS.affectedVersions, affectedVersions)
|
84
|
+
.replaceAll(PLACEHOLDERS.vulnerabilities, vulnerabilities)
|
85
|
+
.replaceAll(PLACEHOLDERS.releaseDate, releaseDate)
|
86
|
+
.replaceAll(PLACEHOLDERS.impact, impact)
|
87
|
+
.replaceAll(PLACEHOLDERS.openSSLUpdate, this.getOpenSSLUpdateTemplate(openSSLUpdate));
|
88
|
+
}
|
89
|
+
|
90
|
+
getOpenSSLUpdateTemplate(openSSLUpdate) {
|
91
|
+
if (openSSLUpdate) {
|
92
|
+
return '\n## OpenSSL Security updates\n\n' +
|
93
|
+
'This security release includes OpenSSL security updates\n';
|
94
|
+
}
|
95
|
+
return '';
|
96
|
+
}
|
97
|
+
|
98
|
+
getSlug(releaseDate) {
|
99
|
+
const month = releaseDate.toLocaleString('en-US', { month: 'long' });
|
100
|
+
const year = releaseDate.getFullYear();
|
101
|
+
return `${month.toLocaleLowerCase()}-${year}-security-releases`;
|
102
|
+
}
|
103
|
+
|
104
|
+
async getAnnouncementDate(cli) {
|
105
|
+
try {
|
106
|
+
const date = await this.promptAnnouncementDate(cli);
|
107
|
+
validateDate(date);
|
108
|
+
return new Date(date).toISOString();
|
109
|
+
} catch (error) {
|
110
|
+
return PLACEHOLDERS.annoucementDate;
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
promptAnnouncementDate(cli) {
|
115
|
+
const today = new Date().toISOString().substring(0, 10).replace(/-/g, '/');
|
116
|
+
return cli.prompt('When is the security release going to be announced? ' +
|
117
|
+
'Enter in YYYY/MM/DD format:', {
|
118
|
+
questionType: 'input',
|
119
|
+
defaultAnswer: today
|
120
|
+
});
|
121
|
+
}
|
122
|
+
|
123
|
+
getImpact(content) {
|
124
|
+
const impact = content.reports.reduce((acc, report) => {
|
125
|
+
for (const affectedVersion of report.affectedVersions) {
|
126
|
+
if (acc[affectedVersion]) {
|
127
|
+
acc[affectedVersion].push(report);
|
128
|
+
} else {
|
129
|
+
acc[affectedVersion] = [report];
|
130
|
+
}
|
131
|
+
}
|
132
|
+
return acc;
|
133
|
+
}, {});
|
134
|
+
|
135
|
+
const impactText = [];
|
136
|
+
for (const [key, value] of Object.entries(impact)) {
|
137
|
+
const groupedByRating = Object.values(_.groupBy(value, 'severity.rating'))
|
138
|
+
.map(severity => {
|
139
|
+
if (!severity[0]?.severity?.rating) {
|
140
|
+
this.cli.error(`severity.rating not found for the report ${severity[0].id}. \
|
141
|
+
Please add it manually before continuing.`);
|
142
|
+
process.exit(1);
|
143
|
+
}
|
144
|
+
const firstSeverityRating = severity[0].severity.rating.toLocaleLowerCase();
|
145
|
+
return `${severity.length} ${firstSeverityRating} severity issues`;
|
146
|
+
}).join(', ');
|
147
|
+
|
148
|
+
impactText.push(`The ${key} release line of Node.js is vulnerable to ${groupedByRating}.`);
|
149
|
+
}
|
150
|
+
|
151
|
+
return impactText.join('\n');
|
152
|
+
}
|
153
|
+
|
154
|
+
getVulnerabilities(content) {
|
155
|
+
const grouped = _.groupBy(content.reports, 'severity.rating');
|
156
|
+
const text = [];
|
157
|
+
for (const [key, value] of Object.entries(grouped)) {
|
158
|
+
text.push(`- ${value.length} ${key.toLocaleLowerCase()} severity issues.`);
|
159
|
+
}
|
160
|
+
return text.join('\n');
|
161
|
+
}
|
162
|
+
|
163
|
+
getAffectedVersions(content) {
|
164
|
+
const affectedVersions = new Set();
|
165
|
+
for (const report of Object.values(content.reports)) {
|
166
|
+
for (const affectedVersion of report.affectedVersions) {
|
167
|
+
affectedVersions.add(affectedVersion);
|
168
|
+
}
|
169
|
+
}
|
170
|
+
return Array.from(affectedVersions).join(', ');
|
171
|
+
}
|
172
|
+
|
173
|
+
getSecurityPreReleaseTemplate() {
|
174
|
+
return fs.readFileSync(
|
175
|
+
new URL(
|
176
|
+
'./github/templates/security-pre-release.md',
|
177
|
+
import.meta.url
|
178
|
+
),
|
179
|
+
'utf-8'
|
180
|
+
);
|
181
|
+
}
|
182
|
+
}
|
@@ -0,0 +1,274 @@
|
|
1
|
+
import {
|
2
|
+
NEXT_SECURITY_RELEASE_FOLDER,
|
3
|
+
NEXT_SECURITY_RELEASE_REPOSITORY,
|
4
|
+
checkoutOnSecurityReleaseBranch,
|
5
|
+
commitAndPushVulnerabilitiesJSON,
|
6
|
+
validateDate,
|
7
|
+
pickReport
|
8
|
+
} from './security-release/security-release.js';
|
9
|
+
import fs from 'node:fs';
|
10
|
+
import path from 'node:path';
|
11
|
+
import auth from './auth.js';
|
12
|
+
import Request from './request.js';
|
13
|
+
import nv from '@pkgjs/nv';
|
14
|
+
|
15
|
+
export default class UpdateSecurityRelease {
|
16
|
+
repository = NEXT_SECURITY_RELEASE_REPOSITORY;
|
17
|
+
constructor(cli) {
|
18
|
+
this.cli = cli;
|
19
|
+
}
|
20
|
+
|
21
|
+
async updateReleaseDate(releaseDate) {
|
22
|
+
const { cli } = this;
|
23
|
+
|
24
|
+
try {
|
25
|
+
validateDate(releaseDate);
|
26
|
+
} catch (error) {
|
27
|
+
cli.error('Invalid date format. Please use the format yyyy/mm/dd.');
|
28
|
+
process.exit(1);
|
29
|
+
}
|
30
|
+
|
31
|
+
// checkout on the next-security-release branch
|
32
|
+
checkoutOnSecurityReleaseBranch(cli, this.repository);
|
33
|
+
|
34
|
+
// update the release date in the vulnerabilities.json file
|
35
|
+
const updatedVulnerabilitiesFiles = await this.updateJSONReleaseDate(releaseDate, { cli });
|
36
|
+
|
37
|
+
const commitMessage = `chore: update the release date to ${releaseDate}`;
|
38
|
+
commitAndPushVulnerabilitiesJSON(updatedVulnerabilitiesFiles,
|
39
|
+
commitMessage, { cli, repository: this.repository });
|
40
|
+
cli.ok('Done!');
|
41
|
+
}
|
42
|
+
|
43
|
+
readVulnerabilitiesJSON(vulnerabilitiesJSONPath) {
|
44
|
+
const exists = fs.existsSync(vulnerabilitiesJSONPath);
|
45
|
+
|
46
|
+
if (!exists) {
|
47
|
+
this.cli.error(`The file vulnerabilities.json does not exist at ${vulnerabilitiesJSONPath}`);
|
48
|
+
process.exit(1);
|
49
|
+
}
|
50
|
+
|
51
|
+
return JSON.parse(fs.readFileSync(vulnerabilitiesJSONPath, 'utf8'));
|
52
|
+
}
|
53
|
+
|
54
|
+
getVulnerabilitiesJSONPath() {
|
55
|
+
return path.join(process.cwd(),
|
56
|
+
NEXT_SECURITY_RELEASE_FOLDER, 'vulnerabilities.json');
|
57
|
+
}
|
58
|
+
|
59
|
+
async updateJSONReleaseDate(releaseDate) {
|
60
|
+
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
|
61
|
+
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
|
62
|
+
content.releaseDate = releaseDate;
|
63
|
+
|
64
|
+
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
|
65
|
+
|
66
|
+
this.cli.ok(`Updated the release date in vulnerabilities.json: ${releaseDate}`);
|
67
|
+
return [vulnerabilitiesJSONPath];
|
68
|
+
}
|
69
|
+
|
70
|
+
async addReport(reportId) {
|
71
|
+
const credentials = await auth({
|
72
|
+
github: true,
|
73
|
+
h1: true
|
74
|
+
});
|
75
|
+
|
76
|
+
const req = new Request(credentials);
|
77
|
+
// checkout on the next-security-release branch
|
78
|
+
checkoutOnSecurityReleaseBranch(this.cli, this.repository);
|
79
|
+
|
80
|
+
// get h1 report
|
81
|
+
const { data: report } = await req.getReport(reportId);
|
82
|
+
const entry = await pickReport(report, { cli: this.cli, req });
|
83
|
+
|
84
|
+
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
|
85
|
+
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
|
86
|
+
content.reports.push(entry);
|
87
|
+
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
|
88
|
+
this.cli.ok(`Updated vulnerabilities.json with the report: ${entry.id}`);
|
89
|
+
const commitMessage = `chore: added report ${entry.id} to vulnerabilities.json`;
|
90
|
+
commitAndPushVulnerabilitiesJSON(vulnerabilitiesJSONPath,
|
91
|
+
commitMessage, { cli: this.cli, repository: this.repository });
|
92
|
+
this.cli.ok('Done!');
|
93
|
+
}
|
94
|
+
|
95
|
+
removeReport(reportId) {
|
96
|
+
const { cli } = this;
|
97
|
+
// checkout on the next-security-release branch
|
98
|
+
checkoutOnSecurityReleaseBranch(cli, this.repository);
|
99
|
+
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
|
100
|
+
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
|
101
|
+
const found = content.reports.some((report) => report.id === reportId);
|
102
|
+
if (!found) {
|
103
|
+
cli.error(`Report with id ${reportId} not found in vulnerabilities.json`);
|
104
|
+
process.exit(1);
|
105
|
+
}
|
106
|
+
content.reports = content.reports.filter((report) => report.id !== reportId);
|
107
|
+
fs.writeFileSync(vulnerabilitiesJSONPath, JSON.stringify(content, null, 2));
|
108
|
+
this.cli.ok(`Updated vulnerabilities.json with the report: ${reportId}`);
|
109
|
+
|
110
|
+
const commitMessage = `chore: remove report ${reportId} from vulnerabilities.json`;
|
111
|
+
commitAndPushVulnerabilitiesJSON(vulnerabilitiesJSONPath,
|
112
|
+
commitMessage, { cli, repository: this.repository });
|
113
|
+
cli.ok('Done!');
|
114
|
+
}
|
115
|
+
|
116
|
+
async requestCVEs() {
|
117
|
+
const credentials = await auth({
|
118
|
+
github: true,
|
119
|
+
h1: true
|
120
|
+
});
|
121
|
+
const vulnerabilitiesJSONPath = this.getVulnerabilitiesJSONPath();
|
122
|
+
const content = this.readVulnerabilitiesJSON(vulnerabilitiesJSONPath);
|
123
|
+
const { reports } = content;
|
124
|
+
const req = new Request(credentials);
|
125
|
+
const programId = await this.getNodeProgramId(req);
|
126
|
+
const cves = await this.promptCVECreation(req, reports, programId);
|
127
|
+
this.assignCVEtoReport(cves, reports);
|
128
|
+
this.updateVulnerabilitiesJSON(content, vulnerabilitiesJSONPath);
|
129
|
+
this.updateHackonerReportCve(req, reports);
|
130
|
+
}
|
131
|
+
|
132
|
+
assignCVEtoReport(cves, reports) {
|
133
|
+
for (const cve of cves) {
|
134
|
+
const report = reports.find(report => report.id === cve.reportId);
|
135
|
+
report.cveIds = [cve.cve_identifier];
|
136
|
+
}
|
137
|
+
}
|
138
|
+
|
139
|
+
async updateHackonerReportCve(req, reports) {
|
140
|
+
for (const report of reports) {
|
141
|
+
const { id, cveIds } = report;
|
142
|
+
this.cli.startSpinner(`Updating report ${id} with CVEs ${cveIds}..`);
|
143
|
+
const body = {
|
144
|
+
data: {
|
145
|
+
type: 'report-cves',
|
146
|
+
attributes: {
|
147
|
+
cve_ids: cveIds
|
148
|
+
}
|
149
|
+
}
|
150
|
+
};
|
151
|
+
const response = await req.updateReportCVE(id, body);
|
152
|
+
if (response.errors) {
|
153
|
+
this.cli.error(`Error updating report ${id}`);
|
154
|
+
this.cli.error(JSON.stringify(response.errors, null, 2));
|
155
|
+
}
|
156
|
+
this.cli.stopSpinner(`Done updating report ${id} with CVEs ${cveIds}..`);
|
157
|
+
}
|
158
|
+
}
|
159
|
+
|
160
|
+
updateVulnerabilitiesJSON(content, vulnerabilitiesJSONPath) {
|
161
|
+
this.cli.startSpinner(`Updating vulnerabilities.json from\
|
162
|
+
${vulnerabilitiesJSONPath}..`);
|
163
|
+
const filePath = path.resolve(vulnerabilitiesJSONPath);
|
164
|
+
fs.writeFileSync(filePath, JSON.stringify(content, null, 2));
|
165
|
+
// push the changes to the repository
|
166
|
+
commitAndPushVulnerabilitiesJSON(filePath,
|
167
|
+
'chore: updated vulnerabilities.json with CVEs',
|
168
|
+
{ cli: this.cli, repository: this.repository });
|
169
|
+
this.cli.stopSpinner(`Done updating vulnerabilities.json from ${filePath}`);
|
170
|
+
}
|
171
|
+
|
172
|
+
async promptCVECreation(req, reports, programId) {
|
173
|
+
const supportedVersions = (await nv('supported'));
|
174
|
+
const cves = [];
|
175
|
+
for (const report of reports) {
|
176
|
+
const { id, summary, title, affectedVersions, cveIds, link } = report;
|
177
|
+
// skip if already has a CVE
|
178
|
+
// risky because the CVE associated might be
|
179
|
+
// mentioned in the report and not requested by Node
|
180
|
+
if (cveIds?.length) continue;
|
181
|
+
|
182
|
+
let severity = report.severity;
|
183
|
+
|
184
|
+
if (!severity.cvss_vector_string || !severity.weakness_id) {
|
185
|
+
try {
|
186
|
+
const h1Report = await req.getReport(id);
|
187
|
+
if (!h1Report.data.relationships.severity?.data.attributes.cvss_vector_string) {
|
188
|
+
throw new Error('No severity found');
|
189
|
+
}
|
190
|
+
severity = {
|
191
|
+
weakness_id: h1Report.data.relationships.weakness?.data.id,
|
192
|
+
cvss_vector_string:
|
193
|
+
h1Report.data.relationships.severity?.data.attributes.cvss_vector_string,
|
194
|
+
rating: h1Report.data.relationships.severity?.data.attributes.rating
|
195
|
+
};
|
196
|
+
} catch (error) {
|
197
|
+
this.cli.error(`Couldnt not retrieve severity from report ${id}, skipping...`);
|
198
|
+
continue;
|
199
|
+
}
|
200
|
+
}
|
201
|
+
|
202
|
+
const { cvss_vector_string, weakness_id } = severity;
|
203
|
+
|
204
|
+
const create = await this.cli.prompt(
|
205
|
+
`Request a CVE for: \n
|
206
|
+
Title: ${title}\n
|
207
|
+
Link: ${link}\n
|
208
|
+
Affected versions: ${affectedVersions.join(', ')}\n
|
209
|
+
Vector: ${cvss_vector_string}\n
|
210
|
+
Summary: ${summary}\n`,
|
211
|
+
{ defaultAnswer: true });
|
212
|
+
|
213
|
+
if (!create) continue;
|
214
|
+
|
215
|
+
const body = {
|
216
|
+
data: {
|
217
|
+
type: 'cve-request',
|
218
|
+
attributes: {
|
219
|
+
team_handle: 'nodejs-team',
|
220
|
+
versions: await this.formatAffected(affectedVersions, supportedVersions),
|
221
|
+
metrics: [
|
222
|
+
{
|
223
|
+
vectorString: cvss_vector_string
|
224
|
+
}
|
225
|
+
],
|
226
|
+
weakness_id: Number(weakness_id),
|
227
|
+
description: title,
|
228
|
+
vulnerability_discovered_at: new Date().toISOString()
|
229
|
+
}
|
230
|
+
}
|
231
|
+
};
|
232
|
+
const { data } = await req.requestCVE(programId, body);
|
233
|
+
if (data.errors) {
|
234
|
+
this.cli.error(`Error requesting CVE for report ${id}`);
|
235
|
+
this.cli.error(JSON.stringify(data.errors, null, 2));
|
236
|
+
continue;
|
237
|
+
}
|
238
|
+
const { cve_identifier } = data.attributes;
|
239
|
+
cves.push({ cve_identifier, reportId: id });
|
240
|
+
}
|
241
|
+
return cves;
|
242
|
+
}
|
243
|
+
|
244
|
+
async getNodeProgramId(req) {
|
245
|
+
const programs = await req.getPrograms();
|
246
|
+
const { data } = programs;
|
247
|
+
for (const program of data) {
|
248
|
+
const { attributes } = program;
|
249
|
+
if (attributes.handle === 'nodejs') {
|
250
|
+
return program.id;
|
251
|
+
}
|
252
|
+
}
|
253
|
+
}
|
254
|
+
|
255
|
+
async formatAffected(affectedVersions, supportedVersions) {
|
256
|
+
const result = [];
|
257
|
+
for (const affectedVersion of affectedVersions) {
|
258
|
+
const major = affectedVersion.split('.')[0];
|
259
|
+
const latest = supportedVersions.find((v) => v.major === Number(major)).version;
|
260
|
+
const version = await this.cli.prompt(
|
261
|
+
`What is the affected version (<=) for release line ${affectedVersion}?`,
|
262
|
+
{ questionType: 'input', defaultAnswer: latest });
|
263
|
+
result.push({
|
264
|
+
vendor: 'nodejs',
|
265
|
+
product: 'node',
|
266
|
+
func: '<=',
|
267
|
+
version,
|
268
|
+
versionType: 'semver',
|
269
|
+
affected: true
|
270
|
+
});
|
271
|
+
}
|
272
|
+
return result;
|
273
|
+
}
|
274
|
+
}
|