`ZipFile.testzip()` Skips Earlier Duplicate Members
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 report
Summary
ZipFile.testzip() can incorrectly report that an archive is valid when an earlier member is corrupt and a later member has the same filename. The method iterates over all ZipInfo objects, but opens each one by filename instead of by its ZipInfo instance. Filename lookup selects the last duplicate, so earlier duplicates are checked against the wrong archive entry.
Minimal Reproducer
import io
import struct
import zipfile
buffer = io.BytesIO()
with zipfile.ZipFile(buffer, "w") as zf:
zf.writestr("duplicate.txt", b"corrupt me")
zf.writestr("duplicate.txt", b"valid data")
raw = bytearray(buffer.getvalue())
# Corrupt the payload of the first duplicate only.
with zipfile.ZipFile(io.BytesIO(raw)) as zf:
first = zf.infolist()[0]
name_length, extra_length = struct.unpack_from(
"<HH", raw, first.header_offset + 26
)
data_offset = (
first.header_offset + zipfile.sizeFileHeader +
name_length + extra_length
)
raw[data_offset] ^= 1
with zipfile.ZipFile(io.BytesIO(raw)) as zf:
first = zf.infolist()[0]
try:
zf.read(first)
except zipfile.BadZipFile:
print("The first duplicate is corrupt")
print(zf.testzip())
Observed output:
The first duplicate is corrupt
None
testzip() should return "duplicate.txt".
Expected Behavior
Every member in infolist() should be validated individually. For duplicate names, testzip() should preserve the identity of each ZipInfo object and open the entry with:
self.open(zinfo, "r")
Rather than:
self.open(zinfo.filename, "r")
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156540
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ừ ZipFile.testzip và so sánh hành vi mở entry hiện tại của nó với reproducer về member trùng lặp trong issue. Chạy reproducer và các bài kiểm tra zipfile liên quan; hoàn tất nghĩa là mọi ZipInfo đều được kiểm tra riêng lẻ và bản trùng lặp bị hỏng trước đó được báo cáo là duplicate.txt.
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
- 35/100