`zipfile` append corrupts a file due to auto filename sanitization
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách chạy reproducer trong issue và kiểm tra các đường dẫn ghi lại archive ở chế độ append và sanitization tên tệp của zipfile trong Python. Công việc được hoàn tất khi một archive được tạo với một member có tên chứa dấu gạch chéo ngược có thể được append và sau đó đọc mà không có sự không khớp giữa tên tệp trong directory và tên tệp trong header; issue liên kết tới PR gh-153626, vì vậy hãy kiểm tra phần việc đó trước.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- backend
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 35/100