Detected subprocess `popen` call with shell equals `True`BAN-B602
Using shell=True
can expose you to security risks if someone crafts input to issue different commands than the ones you intended.
Python possesses many mechanisms to invoke an external executable. However, doing so may present a security issue if appropriate care is not taken to sanitize any user-provided or variable input. Subprocess invocation using a command shell is dangerous as it is vulnerable to various shell injection attacks. It is possible for an attacker to craft inputs to issue different commands than the ones you intended, for example: removing a file.
Great care should be taken to sanitize all input in order to mitigate this risk. Calls of this type are identified by a parameter of shell=True
being given.
It is recommended to use functions that don't spawn a shell. If you must use them, use shlex.quote
to sanitize the input.
Bad practice
import subprocess
subprocess.Popen(cmd, shell=True) # Sensitive, shell spawned
Recommended
import subprocess
subprocess.Popen(cmd)
References:
- subprocess.popen
- Subprocess security considerations
- shlex
- Command Injection
- OWASP Top 10 2021 Category A03 - Injection
- SANS Top 25
- CWE-78