Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/oauth/spawnargv_round2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, "'", "'\"'\"'") + "'"
Expand Down
29 changes: 29 additions & 0 deletions internal/shellwrap/shellescape_hash_test.go
Original file line number Diff line number Diff line change
@@ -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"))
}
2 changes: 1 addition & 1 deletion internal/shellwrap/shellwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading