`zipfile` append corrupts a file due to auto filename sanitization
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 77.2k
- Forks
- 35.9k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug report
Bug description:
When a ZIP archive is opened and rewritten in append mode, a member having a non-standard filename will be corrupted because its filename rewritten to the central directory is auto-sanitized and thus inconsistent with the immutable local file entry when loaded in the future.
This can easily happen if a member containing \ is added in POSIX and the archive is then appended in Windows.
For example:
import io
import zipfile
from unittest import mock
TESTFN = io.BytesIO()
# A file written in POSIX allows '\\'
with mock.patch('os.sep', '/'), mock.patch('os.altsep', None), \
zipfile.ZipFile(TESTFN, mode="w") as zipfp:
zi = zipfile.ZipInfo('MyFolder/My\\File.txt')
zipfp.writestr(zi, 'foo')
# A file written in Windows has '\\' replaced with '/'
with mock.patch('os.sep', '\\'), mock.patch('os.altsep', '/'), \
zipfile.ZipFile(TESTFN, "a") as zipfp:
# trigger archive rewriting
zipfp.comment = b''
with zipfile.ZipFile(TESTFN, "r") as zipfp:
zi = zipfp.infolist()[0]
print('content:', zipfp.read(zi))
The above code raises an error: zipfile.BadZipFile: File name in directory 'MyFolder/My/File.txt' and header b'MyFolder/My\\File.txt' differ.
CPython versions tested on:
3.16, 3.14
Operating systems tested on:
No response
Linked PRs
- gh-153626
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, den Reproducer im Issue auszuführen und die Pfade zur Archivumschreibung im Append-Modus und zur Bereinigung von Dateinamen in Python's zipfile zu untersuchen. Erledigt ist die Aufgabe, wenn ein Archiv, das mit einem Member erstellt wurde, dessen Name einen Backslash enthält, erweitert und anschließend gelesen werden kann, ohne dass eine Abweichung zwischen Verzeichnis- und Header-Dateiname auftritt; das Issue verlinkt den PR gh-153626, also prüfe diese Arbeit zuerst.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- backend
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100