`ZipFile.mkdir()` corrupts archives during an active write
Chưa có ai nhận issue này.
- 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 description:
Summary
ZipFile.mkdir() does not reject calls made while another writable member handle returned by ZipFile.open(..., mode="w") is active. On seekable output,mkdir() seeks back to the current central-directory start and writes a new local header there. This overwrites or aliases the active member's local header and can produce a corrupt ZIP archive without raising an exception.
Minimal Reproducer
import io
import zipfile
buffer = io.BytesIO()
zf = zipfile.ZipFile(buffer, "w")
member = zf.open("file.txt", mode="w")
zf.mkdir("directory") # Should reject this operation
member.write(b"payload")
member.close()
zf.close()
with zipfile.ZipFile(io.BytesIO(buffer.getvalue())) as broken:
print(broken.namelist())
print(broken.read("directory/"))
Observed behavior:
['directory/', 'file.txt']
Traceback (most recent call last):
File "/home/ubuntu/cpython-main/test.py", line 15, in <module>
print(broken.read("directory/"))
~~~~~~~~~~~^^^^^^^^^^^^^^
File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2165, in read
with self.open(name, "r", pwd) as fp:
~~~~~~~~~^^^^^^^^^^^^^^^^
File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2239, in open
raise BadZipFile(
'File name in directory %r and header %r differ.'
% (zinfo.orig_filename, fname))
zipfile.BadZipFile: File name in directory 'directory/' and header b'file.txt' differ.
The exact error may vary with the archive contents, but the resulting archive is structurally inconsistent.
Expected Behavior
mkdir() should reject the operation before changing the archive, consistent with the existing behavior of open(..., mode="w"), write(), writestr(), and close() when a writable member handle is active. A ValueError with a message explaining that another write handle is open would be appropriate.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156079
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu tại Lib/zipfile/init.py, ở ZipFile.mkdir, và so sánh cách nó xử lý các handle của member có thể ghi đang hoạt động với open(..., mode="w"), write(), writestr() và close(). Chạy reproducer tối thiểu; hoàn tất nghĩa là mkdir từ chối thao tác bằng một ValueError trước khi thay đổi archive.
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
- backend
- 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
- 30/100