Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7399d0

Browse files
committed
#7950: add warning about security implications of shell=True to subprocess docs
Patch by Chris Rebert.
1 parent ea3e91e commit c7399d0

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Doc/library/subprocess.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,24 @@ This module defines one class called :class:`Popen`:
7676

7777
Popen(['/bin/sh', '-c', args[0], args[1], ...])
7878

79+
.. warning::
80+
81+
Executing shell commands that incorporate unsanitized input from an
82+
untrusted source makes a program vulnerable to `shell injection
83+
<http://en.wikipedia.org/wiki/Shell_injection#Shell_injection>`_,
84+
a serious security flaw which can result in arbitrary command execution.
85+
For this reason, the use of *shell=True* is **strongly discouraged** in cases
86+
where the command string is constructed from external input::
87+
88+
>>> from subprocess import call
89+
>>> filename = input("What file would you like to display?\n")
90+
What file would you like to display?
91+
non_existent; rm -rf / #
92+
>>> call("cat " + filename, shell=True) # Uh-oh. This will end badly...
93+
94+
*shell=False* does not suffer from this vulnerability; the above Note may be
95+
helpful in getting code using *shell=False* to work.
96+
7997
On Windows: the :class:`Popen` class uses CreateProcess() to execute the child
8098
program, which operates on strings. If *args* is a sequence, it will be
8199
converted to a string using the :meth:`list2cmdline` method. Please note that

0 commit comments

Comments
 (0)