我在我的项目上运行了土匪,得到了以下的安全问题,我不明白为什么这是一个问题,以及问题的解决方案是什么。
--------------------------------------------------
>> Issue: [B108:hardcoded_tmp_directory] Probable insecure usage of temp file/directory.
Severity: Medium Confidence: Medium
Location: abc/xyz/xxx.py:176
More Info: https://bandit.readthedocs.io/en/latest/plugins/b108_hardcoded_tmp_directory.html
175 def get_pickle_file_path(self):
176 return os.path.join("/tmp/aaa", "folder_" + self.name)
177
--------------------------------------------------
>> Issue: [B102:exec_used] Use of exec detected.
Severity: Medium Confidence: High
Location: abc/models.py:1405
More Info: https://bandit.readthedocs.io/en/latest/plugins/b102_exec_used.html
1404 loc = {'result': []}
1405 exec(self.code, globals(), loc)
1406 return loc['result']在寻找B108问题的解决方案之后。我发现了这,其中/tmp被tempfile.gettempdir()函数替换,但两者的值是相同的。tempfile.gettempdir()是/tmp的解决方案吗?
发布于 2020-12-17 02:36:46
我也看到了这个强盗的问题。您共享的第一个链接现在链接到一个资源解释、问题和解决它的方法。
主要问题似乎是,创建可预测的临时文件使您面临“检查时间,使用时间攻击(TOCTOU)”。从资源中:
能够预测文件名并写入包含临时文件的目录的恶意用户可以在程序创建临时文件之前创建与临时文件名称的符号链接,从而有效地劫持临时文件。这允许恶意用户提供恶意数据或导致程序的操作影响攻击者选择的文件。
tempfile.gettempdir()和将umask设置为0077以确保只有创建者才能编辑和读取似乎是最好的解决方案。
从该资源中也可以:
import os
import tempfile
tmpdir = tempfile.mkdtemp()
predictable_filename = 'myfile'
# Ensure the file is read/write by the creator only
saved_umask = os.umask(0077)
path = os.path.join(tmpdir, predictable_filename)
print path
try:
with open(path, "w") as tmp:
tmp.write("secrets!")
except IOError as e:
print 'IOError'
else:
os.remove(path)
finally:
os.umask(saved_umask)
os.rmdir(tmpdir)https://stackoverflow.com/questions/59593031
复制相似问题