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

Abierto
#294 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
58/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
cpp
Área
backend

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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

Lenguaje dominante
C++
Estrellas
4.6k
Forks
519
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de ip7z/7zip

Todos los issues de ip7z/7zip

Issues similares

Más issues de C++

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.