diff --git a/internal/oauth/spawnargv_round2_test.go b/internal/oauth/spawnargv_round2_test.go index 52943f279..034b44504 100644 --- a/internal/oauth/spawnargv_round2_test.go +++ b/internal/oauth/spawnargv_round2_test.go @@ -30,7 +30,7 @@ func posixShellescape(s string) string { if s == "" { return "''" } - if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~") { + if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~#") { return s } return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'" diff --git a/internal/shellwrap/shellescape_hash_test.go b/internal/shellwrap/shellescape_hash_test.go new file mode 100644 index 000000000..92cb630c4 --- /dev/null +++ b/internal/shellwrap/shellescape_hash_test.go @@ -0,0 +1,29 @@ +package shellwrap + +import ( + "os/exec" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// A word that starts with '#' begins a comment in `sh -c`, so an unquoted +// "#general" would silently drop it and every argument after it. +func TestShellescape_HashSurvivesShell(t *testing.T) { + args := []string{"#general", "--port", "3000", "a#b"} + assert.Equal(t, "'#general'", shellescapeFor("#general", false)) + + sh, err := exec.LookPath("sh") + if err != nil { + t.Skip("no POSIX sh on PATH") + } + parts := []string{"printf", "'%s\\n'"} + for _, a := range args { + parts = append(parts, shellescapeFor(a, false)) + } + out, err := exec.Command(sh, "-c", strings.Join(parts, " ")).Output() + require.NoError(t, err) + assert.Equal(t, args, strings.Split(strings.TrimSuffix(string(out), "\n"), "\n")) +} diff --git a/internal/shellwrap/shellwrap.go b/internal/shellwrap/shellwrap.go index fcc9949d4..c5127bd8e 100644 --- a/internal/shellwrap/shellwrap.go +++ b/internal/shellwrap/shellwrap.go @@ -73,7 +73,7 @@ func shellescapeFor(s string, windowsStyle bool) string { } // Unix shell special characters: if none present, return as-is. - if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~") { + if !strings.ContainsAny(s, " \t\n\r\"'\\$`;&|<>(){}[]?*~#") { return s } // Use single quotes and escape any embedded single quotes.