Skip to content

Commit 72bf756

Browse files
authored
feat(git-node): select security release PRs from affectedVersions (#1124)
`git node release --prepare --security` now selects the PRs to cherry-pick from the per-line `affectedVersions` mapping of each report and dependency, instead of scanning open PRs and filtering by label. - `--security` accepts the path to vulnerabilities.json directly (or the security-release repository root). - Reports and dependency updates are picked for the release line being prepared, resolving the owner/repo from the PR URL so both node-private and public node PRs are supported. - CVE-ID trailers are taken from the matching report. - Dependency updates now use the same `affectedVersions` schema as the reports, updating the generator and the `git node security` consumer. Removed the `--filterLabel` option and the open PR scan.
1 parent f2f4ac5 commit 72bf756

7 files changed

Lines changed: 268 additions & 90 deletions

File tree

‎components/git/release.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ const PROMOTE = 'promote';
1414
const RELEASERS = 'releasers';
1515

1616
const releaseOptions = {
17-
filterLabel: {
18-
describe: 'Labels separated by "," to filter security PRs',
19-
type: 'string'
20-
},
2117
'gpg-sign': {
2218
describe: 'GPG-sign commits, will be passed to the git process',
2319
alias: 'S'
@@ -51,7 +47,9 @@ const releaseOptions = {
5147
},
5248
security: {
5349
describe: 'Demarcate the new security release as a security release. ' +
54-
'Optionally provide path to security-release repository for CVE auto-population',
50+
'Optionally provide the path to vulnerabilities.json (or the ' +
51+
'security-release repository) to cherry-pick the reports and ' +
52+
'dependency updates for this release line and auto-populate CVEs',
5553
type: 'string',
5654
coerce: (arg) => {
5755
// If --security=path is used, return the path

‎components/git/security.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import SecurityBlog from '../../lib/security_blog.js';
1212
import SecurityAnnouncement from '../../lib/security-announcement.js';
1313
import { forceRunAsync } from '../../lib/run.js';
1414
import { getAffectedVersionLines } from '../../lib/security-release/security-release.js';
15+
import { getPullRequestURLForLine } from '../../lib/prepare_release.js';
1516

1617
export const command = 'security [options]';
1718
export const describe = 'Manage an in-progress security release or start a new one.';
@@ -305,9 +306,12 @@ async function applySecurityPatches(cli) {
305306
const { releaseDate, reports, dependencies } = await fetchVulnerabilitiesDotJSON(cli, req);
306307

307308
let patchedVersion;
308-
for (const { affectedVersions, prURL, title } of Object.values(dependencies).flat()) {
309-
if (!getAffectedVersionLines(affectedVersions).includes(`${nodeMajorVersion}.x`)) continue;
310-
cli.separator(`Taking care of ${title}...`);
309+
const releaseLine = `${nodeMajorVersion}.x`;
310+
for (const [name, dep] of Object.entries(dependencies ?? {})) {
311+
const prURL = getPullRequestURLForLine(
312+
dep.affectedVersions, releaseLine, dep.prURL);
313+
if (!prURL) continue;
314+
cli.separator(`Taking care of ${name}...`);
311315
if (await skipIfExisting(cli, prURL)) continue;
312316

313317
const argv = parsePRFromURL(prURL);

‎lib/prepare_release.js‎

Lines changed: 133 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,48 @@ import { replaceInFile } from 'replace-in-file';
66

77
import { forceRunAsync, runAsync, runSync } from './run.js';
88
import { writeJson, readJson } from './file.js';
9-
import Request from './request.js';
10-
import auth from './auth.js';
119
import {
1210
getEOLDate,
1311
getStartLTSBlurb,
1412
updateTestProcessRelease
1513
} from './release/utils.js';
1614
import CherryPick from './cherry_pick.js';
1715
import Session from './session.js';
16+
import { getAffectedVersionLines } from './security-release/security-release.js';
1817

1918
const isWindows = process.platform === 'win32';
2019

20+
// Parse a GitHub Pull Request URL into its owner, repo and number.
21+
// Returns null when the URL does not look like a PR URL.
22+
export function parsePullRequestURL(url) {
23+
const match = /github\.com\/([^/]+)\/([^/]+)\/pull\/(\d+)/.exec(url || '');
24+
if (!match) return null;
25+
return { owner: match[1], repo: match[2], number: Number(match[3]) };
26+
}
27+
28+
// Resolve the PR URL that fixes a given release line from a report or
29+
// dependency `affectedVersions` entry.
30+
// Supports the current schema, where `affectedVersions` maps a release line to
31+
// its PR URL (e.g. `{ '22.x': 'https://.../pull/1' }`), and the legacy schema,
32+
// where `affectedVersions` is an array of lines paired with a single `prURL`.
33+
export function getPullRequestURLForLine(affectedVersions, line, legacyPrURL) {
34+
if (
35+
affectedVersions &&
36+
typeof affectedVersions === 'object' &&
37+
!Array.isArray(affectedVersions)
38+
) {
39+
return affectedVersions[line] || null;
40+
}
41+
42+
if (Array.isArray(affectedVersions) && legacyPrURL) {
43+
return getAffectedVersionLines(affectedVersions).includes(line)
44+
? legacyPrURL
45+
: null;
46+
}
47+
48+
return null;
49+
}
50+
2151
export default class ReleasePreparation extends Session {
2252
constructor(argv, cli, dir) {
2353
super(cli, dir);
@@ -32,101 +62,137 @@ export default class ReleasePreparation extends Session {
3262
this.defaultReleaseDate = argv.releaseDate ?? new Date().toISOString().slice(0, 10);
3363
this.ltsCodename = '';
3464
this.date = '';
35-
this.filterLabels = argv.filterLabel && argv.filterLabel.split(',');
3665
this.newVersion = argv.newVersion;
3766
}
3867

3968
get branch() {
4069
return this.isSecurityRelease ? this.releaseBranch : this.stagingBranch;
4170
}
4271

43-
warnForNonMergeablePR(pr) {
72+
// Resolve the path to the security release vulnerabilities.json.
73+
// `--security` may point either directly at a vulnerabilities.json file or at
74+
// the root of the security-release repository, in which case the well-known
75+
// sub-path is appended.
76+
getVulnerabilitiesJSONPath() {
77+
if (!this.securityReleaseRepo) return null;
78+
if (this.securityReleaseRepo.endsWith('.json')) {
79+
return this.securityReleaseRepo;
80+
}
81+
return path.join(
82+
this.securityReleaseRepo,
83+
'security-release',
84+
'next-security-release',
85+
'vulnerabilities.json'
86+
);
87+
}
88+
89+
loadVulnerabilitiesJSON() {
4490
const { cli } = this;
91+
const vulnPath = this.getVulnerabilitiesJSONPath();
4592

46-
cli.warn(`PR#${pr.number} - ${pr.title} is not 'MERGEABLE'.
47-
Status: ${pr.mergeable}`);
93+
if (!vulnPath) {
94+
cli.error('No vulnerabilities.json path provided. Pass the path to ' +
95+
'vulnerabilities.json (or the security-release repository) via ' +
96+
'`--security=<path>`.');
97+
return null;
98+
}
99+
100+
if (!existsSync(vulnPath)) {
101+
cli.error(`vulnerabilities.json not found at ${vulnPath}.`);
102+
return null;
103+
}
104+
105+
try {
106+
cli.startSpinner(`Reading vulnerabilities.json from ${vulnPath}..`);
107+
const vulnData = JSON.parse(readFileSync(vulnPath, 'utf-8'));
108+
cli.stopSpinner(`Done reading vulnerabilities.json from ${vulnPath}`);
109+
return vulnData;
110+
} catch (err) {
111+
cli.error(`Failed to read vulnerabilities.json: ${err.message}`);
112+
return null;
113+
}
48114
}
49115

50-
async getOpenPRs(filterLabels) {
51-
const credentials = await auth({ github: true });
52-
const request = new Request(credentials);
53-
const data = await request.gql('PRs', {
54-
owner: this.owner,
55-
repo: this.repo,
56-
labels: filterLabels
57-
});
58-
return data.repository.pullRequests.nodes;
116+
// Build the list of PRs to cherry-pick for a given release line from both the
117+
// reports and the dependency updates, using their `affectedVersions` mapping.
118+
getSecurityCherryPickTargets(vulnData, line) {
119+
const targets = [];
120+
121+
for (const report of vulnData.reports ?? []) {
122+
const url = getPullRequestURLForLine(
123+
report.affectedVersions, line, report.prURL);
124+
if (url) {
125+
targets.push({
126+
url,
127+
cveIds: report.cveIds?.length ? report.cveIds : null,
128+
label: report.title || report.id
129+
});
130+
}
131+
}
132+
133+
for (const [name, dep] of Object.entries(vulnData.dependencies ?? {})) {
134+
const url = getPullRequestURLForLine(
135+
dep.affectedVersions, line, dep.prURL);
136+
if (url) {
137+
targets.push({ url, cveIds: null, label: `dependency: ${name}` });
138+
}
139+
}
140+
141+
return targets;
142+
}
143+
144+
upstreamForPR({ owner, repo }) {
145+
if (owner === 'nodejs-private') {
146+
return `https://${this.username}:${this.config.token}` +
147+
`@github.com/${owner}/${repo}.git`;
148+
}
149+
return `https://github.com/${owner}/${repo}.git`;
59150
}
60151

61-
async cherryPickSecurityPRs(filterLabels) {
152+
async cherryPickSecurityPRs() {
62153
const { cli } = this;
154+
const line = `${this.versionComponents.major}.x`;
63155

64-
const prs = await this.getOpenPRs(filterLabels);
65-
if (prs.length === 0) {
66-
cli.warn(`There are no PRs available in ${this.owner}/${this.repo}`);
156+
const vulnData = this.loadVulnerabilitiesJSON();
157+
if (!vulnData) {
158+
cli.error('Cannot proceed with a security release without a valid ' +
159+
'vulnerabilities.json.');
67160
return false;
68161
}
69162

70-
const vulnCveMap = new Map();
71-
if (this.isSecurityRelease && this.securityReleaseRepo) {
72-
const vulnPath = path.join(
73-
this.securityReleaseRepo,
74-
'security-release',
75-
'next-security-release',
76-
'vulnerabilities.json'
77-
);
78-
79-
if (!existsSync(vulnPath)) {
80-
cli.error(`vulnerabilities.json not found at ${vulnPath}. ` +
81-
'Skipping CVE auto-population.');
82-
cli.warn('PRs will require manual CVE-ID entry.');
83-
} else {
84-
try {
85-
cli.startSpinner(`Reading vulnerabilities.json from ${vulnPath}..`);
86-
const vulnData = JSON.parse(readFileSync(vulnPath, 'utf-8'));
87-
cli.stopSpinner(`Done reading vulnerabilities.json from ${vulnPath}`);
88-
89-
if (vulnData.reports && Array.isArray(vulnData.reports)) {
90-
vulnData.reports.forEach(report => {
91-
if (report.prURL && report.cveIds && report.cveIds.length > 0) {
92-
vulnCveMap.set(report.prURL, report.cveIds);
93-
}
94-
});
95-
}
96-
cli.ok(`Loaded ${vulnCveMap.size} CVE mappings from vulnerabilities.json`);
97-
} catch (err) {
98-
cli.error(`Failed to read vulnerabilities.json: ${err.message}`);
99-
cli.warn('Continuing without CVE auto-population.');
100-
}
101-
}
163+
const targets = this.getSecurityCherryPickTargets(vulnData, line);
164+
if (targets.length === 0) {
165+
cli.warn(`No PRs found in vulnerabilities.json for release line ${line}.`);
166+
return false;
102167
}
103168

104-
for (const pr of prs) {
105-
if (pr.mergeable !== 'MERGEABLE') {
106-
this.warnForNonMergeablePR(pr);
107-
}
169+
cli.info(`Cherry-picking ${targets.length} PR(s) for ${line} from ` +
170+
'vulnerabilities.json');
108171

109-
// Look up CVE-IDs from vulnerabilities.json
110-
const prUrl = `https://github.com/${this.owner}/${this.repo}/pull/${pr.number}`;
111-
const cveIds = vulnCveMap.get(prUrl);
172+
for (const target of targets) {
173+
const pr = parsePullRequestURL(target.url);
174+
if (!pr) {
175+
cli.warn(`Skipping invalid PR URL for ${target.label}: ${target.url}`);
176+
continue;
177+
}
112178

113-
if (!cveIds || cveIds.length === 0) {
114-
cli.warn(`No CVE-IDs found in vulnerabilities.json for ${prUrl}`);
179+
if (!target.cveIds) {
180+
cli.warn(`No CVE-IDs found in vulnerabilities.json for ${target.url}`);
115181
}
116182

183+
cli.info(`Cherry-picking ${target.label} (${target.url})`);
117184
const cp = new CherryPick(pr.number, this.dir, cli, {
118-
owner: this.owner,
119-
repo: this.repo,
185+
owner: pr.owner,
186+
repo: pr.repo,
120187
gpgSign: this.gpgSign,
121-
upstream: this.isSecurityRelease ? `https://${this.username}:${this.config.token}@github.com/${this.owner}/${this.repo}.git` : this.upstream,
188+
upstream: this.upstreamForPR(pr),
122189
lint: false,
123190
includeCVE: true,
124-
cveIds: cveIds || null,
125-
vulnCveMap
191+
cveIds: target.cveIds
126192
});
127193
const success = await cp.start();
128194
if (!success) {
129-
cli.warn('The cherry-pick has failed. PR-ID: ' + pr.number);
195+
cli.warn(`The cherry-pick has failed. PR: ${target.url}`);
130196
return false;
131197
}
132198
}
@@ -147,7 +213,7 @@ export default class ReleasePreparation extends Session {
147213
{ defaultAnswer: this.runBranchDiff });
148214
if (runBranchDiff) {
149215
if (isSecurityRelease) {
150-
const success = await this.cherryPickSecurityPRs(this.filterLabels);
216+
const success = await this.cherryPickSecurityPRs();
151217
if (!success) {
152218
cli.error('Aborting security release preparation.');
153219
return;

‎lib/prepare_security.js‎

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import {
1717
writeSecurityFile,
1818
SecurityRelease
1919
} from './security-release/security-release.js';
20-
import _ from 'lodash';
2120

2221
function relativeDate(date) {
2322
const days = Math.floor((Date.now() - date) / (1000 * 60 * 60 * 24));
@@ -261,8 +260,7 @@ export default class PrepareSecurityRelease extends SecurityRelease {
261260
const reports = reportSelectionMode === 'include-all'
262261
? await this.includeAllTriagedReports(excludedReports)
263262
: await this.chooseReports(excludedReports);
264-
const depUpdates = await this.getDependencyUpdates();
265-
const deps = _.groupBy(depUpdates, 'name');
263+
const deps = await this.getDependencyUpdates();
266264

267265
// create the vulnerabilities.json file in the security-release repo
268266
const filePath = await this.createVulnerabilitiesJSON(reports, deps, releaseDate);
@@ -558,8 +556,11 @@ export default class PrepareSecurityRelease extends SecurityRelease {
558556
process.exit(1);
559557
}
560558

559+
// Collect dependency updates using the same schema as the reports: each
560+
// dependency maps every affected release line to the PR that lands it, e.g.
561+
// { undici: { affectedVersions: { '22.x': '<prURL>', '24.x': '<prURL>' } } }
561562
async getDependencyUpdates() {
562-
const deps = [];
563+
const deps = {};
563564
const updates = await this.cli.prompt('Are there dependency updates in this security release?',
564565
{
565566
defaultAnswer: true,
@@ -583,20 +584,24 @@ export default class PrepareSecurityRelease extends SecurityRelease {
583584
});
584585

585586
const versions = await this.cli.prompt(
586-
'Which release line does this dependency update affect?', {
587+
'Which release line(s) does this dependency update PR affect? ' +
588+
'(comma-separated)', {
587589
defaultAnswer: supportedVersions,
588590
questionType: 'input'
589591
});
590592

591593
try {
592594
const res = await this.req.getPullRequest(dep);
593-
const { html_url, title } = res;
594-
deps.push({
595-
name,
596-
url: html_url,
597-
title,
598-
affectedVersions: versions.split(',').map((v) => v.replace('v', '').trim())
599-
});
595+
const { html_url: prURL } = res;
596+
const affectedVersions = versions
597+
.split(',')
598+
.map((v) => v.replace('v', '').trim())
599+
.filter(Boolean);
600+
601+
deps[name] ??= { affectedVersions: {} };
602+
for (const line of affectedVersions) {
603+
deps[name].affectedVersions[line] = prURL;
604+
}
600605
this.cli.separator();
601606
} catch (error) {
602607
this.cli.error('Invalid PR url. Please provide a valid PR url.');

‎lib/security_blog.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,13 @@ export default class SecurityBlog extends SecurityRelease {
287287
if (Object.keys(dependencyUpdates).length === 0) return '';
288288
let template = '\nThis security release includes the following dependency' +
289289
' updates to address public vulnerabilities:\n';
290-
for (const [dependency, { versions, affectedVersions }] of Object.entries(dependencyUpdates)) {
291-
const releaseLines = getAffectedVersionLines(affectedVersions);
292-
template += `- ${dependency} (${versions.join(', ')}) on ${releaseLines.join(', ')}\n`;
290+
for (const [dependency, entry] of Object.entries(dependencyUpdates)) {
291+
const releaseLines = getAffectedVersionLines(entry.affectedVersions);
292+
const versions = Array.isArray(entry.versions)
293+
? entry.versions.map((v) => (typeof v === 'string' ? v : v.version))
294+
: [];
295+
const versionSuffix = versions.length ? ` (${versions.join(', ')})` : '';
296+
template += `- ${dependency}${versionSuffix} on ${releaseLines.join(', ')}\n`;
293297
}
294298
return template;
295299
}

0 commit comments

Comments
 (0)