Skip to content

Commit ea8e744

Browse files
committed
feat(git-node): do not trust user input metadata
1 parent e0e1f41 commit ea8e744

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
@@ -286,6 +286,12 @@ export default class LandingSession extends Session {
286286
cli.warn('Not yet ready to amend, run `git node land --abort`');
287287
return;
288288
}
289+
290+
const BACKPORT_RE = /^BACKPORT-PR-URL\s*:\s*(\S+)$/i;
291+
const PR_RE = /^PR-URL\s*:\s*(\S+)$/i;
292+
const REVIEW_RE = /^Reviewed-By\s*:\s*(\S+)$/i;
293+
const CVE_RE = /^CVE-ID\s*:\s*(\S+)$/i;
294+
289295
this.startAmending();
290296

291297
const rev = this.getCurrentRev();
@@ -303,7 +309,12 @@ export default class LandingSession extends Session {
303309
}).trim().length !== 0;
304310

305311
const metadata = this.metadata.trim().split('\n');
306-
const amended = original.split('\n');
312+
// Filtering out metadata that we will add ourselves:
313+
const isFiltered = line =>
314+
(!PR_RE.test(line) || this.backport) &&
315+
!BACKPORT_RE.test(line) &&
316+
!REVIEW_RE.test(line);
317+
const amended = original.split('\n').filter(isFiltered);
307318

308319
// If the original commit message already contains trailers (such as
309320
// "Co-authored-by"), we simply add our own metadata after those. Otherwise,
@@ -313,35 +324,29 @@ export default class LandingSession extends Session {
313324
amended.push('');
314325
}
315326

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-
321327
let containCVETrailer = false;
322328
for (const line of metadata) {
323-
if (line.length !== 0 && original.includes(line)) {
324-
if (line.match(CVE_RE)) {
325-
containCVETrailer = true;
326-
}
329+
if (line.length !== 0 && original.includes(line) && !isFiltered(line)) {
330+
containCVETrailer ||= CVE_RE.test(line);
327331
if (originalHasTrailers) {
328332
cli.warn(`Found ${line}, skipping..`);
333+
continue;
329334
} else {
330335
cli.error('Git found no trailers in the original commit message, ' +
331336
`but '${line}' is present and should be a trailer.`);
332337
process.exit(1); // make it work with git rebase -x
333338
}
334-
} else {
335-
if (line.match(BACKPORT_RE)) {
336-
let prIndex = amended.findIndex(datum => datum.match(PR_RE));
337-
if (prIndex === -1) {
338-
prIndex = amended.findIndex(datum => datum.match(REVIEW_RE)) - 1;
339-
}
340-
amended.splice(prIndex + 1, 0, line);
341-
} else {
342-
amended.push(line);
339+
}
340+
if (BACKPORT_RE.test(line)) {
341+
const prIndex =
342+
(amended.findIndex(datum => PR_RE.test(datum)) + 1) ||
343+
amended.findIndex(datum => REVIEW_RE.test(datum));
344+
if (prIndex !== -1) {
345+
amended.splice(prIndex, 0, line);
346+
continue;
343347
}
344348
}
349+
amended.push(line);
345350
}
346351

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

0 commit comments

Comments
 (0)