Skip to content
11 changes: 6 additions & 5 deletions cmd/shellExecute.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo
// check input data
// example for script: sources: ["./script.sh"]
for position, source := range config.Sources {

sanitizedSource := piperutils.SanitizePath(source)
localSource := sanitizedSource
if strings.Contains(source, "https") {
scriptLocation, err := piperhttp.DownloadExecutable(config.GithubToken, utils, utils, source)
scriptLocation, err := piperhttp.DownloadExecutable(config.GithubToken, utils, utils, sanitizedSource)
if err != nil {
return errors.Wrap(err, "script download error")
}
source = scriptLocation
localSource = scriptLocation
}
// check if the script is physically present
exists, err := utils.FileExists(source)
exists, err := utils.FileExists(localSource)
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 +81,7 @@ func runShellExecute(config *shellExecuteOptions, telemetryData *telemetry.Custo

log.Entry().Info("starting running script:", source)

err = utils.RunExecutable(source, args...)
err = utils.RunExecutable(localSource, args...)
if err != nil {
log.Entry().Errorln("starting running script:", source)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/piperutils/sanitize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package piperutils

import (
"strings"
)

// SanitizePath removes query parameters from a URL or file path
func SanitizePath(input string) string {
return strings.Split(input, "?")[0]
}
51 changes: 51 additions & 0 deletions pkg/piperutils/sanitize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package piperutils

import (
"testing"

"github.com/stretchr/testify/assert"
)

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