Skip to content
10 changes: 5 additions & 5 deletions cmd/shellExecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/piperutils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ func StringWithDefault(input, defaultValue string) string {
}
return inputCleared
}

func SanitizePath(input string) string {
return strings.Split(input, "?")[0]
}
44 changes: 44 additions & 0 deletions pkg/piperutils/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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&param=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))
})
}
Loading