跳到内容

call-with-shell-equals-true (S604)

源自 flake8-bandit linter。

作用

检查在调用子进程时将 shell 参数设置为 true 或其他真值的函数调用。

为什么这不好?

在调用子进程时将 shell 参数设置为 true 或其他真值可能会引入安全漏洞,因为它允许将 shell 元字符和空格传递给子进程,可能导致 shell 注入攻击。

建议避免使用 shell=True,除非绝对必要。如果使用,请确保所有输入都经过适当的清理和引用,以防止此类漏洞。

已知问题

容易出现误报,因为它会在任何带有 shell=True 参数的函数调用上触发。

示例

import my_custom_subprocess

user_input = input("Enter a command: ")
my_custom_subprocess.run(user_input, shell=True)

参考