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

Modify runRemoteSshCommand to quote a command array #148

Open
rgl opened this issue Sep 22, 2021 · 3 comments
Open

Modify runRemoteSshCommand to quote a command array #148

rgl opened this issue Sep 22, 2021 · 3 comments

Comments

@rgl
Copy link
Contributor

rgl commented Sep 22, 2021

The runRemoteSshCommand function accepts a command string, but it should be modified to receive a []string. That way, it could properly escape/quote the command arguments, preventing unwanted errors.

The function signature could be changed to:

func runRemoteSshCommand(esxiConnInfo ConnectionStruct, shortCmdDesc string, remoteSshCommand ...string) (string, error)

We could use (as a library) the shellescape.QuoteCommand from https://github.com/alessio/shellescape/blob/be0896646a3f2d59dc1de4d149beb07c5b2c25c0/shellescape.go#L43-L53

What do you think?

@josenk
Copy link
Owner

josenk commented Sep 22, 2021

What is the use-case? Is something broke? Do you have an example of something that would be fixed with this addition?

@rgl
Copy link
Contributor Author

rgl commented Sep 23, 2021

The use-case if for preventing shell injections. This is a pet peeve of mine, getting rid of constructs that are prone to injection kind of problems, normally due to the lack of user input validation/escaping. And manually constructed command line arguments that receive user input is one of those cases. Normally these can be spotted in "printf"-like usages.

For example, these cases are not obvious if they are properly escaping the input arguments (at least, they seem to miss escaping the double quotes):

remote_cmd = fmt.Sprintf("ls -d %s", boot_disk_vmdkPATH)

remote_cmd = fmt.Sprintf("ls -d \"%s\"", fullPATH)

remote_cmd := fmt.Sprintf("vim-cmd vmsvc/get.summary %s", vmid)

This all comes down for not having a proper api (even more important when this is part of infrastructure/library code), which does the proper escaping for the developer. Having a proper api frees your brain from that concern, and lets you make a more strait forwarded code that is easier to reason about and review.

For another example, this one does not seem to escape the resource_pool_id variable that is a regular expression input:

remote_cmd := fmt.Sprintf("grep -A1 '<objID>%s</objID>' /etc/vmware/hostd/pools.xml | grep '<path>'", resource_pool_id)

@josenk
Copy link
Owner

josenk commented Sep 26, 2021

Although, there's no elevation of privilege, so there's really no risk that it's a back door to anything.

You are right to fix it. I'll take a look when I get a chance.

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