Repository navigation
Sanitize arguments to external commands a bit better - #4157
Conversation
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.
|
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
|
From my second commit message: Is this the behavior that we want? My guess is that if the user wants to use something like $(ls) they can do it |
Ah so that's where the tests for this stuff were haha |
|
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 |
Characters like "(" are also slipping from quotes. |
That's fixed in my build I have added the tests I meant to add. I think this is ready for review |
@fdncred ❤ |
|
Thanks! |
|
When it will be included in the next release? |
|
you can see it in the #general channel in discord, which says Dec 7. |
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_neededfunction. Ideally I would add tests thatrun an external command and verify that that works, like
^echo aa;bbb, but I couldn't find any test like thatthat 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.