我在运行脚本时使用了 os.system('cls' if os.name == 'nt' else 'clear') 来清除输出但是在 codacy 上我遇到了一个安全问题
Starting a process with a shell, possible injection detected, security issue.
如何解决这个问题?
请您参考如下方法:
当您使用从用户那里获取的参数运行该函数时,它会存在安全问题。例如:
import os
def do_clear(command): # Notice command is sent as argument from outside world and hence this makes it vulnerable
os.system(command)
如果该方法被调用,例如
do_clear('rm -f */*')
那么有可能删除当前目录下的所有文件。但如果直接使用'clear'命令,则不用担心安全问题,因为所有情况下只运行'clear'。所以下面的函数是足够安全的。
def do_clear(): # Notice command is not sent as argument from outside world
os.system('cls' if os.name == 'nt' else 'clear') # This is not risky as os.system takes clear/cls command always.
