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

Open
#294 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
cpp
Domain
backend

Research direction

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.

Written by the indexing model from the issue text.

Description

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).

Dominant language
C++
Stars
4.6k
Forks
519
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ip7z/7zip

All issues in ip7z/7zip

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.