我有python脚本,它有代码。在shell中使用shell = True的子进程的消毒输入
...
...
p = subprocess.Popen(cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
output, error = p.communicate()
...
...
当我运行bandit
它会给出错误。
>> Issue: [B602:subprocess_popen_with_shell_equals_true] subprocess call with shell=True identified, security issue.
Severity: High Confidence: High
Location: mypackage/myfile.py:123
123 stderr=subprocess.PIPE,
124 shell=True)
125 output, error = p.communicate()
然后我做了一些谷歌,并发现,我有我的消毒输入和与shlex.split
和shlex.quote
我可以清理它。
我将我的代码更改为。
...
...
p = subprocess.Popen(shlex.split(shlex.quote(cmd)),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
output, error = p.communicate()
...
...
但我仍然得到同样的错误,有没有办法消除这种误差运行bandit -r mypackage/myfile.py
'bandit'无法确定命令参数是否被充分消毒;它只能检测到你正在使用'Popen',这可能会成为一个安全问题。你最好离开'shell = False'并准备'cmd',这个方式可以直接被'exec'使用。 – chepner
分析器无法判断您运行的命令是否是您希望运行的命令。根据你正在做什么,沙盒化intepreter(例如[codejail](https://github.com/edx/codejail))可能是合适的或必要的。这涉及输入消毒和严格控制的操作系统权限的组合。 – gecko
@chepner,'exec'的意思是'subprocess.exec'? – Nilesh