zipfile: ZipInfo.comment exceeding 65535 bytes causes struct.error in ZipFile.close() without proper validation
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 75/100
Línea de trabajo
Empieza en el módulo zipfile, en ZipFile.writestr() y _write_end_record(), y compara el tratamiento de los comentarios por entrada con la comprobación de longitud de ZipFile.comment a nivel del archivo. Añade una validación para que un ZipInfo.comment demasiado largo produzca el ValueError descriptivo indicado; añade o ejecuta una prueba de regresión con un comentario de más de 65.535 bytes. La tarea estará terminada cuando la escritura falle con ese error antes de que close() pueda dejar el archivo sin finalizar.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Bug report
Bug description:
When adding an entry to a ZipFile using a ZipInfo object whose comment exceeds 65,535 bytes (the maximum size representable by the 16-bit unsigned short H in the ZIP central directory header), writestr() accepts the entry without validation.
Later, when ZipFile.close() is called (or upon exiting a with ZipFile(...) block), _write_end_record() attempts to pack len(zinfo.comment) into structCentralDir using H, raising an unhandled struct.error:
struct.error: 'H' format requires 0 <= number <= 65535
Because this failure occurs during close(), the central directory and end-of-central-directory record are never written, leaving the ZIP archive unfinalized.
In contrast, archive-level comments (ZipFile.comment) are guarded against ZIP_MAX_COMMENT (65,535 bytes) with a warning and truncation.
Steps/Code to Reproduce:
import zipfile, io
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w") as zf:
zinfo = zipfile.ZipInfo("test.txt")
zinfo.comment = b"x" * 70000
zf.writestr(zinfo, b"content")
Actual outcome:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "zipfile/__init__.py", line 1475, in __exit__
self.close()
File "zipfile/__init__.py", line 2060, in close
self._write_end_record()
File "zipfile/__init__.py", line 2107, in _write_end_record
centdir = struct.pack(structCentralDir,
stringCentralDir, create_version,
...
len(filename), len(extra_data),
len(zinfo.comment),
...)
struct.error: 'H' format requires 0 <= number <= 65535
Expected outcome:
ZipFile.writestr() (or ZipInfo) should validate len(zinfo.comment) <= 65535 and raise a descriptive ValueError(f"ZipInfo.comment exceeds maximum allowable length of {ZIP_MAX_COMMENT} bytes") (or truncate with a warning similar to archive comments) to prevent corrupting the close sequence.
CPython versions tested on:
Python 3.12, 3.13, main
Operating systems tested on:
Linux
Linked PRs
- gh-162149
- Lenguaje dominante
- Python
- Estrellas
- 78.4k
- Forks
- 36.6k
- Merge medio
- 23 h 34 min
- PR fusionados (30 d)
- 610
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Sin Dockerfile ni archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la 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.
Más de python/cpython
-
interpreter-core type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
stdlib topic-subprocess type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
corner case: base64.b32decode ignorechars=b'=' miscounts paddingPosiblemente ocupada @ElDShell la tomó hoy. Abierto3.15 easy type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
python/cpython#162353 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
TracebackException.lineno changes from str to int after format() when a keyword typo suggestion is foundPosiblemente ocupada @Sudhakar7J la tomó hace 2 días. Abiertostdlib type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 67/100
python/cpython#162296 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[HACL] The `HACL_CAN_COMPILE_VEC128` macro is not defined when building Python with `-Wundef`Posiblemente ocupada @tamaki0575-cloud la tomó hace 1 día. Abiertobuild extension-modules topic-C-API type-bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 64/100
python/cpython#162219 · 2 comentarios ·
Los mantenedores suelen responder en 1 día
Todos los issues de python/cpython
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
vllm-project/vllm#69304 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
pydanty:is-working
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
pydantic/pydantic-ai#14187 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
test
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
prebid/salesagent#2676 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 1/5 1-3 horas Aptitud para principiantes 62/100
sunososobro-hub/cloudball#339 ·
Los mantenedores suelen responder en 1 día