diff --git a/cmd/shellExecute.go b/cmd/shellExecute.go index 56d230792b..3497f5ae33 100644 --- a/cmd/shellExecute.go +++ b/cmd/shellExecute.go @@ -54,16 +54,16 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo // check input data // example for script: sources: ["./script.sh"] for position, source := range config.Sources { - + scriptPath := piperutils.SanitizePath(source) if strings.Contains(source, "https") { - scriptLocation, err := piperhttp.DownloadExecutable(config.GithubToken, utils, utils, source) + scriptLocation, err := piperhttp.DownloadExecutable(config.GithubToken, utils, utils, scriptPath) if err != nil { return errors.Wrap(err, "script download error") } - source = scriptLocation + scriptPath = scriptLocation } // check if the script is physically present - exists, err := utils.FileExists(source) + exists, err := utils.FileExists(scriptPath) if err != nil { log.Entry().WithError(err).Error("failed to check for defined script") return fmt.Errorf("failed to check for defined script: %w", err) @@ -80,7 +80,7 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo log.Entry().Info("starting running script:", source) - err = utils.RunExecutable(source, args...) + err = utils.RunExecutable(scriptPath, args...) if err != nil { log.Entry().Errorln("starting running script:", source) } diff --git a/pkg/piperutils/strings.go b/pkg/piperutils/strings.go index 2fa536a104..8ec7e1509c 100644 --- a/pkg/piperutils/strings.go +++ b/pkg/piperutils/strings.go @@ -17,3 +17,7 @@ func StringWithDefault(input, defaultValue string) string { } return inputCleared } + +func SanitizePath(input string) string { + return strings.Split(input, "?")[0] +} diff --git a/pkg/piperutils/strings_test.go b/pkg/piperutils/strings_test.go index 8f4f196acf..f7b6cf8c40 100644 --- a/pkg/piperutils/strings_test.go +++ b/pkg/piperutils/strings_test.go @@ -41,3 +41,47 @@ func TestStringWithDefault(t *testing.T) { }) } } + +func TestSanitizePath(t *testing.T) { + t.Run("URL with query parameters", func(t *testing.T) { + input := "https://example.com/dir/file.txt?param=value" + expected := "https://example.com/dir/file.txt" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("File path with query parameters", func(t *testing.T) { + input := "invalid-url/file.txt?param=value" + expected := "invalid-url/file.txt" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("Path without query parameters", func(t *testing.T) { + input := "/dir/file.txt" + expected := "/dir/file.txt" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("Multiple query parameters", func(t *testing.T) { + input := "https://api.github.com/script.sh?token=abc¶m=xyz" + expected := "https://api.github.com/script.sh" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("Local path with query", func(t *testing.T) { + input := "./script.sh?arg=value" + expected := "./script.sh" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("Empty string", func(t *testing.T) { + input := "" + expected := "" + assert.Equal(t, expected, SanitizePath(input)) + }) + + t.Run("Only query parameter", func(t *testing.T) { + input := "?param=value" + expected := "" + assert.Equal(t, expected, SanitizePath(input)) + }) +}