Skip to content

Commit e402848

Browse files
authored
Merge pull request #1501 from sam-berning/escape-envs
limactl: escape only the value of env variables in `shell`
2 parents d4c84cc + 3df580f commit e402848

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

cmd/limactl/shell.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,19 @@ func shellAction(cmd *cobra.Command, args []string) error {
128128
}
129129
script := fmt.Sprintf("%s ; exec %s --login", changeDirCmd, shell)
130130
if len(args) > 1 {
131+
quotedArgs := make([]string, len(args[1:]))
132+
parsingEnv := true
133+
for i, arg := range args[1:] {
134+
if parsingEnv && isEnv(arg) {
135+
quotedArgs[i] = quoteEnv(arg)
136+
} else {
137+
parsingEnv = false
138+
quotedArgs[i] = shellescape.Quote(arg)
139+
}
140+
}
131141
script += fmt.Sprintf(
132142
" -c %s",
133-
shellescape.Quote(shellescape.QuoteCommand(args[1:])),
143+
shellescape.Quote(strings.Join(quotedArgs, " ")),
134144
)
135145
}
136146

@@ -191,3 +201,13 @@ func shellAction(cmd *cobra.Command, args []string) error {
191201
func shellBashComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
192202
return bashCompleteInstanceNames(cmd)
193203
}
204+
205+
func isEnv(arg string) bool {
206+
return len(strings.Split(arg, "=")) > 1
207+
}
208+
209+
func quoteEnv(arg string) string {
210+
env := strings.SplitN(arg, "=", 2)
211+
env[1] = shellescape.Quote(env[1])
212+
return strings.Join(env, "=")
213+
}

0 commit comments

Comments
 (0)