首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >强盗问题B108:hardcoded_tmp_directory和B102:exec_used

强盗问题B108:hardcoded_tmp_directory和B102:exec_used
EN

Stack Overflow用户
提问于 2020-01-04 17:15:35
回答 1查看 1.5K关注 0票数 1

我在我的项目上运行了土匪,得到了以下的安全问题,我不明白为什么这是一个问题,以及问题的解决方案是什么。

代码语言:javascript
复制
   --------------------------------------------------
>> 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问题的解决方案之后。我发现了,其中/tmptempfile.gettempdir()函数替换,但两者的值是相同的。tempfile.gettempdir()/tmp的解决方案吗?

EN

回答 1

Stack Overflow用户

发布于 2020-12-17 02:36:46

我也看到了这个强盗的问题。您共享的第一个链接现在链接到一个资源解释、问题和解决它的方法。

主要问题似乎是,创建可预测的临时文件使您面临“检查时间,使用时间攻击(TOCTOU)”。从资源中:

能够预测文件名并写入包含临时文件的目录的恶意用户可以在程序创建临时文件之前创建与临时文件名称的符号链接,从而有效地劫持临时文件。这允许恶意用户提供恶意数据或导致程序的操作影响攻击者选择的文件。

tempfile.gettempdir()和将umask设置为0077以确保只有创建者才能编辑和读取似乎是最好的解决方案。

从该资源中也可以:

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59593031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档