zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 75/100
Hướng nghiên cứu
Bắt đầu trong mô-đun zipfile tại ZipFile.writestr() và _write_end_record(), rồi so sánh cách xử lý comment theo từng mục với kiểm tra độ dài ZipFile.comment ở cấp lưu trữ. Thêm bước xác thực để ZipInfo.comment quá dài tạo ra ValueError mô tả như đã nêu; thêm hoặc chạy một kiểm thử hồi quy với comment dài hơn 65.535 byte. Hoàn tất khi thao tác ghi thất bại với lỗi đó trước khi close() có thể khiến tệp lưu trữ chưa được hoàn tất.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
Bug description:
When adding an entry to a ZipFile using a ZipInfo object whose comment exceeds 65,535 bytes (the maximum size representable by the 16-bit unsigned short H in the ZIP central directory header), writestr() accepts the entry without validation.
Later, when ZipFile.close() is called (or upon exiting a with ZipFile(...) block), _write_end_record() attempts to pack len(zinfo.comment) into structCentralDir using H, raising an unhandled struct.error:
struct.error: 'H' format requires 0 <= number <= 65535
Because this failure occurs during close(), the central directory and end-of-central-directory record are never written, leaving the ZIP archive unfinalized.
In contrast, archive-level comments (ZipFile.comment) are guarded against ZIP_MAX_COMMENT (65,535 bytes) with a warning and truncation.
Steps/Code to Reproduce:
import zipfile, io
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zinfo = zipfile.ZipInfo("test.txt")
zinfo.comment = b"x" * 70000
zf.writestr(zinfo, b"content")
Actual outcome:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "zipfile/__init__.py", line 1475, in __exit__
self.close()
File "zipfile/__init__.py", line 2060, in close
self._write_end_record()
File "zipfile/__init__.py", line 2107, in _write_end_record
centdir = struct.pack(structCentralDir,
stringCentralDir, create_version,
...
len(filename), len(extra_data),
len(zinfo.comment),
...)
struct.error: 'H' format requires 0 <= number <= 65535
Expected outcome:
ZipFile.writestr() (or ZipInfo) should validate len(zinfo.comment) <= 65535 and raise a descriptive ValueError(f"ZipInfo.comment exceeds maximum allowable length of {ZIP_MAX_COMMENT} bytes") (or truncate with a warning similar to archive comments) to prevent corrupting the close sequence.
CPython versions tested on:
Python 3.12, 3.13, main
Operating systems tested on:
Linux
Linked PRs
- gh-162149
- Ngôn ngữ chính
- Python
- Star
- 78.4k
- Fork
- 36.6k
- Merge trung bình
- 23 giờ 34 phút
- Pull request đã merge (30 ngày)
- 610
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc 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.
Issue khác của python/cpython
-
interpreter-core type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
stdlib topic-subprocess type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
corner case: base64.b32decode ignorechars=b'=' miscounts paddingCó thể đã có người làm @ElDShell đã nhận hôm nay. Đang mở3.15 easy type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
python/cpython#162353 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is foundCó thể đã có người làm @Sudhakar7J đã nhận 2 ngày trước. Đang mởstdlib type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
python/cpython#162296 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`Có thể đã có người làm @tamaki0575-cloud đã nhận 1 ngày trước. Đang mởbuild extension-modules topic-C-API type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
python/cpython#162219 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của python/cpython
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
vllm-project/vllm#69304 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
pydanty:is-working
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
pydantic/pydantic-ai#14187 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
test
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
prebid/salesagent#2676 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 62/100
sunososobro-hub/cloudball#339 ·
Maintainer thường phản hồi trong vòng 1 ngày