zip: extracting with a wrong or missing password truncates an existing target file to 0 bytes (`-y` / `-aoa`)

オープン
#294 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
58/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
cpp
領域
backend

調査の方向性

Read CPP/7zip/Archive/Zip/ZipHandler.cpp around Extract() and the wrong-password branches, then compare how CPP/7zip/Archive/7z/7zExtract.cpp opens output files lazily. Reproduce the ZIP and 7z cases from the issue, including the overwrite modes. Done when a wrong password leaves existing files unchanged and creates no empty output file, with extraction behavior still covered for the listed modes.

索引モデルが issue の本文から書いたものです。

説明

Environment
  • 7-Zip (z) 26.06 (arm64), ip7z/7zip @ d3296e3
  • macOS 26.4.1 arm64, Apple clang 21.0.0
Description

For an encrypted zip item, the extraction callback opens (creates or truncates) the output file before the password is checked. When the password is wrong, nothing is written, but the file already exists with length 0. With -y / -aoa an existing file with the same name loses its contents. A 7z archive with the same wrong password does not touch the existing file.

Steps to reproduce
Z=7zz
d=$(mktemp -d) && cd "$d" && printf 'hello hello' > a.txt
$Z a -bd -tzip -psecret z.zip a.txt >/dev/null
$Z a -bd -t7z  -psecret s.7z a.txt >/dev/null
for f in z.zip s.7z; do
  rm -rf xo; mkdir xo; printf 'ORIGINAL-CONTENT' > xo/a.txt
  $Z x -bd -y -pwrong -oxo $f >/dev/null 2>&1 </dev/null
  echo "$f rc=$? existing a.txt is now $(wc -c < xo/a.txt | tr -d ' ') bytes (was 16)"
done
# z.zip rc=2 existing a.txt is now 0 bytes (was 16)
# s.7z  rc=2 existing a.txt is now 16 bytes (was 16)

rm -rf xo; mkdir xo; $Z x -bd -y -pwrong -oxo z.zip >/dev/null 2>&1
ls -l xo     # a.txt, 0 bytes, although nothing could be extracted
for sw in -aoa -aot -aos -aou; do
  rm -rf xo; mkdir xo; printf ORIGINAL-CONTENT > xo/a.txt
  $Z x -bd $sw -pwrong -oxo z.zip >/dev/null 2>&1
  echo "$sw rc=$? $(ls -l xo | tail -n +2 | awk '{print $9":"$5}' | tr '\n' ' ')"
done
# -aoa rc=2 a.txt:0
# -aot rc=2 a.txt:0 a_1.txt:16
# -aos rc=0 a.txt:16
# -aou rc=2 a.txt:16 a_1.txt:0
Expected

x is documented as "Extracts files from an archive to the current directory or to the output directory. ... with full paths", and -ao{a|s|t|u} as the overwrite mode ("-aoa Overwrite All existing files without prompt") for files that are extracted. An item that cannot be extracted because of a wrong password should not replace or empty an existing file, and should not leave an empty file that looks like a successful extraction. 7z archives already behave this way.

Actual

ERROR: Wrong password : a.txt, rc=2, but xo/a.txt is now 0 bytes (was 16). In a fresh directory a 0-byte a.txt is created.

Root cause (analysis on d3296e3)
  1. CPP/7zip/Archive/Zip/ZipHandler.cpp:1608: Extract() calls extractCallback->GetStream(index, &realOutStream, askMode) first.
  2. CPP/7zip/UI/Common/ArchiveExtractCallback.cpp:1577: GetStream ends in _outFileStreamSpec->Create_ALWAYS_or_Open_ALWAYS(...), which truncates or creates the file immediately.
  3. Only afterwards CZipDecoder::Decode() checks the password and returns kWrongPassword (ZipHandler.cpp:1133, 1139, 1270, 1285, 1311). No data was written, and the zero-length file stays.
  4. The 7z handler opens the output file lazily: CPP/7zip/Archive/7z/7zExtract.cpp:75-106 (CFolderOutStream::OpenFile) is called when the first data arrives, so a failed decode never reaches it.
Suggested fix

In ZipHandler.cpp Extract(), run the password/header check (the kWrongPassword branches) before requesting the output stream, or call GetStream lazily on the first write as the 7z handler does. Alternatively remove the zero-length file when the item result is kWrongPassword.


Found with a differential test setup (a model of the documented behavior, and for some reports an independent tool such as libarchive/bsdtar, compared against the real binary) and then checked by hand: every command above was re-run verbatim on the tested build, and the root cause was read in the source. Tested commit: d3296e3 (26.06).

主要言語
C++
スター
4.6k
フォーク
519
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

ip7z/7zip のほかの issue

ip7z/7zip の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。