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

Aperta
#294 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
cpp
Ambito
backend

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
C++
Stelle
4.6k
Fork
519
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di ip7z/7zip

Tutte le issue di ip7z/7zip

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.