question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arguments are incorrectly double-quoted when nesting remote commands

See original GitHub issue

Consider the following example:

In [2]: from plumbum import SshMachine

In [3]: remote = SshMachine('localhost', ssh_opts=['-tt'])

In [4]: ls = remote['ls']

In [5]: sudo = remote['sudo']

In [6]: print sudo[ls['directory_without_spaces_in_name']]
/usr/bin/sudo /bin/ls directory_without_spaces_in_name

In [7]: print sudo[ls['directory with spaces in name']]
/usr/bin/sudo /bin/ls "'directory with spaces in name'"

In [8]: print ls['directory with spaces in name']
/bin/ls 'directory with spaces in name'

What’s happening here is that the single argument to ls, which contains spaces, gets quoted twice when nested, causing the final argument received by ls to contain an extra set of single quotes (causing the command to return incorrect results).

Note that this type of nesting works as expected with local commands:

In [9]: from plumbum import local

In [10]: ls = local['ls']

In [11]: sudo = local['sudo']

In [12]: print sudo[ls['directory_without_spaces_in_name']]
/usr/bin/sudo /bin/ls directory_without_spaces_in_name

In [13]: print sudo[ls['directory with spaces in name']]
/usr/bin/sudo /bin/ls 'directory with spaces in name'

However, it doesn’t work locally without nesting:

In [14]: print ls['directory with spaces in name']
/bin/ls directory with spaces in name

Am I doing anything wrong? Is there any way to control this behavior?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
thedatakingcommented, Jun 24, 2017

Looking at issue #124 and poking at the code, this is my understanding of the problem:

  1. Some arguments need escaping no matter their nesting level, e.g., directory names with spaces in them as @JoshRosen points out.
  2. For some arguments, we don’t want escaping since we want the shell to expand them. E.g. ‘*.py’.
  3. We don’t want to shell-escape the same argument multiple times. Nesting three or more local commands is one way to trigger this.

We need explicit control over argument quoting. One way would be to pass some arguments as a new type RawString or whatever rather than a native python string to tell plumbum we don’t want any automatic escaping for that arg. That would remove a lot of the elegance of plumbum so that is probably not the way to go. Another option, which I’d personally vote for is to make the programmer responsible for automatically shell escaping strings. Existing packages such as pipes and shlex (py3 only) provide a quote function fit for this purpose (at least for non-Windows platforms).

Note, one can disable automatic shell-escaping for local commands indirectly thru this ugly hack:

import sys
from plumbum.machines import LocalCommand
LocalCommand.QUOTE_LEVEL = sys.maxint #any number >> 2 will do

make = local.get("make")
nice = local.get("nice")
print nice["-n", "19", make["VAR='A B C'", "test"]] 
# output: /usr/bin/nice -n 19 /usr/bin/make VAR='A B C' test
1reaction
bendemottcommented, Dec 5, 2019

I am running into this issue as well with REMOTE commands. I want to send a pattern argument into unzip and avoid escaping it.

Would it be possible to have a Raw() class that you can enclose arguments in, so escaping is ignored for these arguments. Is this an acceptable solution? @henryiii - I’m happy to submit a patch if no one else has time to get to it - just don’t want to do wasted work as I’m not a regular contributor to this project.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parameters with double quotes are not properly passed to ...
I'm writing generic powershell script to perform deployments on remote machines. I have hit one problem I can not ...
Read more >
Nested quotes hell - TechNet - Microsoft
Hi,. I have a powershell script that needs parameters passed to it. We are trying to run through powercenter.
Read more >
Nested quotes in subshells - bash - Unix StackExchange
My man bash says "If the substitution appears within double quotes, word splitting and pathname expansion are not performed on the results." (in ......
Read more >
5.1. Quoting Variables
[2] An argument enclosed in double quotes presents itself as a single word, ... five words" COMMAND This is $variable1 # Executes COMMAND...
Read more >
How to have simple and double quotes in a scripted SSH ...
Using quotes in command arguments is always tricky, as the shell will strip off ... Why has the SSH remote command parsing gone...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found