`mailbox.MH.__setitem__()` can destroy a message when replacement fails
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 description:
Summary
`When mailbox.MH replaces an existing message with an invalid str, it correctly raises ValueError, but the original message file may already have been truncated to empty. In other words, the replacement fails while also destroying the existing message content, resulting in data loss.
Affected public API: mailbox.MH.__setitem__().
Minimal reproducer
Run this against CPython before the fix:
import mailbox
import tempfile
with tempfile.TemporaryDirectory() as path:
box = mailbox.MH(path)
key = box.add(b"Subject: original\n\noriginal body\n")
original = box.get_bytes(key)
try:
box[key] = "Subject: caf\u00e9\n\nreplacement body\n"
except ValueError as exc:
print(type(exc).__name__, exc)
print("in-memory:", box.get_bytes(key))
box.close()
reopened = mailbox.MH(path)
print("reopened:", reopened.get_bytes(key))
assert reopened.get_bytes(key) == original
Actual result before the fix:
ValueError String input must be ASCII-only; use bytes or a Message instead
in-memory: b''
reopened: b''
AssertionError
Expected result:
ValueError String input must be ASCII-only; use bytes or a Message instead
in-memory: b'Subject: original\n\noriginal body\n'
reopened: b'Subject: original\n\noriginal body\n'
The same problem occurs if a file-like message object raises while it is being read: the old message is replaced with the bytes written before the exception.
Root cause
MH.__setitem__() performed these operations in this order:
open existing message
-> open the same path with O_TRUNC
-> serialize the replacement with _dump_message()
-> propagate a serialization error
O_TRUNC changes the existing message file before _dump_message() validates or fully reads the replacement. For a non-ASCII str, _dump_message() calls _string_to_bytes() and raises ValueError immediately, leaving the already truncated file in place. There is no rollback path.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-156313
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 với mailbox.MH.setitem() và _dump_message(), sau đó chạy trình tái hiện tối thiểu từ issue trong các bài kiểm thử mailbox của CPython. Hoàn tất khi việc thay thế thất bại một chuỗi không phải ASCII hoặc một message dạng file-like gây ra exception đều để nguyên cả message gốc trong bộ nhớ lẫn message gốc được mở lại.
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
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 35/100