Skip to content

Commit a5c0fcd

Browse files
committed
Protect quotes in show-ssh output
Some ssh option are quoted because their arguments may contain spaces. These quotes must be passed to ssh and not removed by the shell, so this command can work: sh -c $(limactl show-ssh $INSTANCE) Example output: ssh -o 'IdentityFile="/Users/jan/Library/Application Support/rancher-desktop/lima/_config/user"' ... Without the extra single quotes, the `sh -c` command above fails with: command-line line 0: garbage at end of line; "Support/rancher-desktop/lima/_config/user". Signed-off-by: Jan Dubois <[email protected]>
1 parent 18e98dc commit a5c0fcd

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

cmd/limactl/show_ssh.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,29 @@ func showSSHAction(cmd *cobra.Command, args []string) error {
9696
return formatSSH(w, instName, format, opts)
9797
}
9898

99+
func quoteOption(o string) string {
100+
// make sure the shell doesn't swallow quotes in option values
101+
if strings.ContainsRune(o, '"') {
102+
o = "'" + o + "'"
103+
}
104+
return o
105+
}
106+
99107
func formatSSH(w io.Writer, instName, format string, opts []string) error {
100108
fakeHostname := "lima-" + instName // corresponds to the default guest hostname
101109
switch format {
102110
case showSSHFormatCmd:
103111
args := []string{"ssh"}
104112
for _, o := range opts {
105-
args = append(args, "-o", o)
113+
args = append(args, "-o", quoteOption(o))
106114
}
107115
args = append(args, fakeHostname)
108116
// the args are similar to `limactl shell` but not exactly same. (e.g., lacks -t)
109117
fmt.Fprintln(w, strings.Join(args, " ")) // no need to use shellescape.QuoteCommand
110118
case showSSHFormatArgs:
111119
var args []string
112120
for _, o := range opts {
113-
args = append(args, "-o", o)
121+
args = append(args, "-o", quoteOption(o))
114122
}
115123
fmt.Fprintln(w, strings.Join(args, " ")) // no need to use shellescape.QuoteCommand
116124
case showSSHFormatOptions:

0 commit comments

Comments
 (0)