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

subprocess.list2cmdline doesn't quote the & character #53218

Closed
shypike mannequin opened this issue Jun 11, 2010 · 16 comments
Closed

subprocess.list2cmdline doesn't quote the & character #53218

shypike mannequin opened this issue Jun 11, 2010 · 16 comments
Labels
OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@shypike
Copy link
Mannequin

shypike mannequin commented Jun 11, 2010

BPO 8972
Nosy @gpshead, @vstinner, @ericvsmith, @ezio-melotti, @bitdancer
Superseder
  • bpo-7839: Popen should raise ValueError if pass a string when shell=False or a list when shell=True
  • Files
  • list2cmdline_ampersand_fix.patch: patch file for subprocess.py and test_subprocess.py
  • list2cmdline_proper_fix.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2014-07-15.17:33:05.220>
    created_at = <Date 2010-06-11.13:11:04.774>
    labels = ['invalid', 'type-bug', 'library', 'OS-windows']
    title = "subprocess.list2cmdline doesn't quote the & character"
    updated_at = <Date 2014-07-15.17:33:05.219>
    user = 'https://bugs.python.org/shypike'

    bugs.python.org fields:

    activity = <Date 2014-07-15.17:33:05.219>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-07-15.17:33:05.220>
    closer = 'r.david.murray'
    components = ['Library (Lib)', 'Windows']
    creation = <Date 2010-06-11.13:11:04.774>
    creator = 'shypike'
    dependencies = []
    files = ['17645', '17650']
    hgrepos = []
    issue_num = 8972
    keywords = ['patch']
    message_count = 16.0
    messages = ['107544', '107646', '107672', '107679', '107680', '107713', '107721', '107752', '107768', '107769', '108131', '108135', '108162', '128127', '222887', '223128']
    nosy_count = 9.0
    nosy_names = ['gregory.p.smith', 'exarkun', 'vstinner', 'eric.smith', 'ezio.melotti', 'r.david.murray', 'SilentGhost', 'shypike', 'BreamoreBoy']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = '7839'
    type = 'behavior'
    url = 'https://bugs.python.org/issue8972'
    versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

    @shypike
    Copy link
    Mannequin Author

    shypike mannequin commented Jun 11, 2010

    subprocess.py/list2cmdline should also put double quotes around strings that contain ampersands (&), but no spaces.
    If not, the Windows command processor will split the command into two separate parts. In short, '&' needs the same treatment as '|'.

    @shypike shypike mannequin added OS-windows type-bug An unexpected behavior, bug, or error labels Jun 11, 2010
    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 12, 2010

    Thanks for reporting this issue. Can you attach a patch which adds a unit test covering this behavior and fixing the quoting rules? It would be very helpful in resolving this ticket. If implementing the whole thing is too much work, if you could just provide a failing unit test that would still be greatly helpful.

    Thanks again.

    @shypike
    Copy link
    Mannequin Author

    shypike mannequin commented Jun 12, 2010

    Added patch file for subprocess.py and test_subprocess.py.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 12, 2010

    Thanks.

    I'm not sure this is a correct change. And in fact, I would say that the current quoting of | is also incorrect.

    & and | (and ^ and perhaps several others) have special meaning to cmd.exe. list2cmdline is documented as applying the quoting rules which the "MS C runtime" uses: cmd.exe and the C runtime are different and have different rules.

    It seems to me that whoever added the | handling to list2cmdline was confused about the purpose of this function, or failed to properly document the function.

    It would make more sense to document list2cmdline as applying cmd.exe-style quoting rules, if those are the rules it is actually going to implement.

    A better option, though, would probably be to implement the cmd.exe quoting rules in a different function from the MS C runtime rules.

    This all might benefit from a sanity check from someone who's actually worked with the subprocess module before, though (ie, not me).

    @exarkun exarkun mannequin added the stdlib Python modules in the Lib dir label Jun 12, 2010
    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 12, 2010

    http://www.autohotkey.net/~deleyd/parameters/parameters.htm#WINCRULES is a helpful reference, by the way.

    @shypike
    Copy link
    Mannequin Author

    shypike mannequin commented Jun 12, 2010

    A work-around could be that the caller puts double quotes around the individual elements of the sequence that need it.
    However, this won't work because list2cmdline doesn't handle backslash quoting properly. An element like r'"foo"' is translated to r'\"foo\"'. This is incorrect because cmd.exe cannot handle this. The backslash may be appropriate for embedded quotes (like in r'foo"bar'), but not for outer quotes (like in r'"foobar"').

    The user shouldn't have to worry with adding quotes anyway, so it would be better to demand that '|' and '&' are passed as separate elements in the sequence. Example ['echo', 'foo', '&', 'bar'].
    When someone passes ['echo', 'foo&bar'], it is very obvious that r'echo "foo&bar"' is expected and not r'echo foo & bar'.

    I have added a patch for this way of working of list2cmdline.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 13, 2010

    I'm not sure my last message was clear.

    A work-around could be that ...

    What problem is being worked around?

    @shypike
    Copy link
    Mannequin Author

    shypike mannequin commented Jun 13, 2010

    The discussion is going the wrong way. Let me state what is the actual problem.
    When the Popen() function is called with "shell=True", you cannot pass command line elements in which the characters '&' and '|' are embedded (example r'Q&A'). list2cmdline should embed such elements in double quotes '"' in the same way as when a space is detected (so r'"Q&A"').
    When '&' and '|' require their special meaning, they should be passed as separate elements (r'&') and in that case list2cmdline should not double-quote them.

    The whole C++ discussion was only invoked because I said a potential work-around doesn't work because the way list2cmdline is designed. That remark is not relevant, because the actual problem is different.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 14, 2010

    That remark is not relevant, because the actual problem is different.

    Maybe you can expand the test case to demonstrate the actual problem? The tests in the latest patch are for list2cmdline directly. But you can't observe the problem a bug in list2cmdline causes until you actually try to launch a child process. So perhaps the test should do that?

    @bitdancer
    Copy link
    Member

    The actual bug here is that list2cmdline is called when shell=True and a list is passed. This should not be done. Instead, Popen should raise an TypeError (see bpo-7839).

    When calling Popen with shell=True, you should be passing in a string that is already correctly quoted. This is how it works in Unix, and is how it should work in Windows as well.

    So I agree with exarkun's comments in msg107679, and I think Gregory's fix in r60115 for bpo-1300 was incorrect and that in fact the OP was correct that he did not understand what was happening (note that he did not report an actual bug, he just reported that the code looked wrong to him).

    @shypike
    Copy link
    Mannequin Author

    shypike mannequin commented Jun 18, 2010

    I see your point about passing a command line as a single string instead of a list. Too bad that Popen doesn't just do the obvious thing (assemble the list into a string in a shell/cmd.exe compatible way).

    bpo-1300 should indeed be reversed.

    Raising ValueError will result in breaking some existing programs.

    @exarkun
    Copy link
    Mannequin

    exarkun mannequin commented Jun 18, 2010

    I've reverted the bpo-1300 revision from 2.6, 2.7, 3.1, and 3.2. I hope
    7839 is resolved soon.

    @bitdancer
    Copy link
    Member

    The reason that Popen doesn't do "the obvious thing" with a list and shell=True is that it is far from obvious what the correct thing is to do, especially for windows.

    Thanks for the revert, exarkun.

    @SilentGhost
    Copy link
    Mannequin

    SilentGhost mannequin commented Feb 7, 2011

    bpo-11139 was closed as a duplicate of this issue.

    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 12, 2014

    I believe this is still valid in which case could we have the stage and resolution fields set appropriately please. You might also like to take a look at bpo-13238 which is referred to by bpo-7839.

    @bitdancer
    Copy link
    Member

    The problem pointed to by this report was resolved by the revert. The issue of what to do about a list passed with shell=True is addressed (with no consensus) by bpo-7839. A proposal to add a windows equivalent of shlex.quote has been floated, but no takers have come forward to implement one, as far as I know.

    There's nothing left to do here in this issue, as far as I can see.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    OS-windows stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant