Skip to main content
 首页 » 编程设计

python之tmpnam 的不安全性/替代品有多不安全

2025年05月04日13lexus

我考虑过使用tmpnam 来设置QPrinter 的输出文件名。但是 Python 文档建议不要使用它。

os.tmpnam()

Return a unique path name that is reasonable for creating a temporary file. ... Applications are responsible for properly creating and managing files created using paths returned by tmpnam(); no automatic cleanup is provided.

Warning

Use of tmpnam() is vulnerable to symlink attacks; consider using tmpfile() (section File Object Creation) instead.

Windows:微软的 tmpnam() 始终执行 在根目录中创建一个名称 当前驱动器的,那是 通常是温度不佳的位置 文件(根据权限,您可以 甚至无法使用打开文件 这个名字)。

  • 如果我的应用程序不需要任何特殊权限,这真的不安全吗?
  • 考虑到我只能将路径设置为 QPrinter 的输出文件名,有哪些安全替代方案?

请您参考如下方法: