`ZipFile.mkdir()` corrupts archives during an active write
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 36k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
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
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece em Lib/zipfile/init.py, em ZipFile.mkdir, e compare o tratamento que ele faz dos handles de membros graváveis ativos com open(..., mode="w"), write(), writestr() e close(). Execute o reprodutor mínimo; considera-se concluído quando mkdir rejeitar a operação com um ValueError antes de alterar o arquivo compactado.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- backend
- Tipo de issue
- Bug
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 30/100