Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zsh completion scripts do not properly escape help text #989

Closed
corneliusweig opened this issue Nov 8, 2019 · 3 comments
Closed

Zsh completion scripts do not properly escape help text #989

corneliusweig opened this issue Nov 8, 2019 · 3 comments

Comments

@corneliusweig
Copy link

Cobra gained a much more powerful zsh completion script generator with #646, showing help text for flags and sub-commands.

This help text is not properly escaped, for example when it contains brackets (#899). Also, and more importantly, it does not escape command substitution constructs such as $(cmd). This causes the command in the help text to be executed whenever the completion script is invoked.

Here is a minimal example to demonstrate the problem:

package main

import (
   "fmt"
   "os"

   "github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
   Use:   "zshexp",
   Short: "Source the script, then invoke completion",
   Run: func(cmd *cobra.Command, args []string) {
      _ = cmd.GenZshCompletion(os.Stdout)
      fmt.Println("compdef _zshexp zshexp") // to make it usable via `source <(./zshexp)`
   },
}

func main() {
   if err := rootCmd.Execute(); err != nil {
      fmt.Println(err)
      os.Exit(1)
   }
}

func init() {
   rootCmd.Flags().BoolP("what", "w", false, "'$(docker-machine)'")
   subCmd := &cobra.Command{
      Use:   "bla",
      Short: "$(cat ~/.bashrc)", // <<<<<<< this command gets executed when invoking the completion
   }
   rootCmd.AddCommand(subCmd)
}

To reproduce the error, do

  • go build -o whatever
  • source <(./whatever)
  • $ ./whatever <tab><tab>, this will execute the command marked above.
@corneliusweig
Copy link
Author

fix suggested in #899 (comment)

chmouel added a commit to chmouel/cobra that referenced this issue Nov 12, 2019
Closes spf13#989

Authored-by: Cornelius Weig <https://github.com/corneliusweig>
Signed-off-by: Chmouel Boudjnah <[email protected]>
chmouel added a commit to chmouel/cobra that referenced this issue Feb 11, 2020
Closes spf13#989

Authored-by: Cornelius Weig <https://github.com/corneliusweig>
Signed-off-by: Chmouel Boudjnah <[email protected]>
@github-actions
Copy link

github-actions bot commented Apr 3, 2020

This issue is being marked as stale due to a long period of inactivity

@johnSchnake
Copy link
Collaborator

#899 (comment) says the issue has been resolved. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants