Skip to content

Sanitize arguments to external commands a bit better - #4157

Merged
fdncred merged 6 commits into
nushell:mainfrom
BraulioVM:fix/4140
Nov 29, 2021
Merged

fdncred merged 6 commits into
nushell:mainfrom
BraulioVM:fix/4140

Conversation

@BraulioVM

Copy link
Copy Markdown
Contributor

Fixes #4140

We are passing commands into a shell underneath but we were not
escaping arguments correctly. This new version of the code also takes
into consideration the ";" and "&" characters, which have special
meaning in shells.

We would probably benefit from a more robust way to join arguments to
shell programs. Python's stdlib has shlex.join, and perhaps we can
take that implementation as a reference.

Testing: I have added a few unit tests to the escape_where_needed function. Ideally I would add tests that
run an external command and verify that that works, like ^echo aa;bbb, but I couldn't find any test like that
that I could take as reference. Also I have no idea if this test would also apply to windows because I'm
not familiar with Windows shells. Testing manually on my build of nushell did work.

We are passing commands into a shell underneath but we were not
escaping arguments correctly. This new version of the code also takes
into consideration the ";" and "&" characters, which have special
meaning in shells.

We would probably benefit from a more robust way to join arguments to
shell programs. Python's stdlib has shlex.join, and perhaps we can
take that implementation as a reference.
@BraulioVM

Copy link
Copy Markdown
Contributor Author

Oh ok robust sanitization is much easier than I thought. It's just a matter of enclosing each argument in single-quotes (https://github.com/python/cpython/blob/main/Lib/shlex.py#L325). Will fix

I believe the right place to do escaping of arguments was in the
spawn_sh_command function. Note that this change prevents things like:

^echo "$(ls)"

from executing the ls command. Instead, this will just print

$(ls)

The regex has been taken from the python stdlib implementation of shlex.quote
@BraulioVM

BraulioVM commented Nov 26, 2021 •

Copy link
Copy Markdown
Contributor Author

From my second commit message:

Note that this change prevents things like:

^echo "$(ls)"

from executing the ls command (within the subshell). Instead, this will just print

$(ls)

Is this the behavior that we want? My guess is that if the user wants to use something like $(ls) they can do it
in nu, and not pass the $(ls) to a subshell

@BraulioVM

Copy link
Copy Markdown
Contributor Author
=== stderr

thread 'shell::pipeline::commands::external::it_evaluation::takes_rows_of_nu_value_strings' panicked at 'assertion failed: `(left == right)`
  left: `"\"jonathan_likes_cake.txt\""`,
 right: `"jonathan_likes_cake.txt"`', tests/shell/pipeline/commands/external.rs:114:13

---- shell::pipeline::commands::external::nu_commands::echo_internally_externally stdout ----

Ah so that's where the tests for this stuff were haha

@BraulioVM

BraulioVM commented Nov 26, 2021 •

Copy link
Copy Markdown
Contributor Author

Ok after looking at the failing tests, I think this is going to be a bit harder than I thought... Maybe the solution in 09082ac is better.. I will keep looking into it tomorrow

Edit: nevermind, I fixed it, but I still want to add a few tests

@danimesq

Copy link
Copy Markdown
Contributor

This new version of the code also takes
into consideration the ";" and "&" characters, which have special
meaning in shells.

Characters like "(" are also slipping from quotes.

@BraulioVM

Copy link
Copy Markdown
Contributor Author

Characters like "(" are also slipping from quotes.

That's fixed in my build

/home/braulio/hacks/nushell(fix/4140)> ^cowsay "a;(&(ls)"
 __________
< a;(&(ls) >
 ----------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

I have added the tests I meant to add. I think this is ready for review

@danimesq

danimesq commented Nov 27, 2021 •

Copy link
Copy Markdown
Contributor

I have added the tests I meant to add. I think this is ready for review

@fdncred ❤

@fdncred

fdncred commented Nov 29, 2021

Copy link
Copy Markdown
Contributor

Thanks!

@fdncred
fdncred merged commit 1794ad5 into nushell:main Nov 29, 2021
@danimesq

Copy link
Copy Markdown
Contributor

When it will be included in the next release?

@fdncred

fdncred commented Nov 29, 2021

Copy link
Copy Markdown
Contributor

you can see it in the #general channel in discord, which says Dec 7.

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

Successfully merging this pull request may close these issues.

";" is slipping from quotes such as a git commit message

3 participants