From 74f3da2bb68feaac06ff2fe09ff84fa49b774f85 Mon Sep 17 00:00:00 2001 From: DongZifan <169039417+DongZifan@users.noreply.github.com> Date: Fri, 24 Apr 2026 08:40:14 +0800 Subject: [PATCH] Refine regex for branch name validation --- preview/clean.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/preview/clean.js b/preview/clean.js index f3a22e02dacd8..9a105b8115d7c 100644 --- a/preview/clean.js +++ b/preview/clean.js @@ -22,6 +22,7 @@ function Cleaner(token, repo, cache_dir, tmp_dir) { 'clone', '--bare', '--reference', `${cache_dir}/${repo_name}`, + '--', repo, local_path]) } @@ -29,11 +30,12 @@ function Cleaner(token, repo, cache_dir, tmp_dir) { return exec_git(['show-ref', '--heads'], { cwd: local_path }); } + const heads_to_prs = (heads) => { return heads .split('\n') .reduce((acc, line) => { - const found = line.match(/^.+ refs\/heads\/((.+)_(\d+))$/); + const found = line.match(/^[a-f0-9]+\s+refs\/heads\/((?!-)([a-zA-Z0-9_.-]+)_(\d+))$/); if (found) { acc.push({ branch: found[1], @@ -77,7 +79,7 @@ function Cleaner(token, repo, cache_dir, tmp_dir) { const prAge = async function (pr) { return parseInt(await exec_git( [ - "show", "--pretty=%ad", "--no-notes", "--no-patch", "--date=unix", + "show", "--pretty=%ad", "--no-notes", "--no-patch", "--date=unix","--", pr.branch ], { cwd: local_path } @@ -89,6 +91,9 @@ function Cleaner(token, repo, cache_dir, tmp_dir) { // Just for super double ultra paranoia. throw "Can't delete master!"; } + if (pr.branch.startsWith('-')) { + throw new Error(`Invalid branch name detected, possible argument injection: ${pr.branch}`); + } await exec_git( ['push', 'origin', '--delete', pr.branch], { cwd: local_path }