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

Default syntax adds unexpected quotes #57

Closed
Brice-xCIT opened this issue Sep 25, 2019 · 6 comments
Closed

Default syntax adds unexpected quotes #57

Brice-xCIT opened this issue Sep 25, 2019 · 6 comments

Comments

@Brice-xCIT
Copy link

Hello. I ran into an issue where I'm trying to execute a command with quotes:

Running: sudo -H -u root -s bash -c "apt-get update -y"

but the default syntax added quotes in the inner command, making it fail on Ubuntu:

Ran: sudo -H -u root -s bash -c "\"apt-get" update "-y\""

fortunately the options let me set up my own implementation of syntax (I am managing escaping myself) but the method is deprecated. Maybe it would be nice to un-deprecate it? :)

@Brice-xCIT Brice-xCIT changed the title Default syntax does not correctly escapes quotes Default syntax adds unexpected quotes Sep 25, 2019
@madelson
Copy link
Owner

@Brice-xCIT can you show how you are passing the arguments?

Each argument should be passed as a separate element to the arguments array. If you combine your arguments into one string, then the library thinks you are passing a single argument containing spaces, which must be escaped.

@Brice-xCIT
Copy link
Author

Brice-xCIT commented Sep 25, 2019

@madelson Thank you for your reply.
The issue still occurs if I make sure to split each argument containing white-spaces into an array. (For instance breaking down "apt-get update -y" into "apt-get update and -y". Escaping still occurs, which would be okay if it was limited to enclosing quotes. Instead it is determined to surround each whitespace-delimited argument that contains escapable character in quotes, which breaks my final bash command...
As mentionned in #58 and here above, my workaround is a custom syntax object that bypasses this rewriting. My application has its own layer above MedallionShell, that builds and escapes commands. The custom syntax class helps during this integration phase.

@madelson
Copy link
Owner

madelson commented Sep 25, 2019

@Brice-xCIT interesting. Can you try calling it like this and seeing if that gets the expected result?

Command.Run("sudo", new[] { "-H", "-u", "root", "-s" "bash", "-c", "apt-get update -y" });

Note that I have apt-get update -y as one argument. The way command line escaping works is that as the caller you are passing a set of tokens to the command. By default, tokens break on whitespace, and quotes are used for escaping whenever a token needs to contain whitespace. Here, we want the sudo command to receive 7 tokens (I think) so we want to pass 7 arguments in the array.

If that doesn't work, can you post the exact code you're using to call MedallionShell for this command along with various things you've tried?

Another thing to look into would be to try and make your call with the underlying Process API and it's Arguments collection (just as a debugging step).

@madelson
Copy link
Owner

@Brice-xCIT any update here? Did the above suggestion work for you?

@madelson
Copy link
Owner

Closing for now; feel free to re-open if you still believe there is an issue.

@Brice-xCIT
Copy link
Author

Hi there! Sorry for not answering earlier, I had to move on to more pressing work and could not get back to this. I still cannot really, so leaving this ticket closed is okay, I will come back to it later if I still have the issue.

This said with your explanation I think I understand my mistake was to pass the 7th token split in 3 parts. Since I manage the escaping at a higher level I will keep my current solution for now. Thanks!

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