zipfile honors inconsistent compression method state when extracting

未關閉
#161,945 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
python
領域
security

研究方向

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.

由索引模型根據 Issue 內容生成。

描述

type-bug
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
主要語言
Python
星號
78.2k
分支
36.6k
平均合併
19 小時 57 分鐘
30 天內合併 PR
630

環境準備

在 Codespaces 中開啟

在瀏覽器裡用你自己的 GitHub 帳號啟動這個專案的開發容器。

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。