email ContentManager mutates messages after failed content validation and accepts unknown byte CTE values
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
Summary
EmailMessage.set_content() clears existing content before built-in content validation completes. A failing text-content request therefore destroys the original payload. Separately, the bytes content handler accepts arbitrary cte strings and serializes them as Content-Transfer-Encoding, even though the documented API defines a fixed set of permitted values. Together, these behaviors let invalid content requests corrupt an existing message or produce invalid outgoing MIME metadata.
Reproduction Code
from email.message import EmailMessage
# Failed text setup loses the prior message content.
msg = EmailMessage()
msg.set_content("original")
before = msg.as_bytes()
try:
msg.set_content("replacement", charset="does-not-exist")
except LookupError as exc:
print(type(exc).__name__, exc)
print(msg.as_bytes() == before)
print(msg.as_string())
# Unsupported byte CTE is accepted and emitted unchanged.
msg = EmailMessage()
msg.set_content(
b"abc",
maintype="application",
subtype="octet-stream",
cte="not-a-transfer-encoding",
)
print(msg.as_bytes().decode("ascii"))
Actual Behavior
The first request raises LookupError: unknown encoding: does-not-exist, then prints False. The serialized message no longer contains original; it retains MIME headers added before the failure.
The second request succeeds and produces:
Content-Type: application/octet-stream
Content-Transfer-Encoding: not-a-transfer-encoding
MIME-Version: 1.0
abc
A failing add_attachment("replacement", charset="does-not-exist") also leaves the parent converted to a one-part multipart/mixed message.
Expected Behavior
Invalid content requests should raise before changing the target message. In particular, a failed set_content() or add_attachment() call should preserve the prior serialized message and MIME structure.
The bytes content handler should reject unsupported CTE values with ValueError, consistently with the documented CTE set and the text content handler, rather than emitting an unrecognized Content-Transfer-Encoding header.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-157263
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 các điểm vào email.message.EmailMessage.set_content() và add_attachment(), sau đó theo dõi các trình xử lý nội dung văn bản và bytes được các bản tái hiện sử dụng. Chạy các ví dụ được cung cấp và các bài kiểm thử email liên quan; hoàn thành khi các yêu cầu nội dung bị lỗi vẫn giữ nguyên thông báo đã tuần tự hóa trước đó và các giá trị CTE bytes không được hỗ trợ bị từ chố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
- 25/100