Skip to content

bpo-40932: Note security caveat of shlex.quote on Windows#21502

Merged
miss-islington merged 6 commits into
python:masterfrom
ammaraskar:shlex_windows_warning
Nov 11, 2020
Merged

bpo-40932: Note security caveat of shlex.quote on Windows#21502
miss-islington merged 6 commits into
python:masterfrom
ammaraskar:shlex_windows_warning

Conversation

@ammaraskar
Copy link
Copy Markdown
Member

@ammaraskar ammaraskar commented Jul 16, 2020

Added a note in the subprocess docs that recommend using shlex.quote without mentioning that this is only applicable to Unix.

Also added a warning straight into the shlex docs since it only says "for simple syntaxes resembling that of the Unix shell" and says using quote plugs the security hole without mentioning this important caveat.

https://bugs.python.org/issue40932

Automerge-Triggered-By: GH:zware

@zooba
Copy link
Copy Markdown
Member

zooba commented Jul 20, 2020

See my comment on the issue, but if we decide that dividing by OS is clear enough (even though there are non-conforming shells on both OSs) then this is fine.

@sovetov
Copy link
Copy Markdown

sovetov commented Jul 27, 2020

It's a worthy note indeed. It's not only security: it'll likely fail or work incorrectly with a single quote ' in the argument.

A similar function for Windows is subprocess.list2cmdline, which quotes according to C runtime rules. Although it's up to the a program to decide how to parse a command string. E.g. cmd /c obeys it's own quoting rules.

@ammaraskar
Copy link
Copy Markdown
Member Author

Adjusted wording for the shlex warning based on feedback on the bug.

Comment thread Doc/library/subprocess.rst Outdated
Comment thread Doc/library/shlex.rst Outdated
Copy link
Copy Markdown
Member

@zware zware left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than a spelling typo, I think this looks good now.

Comment thread Doc/library/shlex.rst Outdated
Co-authored-by: Zachary Ware <zachary.ware@gmail.com>
Comment thread Doc/library/shlex.rst Outdated

The ``shlex`` module is **only designed for Unix shells**.

The :func:`quote` function is not guaranteed to be safe on non-POSIX
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there such a thing as a POSIX-compliant shell? Also I think "guaranteed to be correct" is better than "safe", here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a POSIX-compliant shell

I think it's a pretty common term I've seen, loosely meaning shells that follow the posix specs here https://pubs.opengroup.org/onlinepubs/009695399/utilities/contents.html

Your wording definitely seems a bit better, changed.

@miss-islington miss-islington merged commit f9a8386 into python:master Nov 11, 2020
adorilson pushed a commit to adorilson/cpython that referenced this pull request Mar 13, 2021
…1502)

Added a note in the `subprocess` docs that recommend using `shlex.quote` without mentioning that this is only applicable to Unix. 

Also added a warning straight into the `shlex` docs since it only says "for simple syntaxes resembling that of the Unix shell" and says using `quote` plugs the security hole without mentioning this important caveat.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants