Skip to content

Commit be846eb

Browse files
committed
feat(git-node): do not trust user input metadata
1 parent 8fc91ad commit be846eb

File tree

2 files changed

+24
-25
lines changed

2 files changed

+24
-25
lines changed

components/git/land.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,6 @@ async function main(state, argv, cli, dir) {
202202
}
203203
session = new LandingSession(cli, req, dir, argv);
204204
const metadata = await getMetadata(session.argv, argv.skipRefs, cli);
205-
if (argv.backport) {
206-
const split = metadata.metadata.split('\n')[0];
207-
if (split === 'PR-URL: ') {
208-
cli.error('Commit message is missing PR-URL');
209-
}
210-
}
211205
return session.start(metadata);
212206
} else if (state === APPLY) {
213207
return session.apply();

lib/landing_session.js

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ export default class LandingSession extends Session {
312312
cli.warn('Not yet ready to amend, run `git node land --abort`');
313313
return;
314314
}
315+
316+
const BACKPORT_RE = /^BACKPORT-PR-URL\s*:\s*(\S+)$/i;
317+
const PR_RE = /^PR-URL\s*:\s*(\S+)$/i;
318+
const REVIEW_RE = /^Reviewed-By\s*:\s*(\S+)$/i;
319+
const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/i;
320+
315321
this.startAmending();
316322

317323
const rev = this.getCurrentRev();
@@ -329,7 +335,12 @@ export default class LandingSession extends Session {
329335
}).trim().length !== 0;
330336

331337
const metadata = this.metadata.trim().split('\n');
332-
const amended = original.split('\n');
338+
// Filtering out metadata that we will add ourselves:
339+
const isFiltered = line =>
340+
(!PR_RE.test(line) || this.backport) &&
341+
!BACKPORT_RE.test(line) &&
342+
!REVIEW_RE.test(line);
343+
const amended = original.split('\n').filter(isFiltered);
333344

334345
// If the original commit message already contains trailers (such as
335346
// "Co-authored-by"), we simply add our own metadata after those. Otherwise,
@@ -339,35 +350,29 @@ export default class LandingSession extends Session {
339350
amended.push('');
340351
}
341352

342-
const BACKPORT_RE = /BACKPORT-PR-URL\s*:\s*(\S+)/i;
343-
const PR_RE = /PR-URL\s*:\s*(\S+)/i;
344-
const REVIEW_RE = /Reviewed-By\s*:\s*(\S+)/i;
345-
const CVE_RE = /CVE-ID\s*:\s*(\S+)/i;
346-
347353
let containCVETrailer = false;
348354
for (const line of metadata) {
349-
if (line.length !== 0 && original.includes(line)) {
350-
if (line.match(CVE_RE)) {
351-
containCVETrailer = true;
352-
}
355+
if (line.length !== 0 && original.includes(line) && !isFiltered(line)) {
356+
containCVETrailer ||= CVE_RE.test(line);
353357
if (originalHasTrailers) {
354358
cli.warn(`Found ${line}, skipping..`);
359+
continue;
355360
} else {
356361
cli.error('Git found no trailers in the original commit message, ' +
357362
`but '${line}' is present and should be a trailer.`);
358363
process.exit(1); // make it work with git rebase -x
359364
}
360-
} else {
361-
if (line.match(BACKPORT_RE)) {
362-
let prIndex = amended.findIndex(datum => datum.match(PR_RE));
363-
if (prIndex === -1) {
364-
prIndex = amended.findIndex(datum => datum.match(REVIEW_RE)) - 1;
365-
}
366-
amended.splice(prIndex + 1, 0, line);
367-
} else {
368-
amended.push(line);
365+
}
366+
if (BACKPORT_RE.test(line)) {
367+
const prIndex =
368+
(amended.findIndex(datum => PR_RE.test(datum)) + 1) ||
369+
amended.findIndex(datum => REVIEW_RE.test(datum));
370+
if (prIndex !== -1) {
371+
amended.splice(prIndex, 0, line);
372+
continue;
369373
}
370374
}
375+
amended.push(line);
371376
}
372377

373378
if (!containCVETrailer && this.includeCVE) {

0 commit comments

Comments
 (0)