zipfile honors inconsistent compression method state when extracting
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Start from the reproduction script in the issue, which rewrites only the local header compression method and then reads the member through zipfile. The fix belongs in the zipfile module's code that reads local file headers and checks them against central directory entries, where the existing file-name mismatch check is the pattern to follow. Done means zipfile rejects an entry whose local and central compression methods differ, and a regression test covers it. The body links PR gh-161948, so check that work before starting.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug report
Moving from GHSA to public after discussion consensus.
Summary
An user can contrive a ZIP input such that Python's zipfile attempts to decompress a member of the file with a different decompression method than other parsers will select. This can result in an extraction differential.
Details
ZIP entries record their compression method in two places: the local header and the central directory.
Python honors the central directory entry's compression method, even if it differs from the local file entry's stated method. Consequently, Python will attempt to decompress an entry with an inconsistent method.
The correct behavior is to reject the discrepancy, similarly to how Python already rejects file name discrepancies between the local header and central directory.
PoC
import binascii
import io
import struct
import zipfile
original = b"central-directory interpretation\n" * 4
# Produce a valid DEFLATED archive and close it so the central directory exists.
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", compression=zipfile.ZIP_DEFLATED) as z:
z.writestr("test.txt", original)
raw = bytearray(buf.getvalue())
with zipfile.ZipFile(io.BytesIO(raw), "r") as z:
info = z.getinfo("test.txt")
local = info.header_offset
assert raw[local : local + 4] == b"PK\x03\x04"
name_len, extra_len = struct.unpack_from("<HH", raw, local + 26)
data_start = local + 30 + name_len + extra_len
compressed = bytes(raw[data_start : data_start + info.compress_size])
# Rewrite ONLY the local header to describe a valid STORED member.
#
# Local-header offsets:
# +8: compression method (uint16)
# +14: CRC-32 (uint32)
# +18: compressed size (uint32)
# +22: uncompressed size (uint32)
struct.pack_into("<H", raw, local + 8, zipfile.ZIP_STORED)
struct.pack_into(
"<III",
raw,
local + 14,
binascii.crc32(compressed) & 0xFFFFFFFF,
len(compressed),
len(compressed),
)
central = raw.index(b"PK\x01\x02")
local_method = struct.unpack_from("<H", raw, local + 8)[0]
central_method = struct.unpack_from("<H", raw, central + 10)[0]
assert local_method == zipfile.ZIP_STORED
assert central_method == zipfile.ZIP_DEFLATED
print(f"local={local_method}, central={central_method}")
# Python trusts the central directory and successfully decompresses.
with zipfile.ZipFile(io.BytesIO(raw), "r") as z:
python_contents = z.read("test.txt")
assert python_contents == original
print("zipfile:", repr(python_contents))
# Simulate a parser that trusts the local header.
local_crc, local_csize, local_usize = struct.unpack_from("<III", raw, local + 14)
local_contents = bytes(raw[data_start : data_start + local_csize])
assert len(local_contents) == local_usize
assert binascii.crc32(local_contents) & 0xFFFFFFFF == local_crc
assert local_contents != python_contents
print("local-header parser:", repr(local_contents))
print("different outputs:", local_contents != python_contents)
Impact
The impact is a fail-open differential: zipfile extracts the malformed ZIP as though it's valid. Other parsers may pick the other incorrect fail-open half of the differential, which is to extract using the local file entry's compression method.
A strict parser should reject a ZIP file if it contains a local file entry/central directory entry pair that has an inconsistent compression method.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux, Windows, macOS
Linked PRs
- gh-161948
- Ngôn ngữ chính
- Python
- Star
- 78.2k
- Fork
- 36.6k
- Merge trung bình
- 19 giờ 57 phút
- Pull request đã merge (30 ngày)
- 630
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
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 1 ngày
-
stdlib type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
extension-modules stdlib topic-XML type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
python/cpython#161807 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
stdlib type-feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
python/cpython#161788 · 7 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
extension-modules topic-IO type-bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
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 62/100
cpp-best-practices/project_validator#267 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] Sending a Telegram message chain twice duplicates mentionsCó thể đã có người làm @123XieC đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
AstrBotDevs/AstrBot#11125 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
backend bug priority: high
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
nooriegarg/ContribIQ#73 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
bug p3 security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày