Skip to main content
 首页 » 编程设计

python之如何修复 python 中的安全问题

2024年10月01日15over140

我在运行脚本时使用了 os.system('cls' if os.name == 'nt' else 'clear') 来清除输出但是在 codacy 上我遇到了一个安全问题

Starting a process with a shell, possible injection detected, security issue.

如何解决这个问题?

脚本链接:https://www.codacy.com/app/vaibhavsingh97/StalkPy/file/9458582870/issues/source?bid=5189215&fileBranchId=5189215#l43

请您参考如下方法:

当您使用从用户那里获取的参数运行该函数时,它会存在安全问题。例如:

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.