Git development
 help / color / mirror / Atom feed
* [PATCH] cvsexportcommit: avoid shell in -W git calls
@ 2026-11-19 19:23 Dominik Maier
  2026-11-24 22:31 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-19 19:23 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, dmnk+artist, Dominik Maier

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.

Signed-off-by: Dominik Maier <dmnk@google.com>
---
 git-cvsexportcommit.perl       | 11 +++++++----
 t/t9200-git-cvsexportcommit.sh | 19 +++++++++++++++++++
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl
index edf02f9964..ce21d1ac8f 100755
--- a/git-cvsexportcommit.perl
+++ b/git-cvsexportcommit.perl
@@ -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";
     }
 }
diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh
index 8e0272df20..74058f0128 100755
--- a/t/t9200-git-cvsexportcommit.sh
+++ b/t/t9200-git-cvsexportcommit.sh
@@ -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
-- 
2.56.1.750.gf70dc682cf-goog


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] cvsexportcommit: avoid shell in -W git calls
  2026-11-19 19:23 [PATCH] cvsexportcommit: avoid shell in -W git calls Dominik Maier
@ 2026-11-24 22:31 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2026-11-24 22:31 UTC (permalink / raw)
  To: Dominik Maier; +Cc: git, dmnk+artist

Dominik Maier <dmnk@google.com> writes:

> 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.

Indeed, Git's refname rules allow characters such as ';' and '$', so a
valid branch name can trigger shell interpretation in single-argument
system() calls.

> diff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl
> index edf02f9964..ce21d1ac8f 100755
> --- a/git-cvsexportcommit.perl
> +++ b/git-cvsexportcommit.perl
> @@ -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";
>  }

The rewrite from a single-arg system() to list form is trivially correct
and great safety improvement.

The chomping of $go_back_to in between looks somewhat out of place,
though.

> @@ -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";

We cannot tell where $commit comes from or if it is safe as it gets
prepared far away.  It came from @ARGV and cleansed by passing it to git
rev-parse --verify, so it should be safe.  But as a principle it is good
to retire the single-arg form of system(), and from that perspective,
this is a good change.

> @@ -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";

To reduce the "what's chomp doing here?" confusion, we could move the
chomp() here, but I think the posted patch as-is is probably better, out
of the principle that we should cleanse the data we obtain from outside
world as early as possible.

>      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";

And conversion of these two system() into the list form looks correct.
Even with chomp(), $go_back_to is a refname that can contain ';' and
other characters that the shell pays attention to, and using the list
form is not just nice but is necessary.

Looks good.  Thanks.

> diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh
> index 8e0272df20..74058f0128 100755
> --- a/t/t9200-git-cvsexportcommit.sh
> +++ b/t/t9200-git-cvsexportcommit.sh
> @@ -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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-24 22:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:23 [PATCH] cvsexportcommit: avoid shell in -W git calls Dominik Maier
2026-11-24 22:31 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox