python / python/cpython

Improper sanitization and documentation for `ZipFile.mkdir(mode)`

未关闭
#154,490 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stdlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

Bug report

Bug description:

When calling ZipFile.mkdir(), the user provided mode does not have the file format bits cleared, which may cause the created entry be recognized as an incorrect or unknown file format.

import io
import stat
import zipfile

with zipfile.ZipFile(io.BytesIO(), 'w') as zh:
    zh.mkdir('foo/', 0o107777)  # regular file
    zinfo = zh.getinfo('foo/')
    print(oct(stat.S_IFMT(zinfo.external_attr >> 16)))  # 0o140000 (socket file)

with zipfile.ZipFile(io.BytesIO(), 'w') as zh:
    zh.mkdir('foo/', 0o127777)  # symbolic link
    zinfo = zh.getinfo('foo/')
    print(oct(stat.S_IFMT(zinfo.external_attr >> 16)))  # 0o160000 (unknown)

Additionally, the current source code and doc for ZipFile.mkdir()'s default mode value are 511, which is identical to 0o777 but less intuitive.

Suggestion

Instead of the current sanitization:

zinfo.external_attr = ((0o40000 | mode) & 0xFFFF) << 16

we should probably sanitize the provided mode as what stat.S_IMODE does, i.e.:

zinfo.external_attr = (0o40000 | (mode & 0o7777)) << 16

We should probably also revise the source code and doc for ZipFile.mkdir()'s default mode value to 0o777, to be consistent with os.mkdir() and more intuitive.

CPython versions tested on:

3.14

Operating systems tested on:

No response

Linked PRs
  • gh-154509

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 ZipFile.mkdir() 入口点开始,检查 mode 如何被清理为 external_attr,以及文档中记录的默认值。将其行为与 stat.S_IMODE 和 os.mkdir() 进行比较;完成标准是排除文件类型位,并将默认值显示为 0o777,同时检查已关联的 PR gh-154509,了解已经在进行的工作。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
2/5
预计耗时
1-3 小时
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。