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

bugfix: fix sudo -E bash -c '$cmd' with cmd that contains single quotations #4427

Merged
merged 1 commit into from
Dec 19, 2023
Merged
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
9 changes: 7 additions & 2 deletions pkg/ssh/sshcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ func (c *Client) Ping(host string) error {
}

func (c *Client) wrapCommands(cmds ...string) string {
cmdJoined := strings.Join(cmds, "; ")
if !c.Option.sudo || c.Option.user == defaultUsername {
return strings.Join(cmds, "; ")
return cmdJoined
}
return fmt.Sprintf("sudo -E /bin/bash -c '%s'", strings.Join(cmds, "; "))

// Escape single quotes in cmd, fix https://github.com/labring/sealos/issues/4424
// e.g. echo 'hello world' -> `sudo -E /bin/bash -c 'echo "hello world"'`
cmdEscaped := strings.ReplaceAll(cmdJoined, `'`, `"`)
return fmt.Sprintf("sudo -E /bin/bash -c '%s'", cmdEscaped)
}

func (c *Client) CmdAsyncWithContext(ctx context.Context, host string, cmds ...string) error {
Expand Down
Loading