Skip to content

Use subprocess securely to avoid shell injectionΒ #24646

@wangzelin007

Description

@wangzelin007

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.

Consequences

  • If you use shell=True, your code is extremely likely to be vulnerable

  • Even if your code is not vulnerable, the next person who maintains can easily introduce a vulnerability.

  • Shell injections are arbitrary code execution - a competent attacker will use these to compromise the rest of your system.

How to Avoid

  • Stop using the following commands that may cause shell injection according to Bandit rule B605
    • os.system
    • os.popen
    • os.popen2
    • os.popen3
    • os.popen4
    • popen2.popen2
    • popen2.popen3
    • popen2.popen4
    • popen2.Popen3
    • popen2.Popen4
    • commands.getoutput
    • commands.getstatusoutput
  • Use subprocess securely
def ping(myserver):
    args = ['ping', '-c', '1', myserver]
    return subprocess.check_output(args, shell=False)

Metadata

Metadata

Assignees

Labels

Azure CLI TeamThe command of the issue is owned by Azure CLI teamOKR CandidateThis label is used to track how many GitHub issues we have resolved for OKR purpose.common issuefeature-request

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions