From 6c1db9c5e1871a541660f006c147987538089cd4 Mon Sep 17 00:00:00 2001 From: mitchell Date: Fri, 22 Mar 2024 15:13:11 -0400 Subject: [PATCH] Quote `state exec` arguments on Windows. --- internal/runners/exec/exec.go | 9 ++++++++- test/integration/exec_int_test.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/runners/exec/exec.go b/internal/runners/exec/exec.go index 5868024978..66b8779658 100644 --- a/internal/runners/exec/exec.go +++ b/internal/runners/exec/exec.go @@ -196,8 +196,15 @@ func (s *Exec) Run(params *Params, args ...string) (rerr error) { lang := language.Bash scriptArgs := fmt.Sprintf(`%q "$@"`, exeTarget) if strings.Contains(s.subshell.Binary(), "cmd") { + // Note: we cannot simply use the %* variable, because its expansion does not quote individual + // arguments. We need to manually quote and pass "%1" "%2" [...] "%255" + // See https://ss64.com/nt/syntax-args.html lang = language.Batch - scriptArgs = fmt.Sprintf("@ECHO OFF\n%q %%*", exeTarget) + params := make([]string, 256) + for i := 1; i <= 255; i++ { + params[i] = fmt.Sprintf(`"%%%d"`, i) + } + scriptArgs = fmt.Sprintf(`@ECHO OFF\n%q %s`, exeTarget, strings.Join(params, " ")) } sf, err := scriptfile.New(lang, "state-exec", scriptArgs) diff --git a/test/integration/exec_int_test.go b/test/integration/exec_int_test.go index 9f5861661e..d351d156a1 100644 --- a/test/integration/exec_int_test.go +++ b/test/integration/exec_int_test.go @@ -197,6 +197,26 @@ func (suite *ExecIntegrationTestSuite) TestExecWithPath() { } +func (suite *ExecIntegrationTestSuite) TestExecQuotedArgs() { + suite.OnlyRunForTags(tagsuite.Exec) + ts := e2e.New(suite.T(), false) + defer ts.Close() + + cp := ts.Spawn("checkout", "ActiveState-CLI/Perl-5.32", ".") + cp.Expect("Skipping runtime setup") + cp.Expect("Checked out project") + cp.ExpectExitCode(0) + + fileutils.WriteFile(filepath.Join(ts.Dirs.Work, "testargs.txt"), []byte(`printf "Argument: '%s'.\n", $ARGV[0];`)) + + cp = ts.SpawnWithOpts( + e2e.OptArgs("exec", "perl", "testargs.txt", "a