`ZipFile.mkdir()` corrupts archives during an active write
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug description:
Summary
ZipFile.mkdir() does not reject calls made while another writable member handle returned by ZipFile.open(..., mode="w") is active. On seekable output,mkdir() seeks back to the current central-directory start and writes a new local header there. This overwrites or aliases the active member's local header and can produce a corrupt ZIP archive without raising an exception.
Minimal Reproducer
import io
import zipfile
buffer = io.BytesIO()
zf = zipfile.ZipFile(buffer, "w")
member = zf.open("file.txt", mode="w")
zf.mkdir("directory") # Should reject this operation
member.write(b"payload")
member.close()
zf.close()
with zipfile.ZipFile(io.BytesIO(buffer.getvalue())) as broken:
print(broken.namelist())
print(broken.read("directory/"))
Observed behavior:
['directory/', 'file.txt']
Traceback (most recent call last):
File "/home/ubuntu/cpython-main/test.py", line 15, in <module>
print(broken.read("directory/"))
~~~~~~~~~~~^^^^^^^^^^^^^^
File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2165, in read
with self.open(name, "r", pwd) as fp:
~~~~~~~~~^^^^^^^^^^^^^^^^
File "/home/ubuntu/cpython-main/Lib/zipfile/__init__.py", line 2239, in open
raise BadZipFile(
'File name in directory %r and header %r differ.'
% (zinfo.orig_filename, fname))
zipfile.BadZipFile: File name in directory 'directory/' and header b'file.txt' differ.
The exact error may vary with the archive contents, but the resulting archive is structurally inconsistent.
Expected Behavior
mkdir() should reject the operation before changing the archive, consistent with the existing behavior of open(..., mode="w"), write(), writestr(), and close() when a writable member handle is active. A ValueError with a message explaining that another write handle is open would be appropriate.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156079
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en Lib/zipfile/init.py, en ZipFile.mkdir, y compara cómo gestiona los handles de miembros activos en modo escritura con open(..., mode="w"), write(), writestr() y close(). Ejecuta la reproducción mínima; se considera terminado cuando mkdir rechaza la operación con un ValueError antes de modificar el archivo comprimido.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- backend
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 30/100