跳到内容

subprocess-popen-with-shell-equals-true (S602)

源自 flake8-bandit linter。

作用

检查使用 shell 启动子进程的方法调用。

为什么这不好?

使用 shell 启动子进程可能允许攻击者执行任意 shell 命令。考虑在不调用 shell 的情况下启动进程,并对输入进行净化以减轻 shell 注入的风险。

示例

import subprocess

subprocess.run("ls -l", shell=True)

建议改为

import subprocess

subprocess.run(["ls", "-l"])

参考