Skip to content

Commit 20cea90

Browse files
committed
bpo-40932: Note security caveat of shlex.quote on Windows
1 parent 936a660 commit 20cea90

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

Doc/library/shlex.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,18 @@ The :mod:`shlex` module defines the following functions:
6161
string that can safely be used as one token in a shell command line, for
6262
cases where you cannot use a list.
6363

64+
.. warning::
65+
66+
The ``shlex`` module is **only designed for Unix systems**.
67+
68+
The :func:`quote` function is not garaunteed to be safe on other operating
69+
systems such as Windows. Executing commands quoted by this module on
70+
other operating systems can open the possibility of a command injection
71+
vulnerability.
72+
73+
Consider using functions that pass command arguments with lists such as
74+
:func:`subprocess.run` with ``shell=False``.
75+
6476
This idiom would be unsafe:
6577

6678
>>> filename = 'somefile; rm -rf ~'

Doc/library/subprocess.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,12 @@ quoted appropriately to avoid
693693
`shell injection <https://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_
694694
vulnerabilities.
695695

696-
When using ``shell=True``, the :func:`shlex.quote` function can be
697-
used to properly escape whitespace and shell metacharacters in strings
698-
that are going to be used to construct shell commands.
696+
When using ``shell=True`` on **Unix** systems, the :func:`shlex.quote`
697+
function can be used to properly escape whitespace and shell metacharacters in
698+
strings that are going to be used to construct shell commands. Note that
699+
the :mod:`shlex` module is *only meant for Unix systems* and using it on other
700+
operating systems such as Windows can create a command injection vulnerability
701+
when coupled with ``shell=True``.
699702

700703

701704
Popen Objects

0 commit comments

Comments
 (0)