zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation

Đang mở Phù hợp với người mới
#162,129 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@1cbyc đang làm issue này rồi.

Từ ngày 17/11/2026.

  • #162149 của @1cbyc — đang mở

Đá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
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend, databases

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ả

stdlib type-bug

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

Mở trong Codespaces

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.

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.

Issue khác của python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.