python / python/cpython

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

Đang mở
#154,490 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

stdlib type-bug
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu tại điểm vào ZipFile.mkdir() và kiểm tra cách mode được làm sạch thành external_attr, cùng với giá trị mặc định được ghi trong tài liệu. So sánh hành vi với stat.S_IMODE và os.mkdir(); được xem là hoàn tất khi các bit loại tệp bị loại trừ và giá trị mặc định được hiển thị là 0o777, đồng thời kiểm tra PR được liên kết gh-154509 để xem công việc nào đã được thực hiện.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
operating-systems
Loại issue
Lỗi
Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.