跳到内容

硬编码临时文件 (S108)

源自 flake8-bandit linter。

作用

检查硬编码临时文件或目录路径的使用。

为什么这不好?

为临时文件使用硬编码路径可能不安全。如果攻击者发现硬编码路径的位置,他们可以用恶意载荷替换文件或目录的内容。

其他程序也可能读取或写入这些硬编码路径的内容,从而导致意外行为。

示例

with open("/tmp/foo.txt", "w") as file:
    ...

建议改为

import tempfile

with tempfile.NamedTemporaryFile() as file:
    ...

Options (选项)

参考