Skip to content

Commit

Permalink
Fix completion when password name contains quotes
Browse files Browse the repository at this point in the history
RELEASE_NOTES=[BUGFIX] Fix completion when password name contains
quotes.

Signed-off-by: Patrick Decat <pdecat@gmail.com>
  • Loading branch information
pdecat committed Mar 9, 2022
1 parent b5236f7 commit e6d332c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
8 changes: 7 additions & 1 deletion internal/action/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ import (
"github.com/urfave/cli/v2"
)

var escapeRegExp = regexp.MustCompile(`(\s|\(|\)|\<|\>|\&|\;|\#|\\|\||\*|\?)`)
var escapeRegExp = regexp.MustCompile(`('|"|\s|\(|\)|\<|\>|\&|\;|\#|\\|\||\*|\?)`)

// bashEscape Escape special characters with `\`.
func bashEscape(s string) string {
return escapeRegExp.ReplaceAllStringFunc(s, func(c string) string {
if c == `\` {
return `\\\\`
}
if c == `'` {
return `\` + c
}
if c == `"` {
return `\\\` + c
}
return `\\` + c
})
}
Expand Down
24 changes: 20 additions & 4 deletions internal/action/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,26 @@ import (
)

func TestBashEscape(t *testing.T) {
expected := `a\\<\\>\\|\\\\and\\ sometimes\\?\\*\\(\\)\\&\\;\\#`
if escaped := bashEscape(`a<>|\and sometimes?*()&;#`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
t.Run("bash escape", func(t *testing.T) {
expected := `a\\<\\>\\|\\\\and\\ sometimes\\?\\*\\(\\)\\&\\;\\#`
if escaped := bashEscape(`a<>|\and sometimes?*()&;#`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})

t.Run("bash escape single quote", func(t *testing.T) {
expected := `good\\ ol\'\\ days`
if escaped := bashEscape(`good ol' days`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})

t.Run("bash escape double quote", func(t *testing.T) {
expected := `my\\ \\\"bad\\\"\\ password`
if escaped := bashEscape(`my "bad" password`); escaped != expected {
t.Errorf("Expected %q, but got %q", expected, escaped)
}
})
}

func TestComplete(t *testing.T) {
Expand Down

0 comments on commit e6d332c

Please sign in to comment.