OS commands are security-sensitive. For example, their use has led in the past to the following vulnerabilities:
Applications that execute operating system commands or execute commands that interact with the underlying system should neutralize any externally-provided input used to construct those commands. Failure to do so could allow an attacker to execute unexpected or dangerous commands, potentially leading to loss of confidentiality, integrity or availability.
This rule flags code that specifies the name of the command to run. The goal is to guide security code reviews.
(*) You are at risk if you answered yes to any of those questions.
Restrict the control given to the user over the executed command:
Restrict which users can have access to the command:
Reduce the damage the command can do:
Python 3
import subprocess import os params = ["ls", "-l"] subprocess.run(params) # Questionable subprocess.Popen(params) # Questionable # Older API subprocess.call(params) # Questionable subprocess.check_call(params) # Questionable subprocess.check_output(params) # Questionable cmd = "ls -l" os.system(cmd) # Questionable mode = os.P_WAIT file = "ls" path = "/bin/ls" env = os.environ os.spawnl(mode, path, *params) # Questionable os.spawnle(mode, path, *params, env) # Questionable os.spawnlp(mode, file, *params) # Questionable os.spawnlpe(mode, file, *params, env) # Questionable os.spawnv(mode, path, params) # Questionable os.spawnve(mode, path, params, env) # Questionable os.spawnvp(mode, file, params) # Questionable os.spawnvpe(mode, file, params, env) # Questionable mode = 'r' (child_stdout) = os.popen(cmd, mode, 1) # Questionable # print(child_stdout.read()) (_, output) = subprocess.getstatusoutput(cmd) # Questionable out = subprocess.getoutput(cmd) # Questionable os.startfile(path) # Questionable os.execl(path, *params) # Questionable os.execle(path, *params, env) # Questionable os.execlp(file, *params) # Questionable os.execlpe(file, *params, env) # Questionable os.execv(path, params) # Questionable os.execve(path, params, env) # Questionable os.execvp(file, params) # Questionable os.execvpe(file, params, env) # Questionable
Python 2
import os import popen2 cmd = "ls -l" mode = "r" (_, child_stdout) = os.popen2(cmd, mode) # Questionable (_, child_stdout, _) = os.popen3(cmd, mode) # Questionable (_, child_stdout) = os.popen4(cmd, mode) # Questionable (child_stdout, _) = popen2.popen2(cmd) # Questionable (child_stdout, _, _) = popen2.popen3(cmd) # Questionable (child_stdout, _) = popen2.popen4(cmd) # Questionable