gpt4 book ai didi

python - 如何修复 python 中的安全问题?

转载 作者:太空宇宙 更新时间:2023-11-04 02:42:57 25 4
gpt4 key购买 nike

我在运行脚本时使用了 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.

关于python - 如何修复 python 中的安全问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45912088/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com