{"thread":{"id":"66890","subject":"[PATCH] cvsexportcommit: avoid shell in -W git calls","startedAt":"2026-11-19T19:23:21Z","lastAt":"2026-11-19T19:23:21Z","messageCount":1,"participants":["Dominik Maier"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"558883","messageId":"20261119192321.2690660-1-dmnk@google.com","threadId":"66890","inReplyTo":null,"subject":"[PATCH] cvsexportcommit: avoid shell in -W git calls","fromName":"Dominik Maier","fromEmail":"dmnk@google.com","sentAt":"2026-11-19T19:23:21Z","receivedAt":"2026-11-19T19:23:21Z","isPatch":true,"sender":{"key":"dmnk@google.com","avatar":null},"body":"When \"-W\" is enabled, git-cvsexportcommit reads \"$go_back_to\" from\n\"git symbolic-ref HEAD\" and passes it unquoted to single-argument\nsystem(\"git checkout $go_back_to\") and system(\"git symbolic-ref HEAD\n$go_back_to\"). Valid Git branch names may contain shell metacharacters\nsuch as ';', causing Perl to invoke \"/bin/sh -c\" and split or execute\nunintended shell tokens instead of restoring HEAD.\n\nChomp \"$go_back_to\" and invoke list-form system('git', ...) for the \"-W\"\ncheckout and symbolic-ref calls so git is executed directly without a\nshell.\n\nSigned-off-by: Dominik Maier <dmnk@google.com>\n---\n git-cvsexportcommit.perl       | 11 +++++++----\n t/t9200-git-cvsexportcommit.sh | 19 +++++++++++++++++++\n 2 files changed, 26 insertions(+), 4 deletions(-)\n\ndiff --git a/git-cvsexportcommit.perl b/git-cvsexportcommit.perl\nindex edf02f9964..ce21d1ac8f 100755\n--- a/git-cvsexportcommit.perl\n+++ b/git-cvsexportcommit.perl\n@@ -142,7 +142,9 @@\n     $opt_v && print \"Resetting to $parent\\n\";\n     $go_back_to = `git symbolic-ref HEAD 2> /dev/null ||\n \tgit rev-parse HEAD` || die \"Could not determine current branch\";\n-    system(\"git checkout -q $parent^0\") && die \"Could not check out $parent^0\";\n+    chomp $go_back_to;\n+    system('git', 'checkout', '-q', \"$parent^0\") &&\n+\tdie \"Could not check out $parent^0\";\n }\n \n $opt_v && print \"Applying to CVS commit $commit from parent $parent\\n\";\n@@ -331,7 +333,7 @@\n \n print \"Applying\\n\";\n if ($opt_W) {\n-    system(\"git checkout -q $commit^0\") && die \"cannot patch\";\n+    system('git', 'checkout', '-q', \"$commit^0\") && die \"cannot patch\";\n } else {\n     `GIT_INDEX_FILE=$tmpdir/index git apply $context --summary --numstat --apply <.cvsexportcommit.diff` || die \"cannot patch\";\n }\n@@ -410,9 +412,10 @@\n unlink(\".cvsexportcommit.diff\");\n \n if ($opt_W) {\n-    system(\"git checkout $go_back_to\") && die \"cannot move back to $go_back_to\";\n+    system('git', 'checkout', '--end-of-options', $go_back_to, '--') &&\n+\tdie \"cannot move back to $go_back_to\";\n     if (!($go_back_to =~ /^[0-9a-fA-F]{$hexsz}$/)) {\n-\tsystem(\"git symbolic-ref HEAD $go_back_to\") &&\n+\tsystem('git', 'symbolic-ref', 'HEAD', $go_back_to) &&\n \t    die \"cannot move back to $go_back_to\";\n     }\n }\ndiff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh\nindex 8e0272df20..74058f0128 100755\n--- a/t/t9200-git-cvsexportcommit.sh\n+++ b/t/t9200-git-cvsexportcommit.sh\n@@ -341,4 +341,23 @@ test_expect_success 'use the same checkout for Git and CVS' '\n \n '\n \n+test_expect_success 'cvsexportcommit -W handles shell metacharacters in branch name' '\n+\t(\n+\t\tmkdir shared_meta &&\n+\t\tcd shared_meta &&\n+\t\tsane_unset GIT_DIR &&\n+\t\tcvs co . &&\n+\t\tgit init &&\n+\t\tgit checkout -b \"topic;false\" &&\n+\t\techo init >meta_base &&\n+\t\tgit add meta_base &&\n+\t\tgit commit -m \"initial\" &&\n+\t\techo added >meta_new &&\n+\t\tgit add meta_new &&\n+\t\tgit commit -m \"add meta_new\" &&\n+\t\tgit cvsexportcommit -W -p -u -c HEAD &&\n+\t\ttest \"$(git symbolic-ref HEAD)\" = \"refs/heads/topic;false\"\n+\t)\n+'\n+\n test_done\n-- \n2.56.1.750.gf70dc682cf-goog\n\n\n"}]}