Skip to contentSkip to conversation

cvsexportcommit: avoid shell in -W git calls

PATCH New Dominik Maier 1 participant 1 message Nov 19

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cvsexportcommit: avoid shell in -W git calls

When "-W" is enabled, git-cvsexportcommit reads "$go_back_to" from "git symbolic-ref HEAD" and passes it unquoted to single-argument system("git checkout $go_back_to") and system("git symbolic-ref HEAD $go_back_to"). Valid Git branch names may contain shell metacharacters such as ';', causing Perl to invoke "/bin/sh -c" and split or execute unintended shell tokens instead of restoring HEAD.

Chomp "$go_back_to" and invoke list-form system('git', ...) for the "-W" checkout and symbolic-ref calls so git is executed directly without a shell.

---
 git-cvsexportcommit.perl       | 11 +++++++----
 t/t9200-git-cvsexportcommit.sh | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)
git-cvsexportcommit.perl +7 -4
index edf02f9964..ce21d1ac8f 100755
@@ -142,7 +142,9 @@
     $opt_v && print "Resetting to $parent\n";
     $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||
 	git rev-parse HEAD` || die "Could not determine current branch";
-    system("git checkout -q $parent^0") && die "Could not check out $parent^0";
+    chomp $go_back_to;
+    system('git', 'checkout', '-q', "$parent^0") &&
+	die "Could not check out $parent^0";
 }
 
 $opt_v && print "Applying to CVS commit $commit from parent $parent\n";
@@ -331,7 +333,7 @@
 
 print "Applying\n";
 if ($opt_W) {
-    system("git checkout -q $commit^0") && die "cannot patch";
+    system('git', 'checkout', '-q', "$commit^0") && die "cannot patch";
 } else {
     `GIT_INDEX_FILE=$tmpdir/index git apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die "cannot patch";
 }
@@ -410,9 +412,10 @@
 unlink(".cvsexportcommit.diff");
 
 if ($opt_W) {
-    system("git checkout $go_back_to") && die "cannot move back to $go_back_to";
+    system('git', 'checkout', '--end-of-options', $go_back_to, '--') &&
+	die "cannot move back to $go_back_to";
     if (!($go_back_to =~ /^[0-9a-fA-F]{$hexsz}$/)) {
-	system("git symbolic-ref HEAD $go_back_to") &&
+	system('git', 'symbolic-ref', 'HEAD', $go_back_to) &&
 	    die "cannot move back to $go_back_to";
     }
 }
t/t9200-git-cvsexportcommit.sh +19 -0
index 8e0272df20..74058f0128 100755
@@ -341,4 +341,23 @@ test_expect_success 'use the same checkout for Git and CVS' '
 
 '
 
+test_expect_success 'cvsexportcommit -W handles shell metacharacters in branch name' '
+	(
+		mkdir shared_meta &&
+		cd shared_meta &&
+		sane_unset GIT_DIR &&
+		cvs co . &&
+		git init &&
+		git checkout -b "topic;false" &&
+		echo init >meta_base &&
+		git add meta_base &&
+		git commit -m "initial" &&
+		echo added >meta_new &&
+		git add meta_new &&
+		git commit -m "add meta_new" &&
+		git cvsexportcommit -W -p -u -c HEAD &&
+		test "$(git symbolic-ref HEAD)" = "refs/heads/topic;false"
+	)
+'
+
 test_done
Signature · 1 line
2.56.1.750.gf70dc682cf-goog