Incorrect decoding of preamble in email parser
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 36k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
Method: email.message_from_binary_file()
It seems we've found an issue with the email.parser module when parsing raw binary MIME message where the preamble contains UTF-8 encoded data
When using the .as_string() method on the returned message, the unicode data will contain invalid UTF-8 characters ("surrogates not allowed")
The problem does not occur when using the email.message_from_string() method
I've made a somewhat minimal example (from the MIME RFC) exposing the issue.
Here "préamble" is decoded as "pr\udcc3\udca9amble"
import io
import email
import email.policy
CONTENTS = """
From: Nathaniel Borenstein <nsb@bellcore.com>
To: Ned Freed <ned@innosoft.com>
Subject: Sample message
MIME-Version: 1.0
Content-type: multipart/mixed; boundary="i-am-boundary"
This is the préamble. It is to be ignored, though it
is a handy place for mail composers to include an
explanatory note to non-MIME compliant readers.
--i-am-boundary
Content-type: text/plain; charset=us-ascii
This is explicitly typed plain ASCII text.
It DOES end with a linebreak.
--i-am-boundary
Content-type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
This should be correctly encapsulated: Un petit café ?
--i-am-boundary--
This is the epilogue. It is also to be ignored.
""".lstrip()
CONTENTS_BYTES = io.BytesIO(CONTENTS.encode())
# Does not have an impact on the result
POLICY = email.policy.default.clone(utf8=True)
def show_message(msg):
# Parts are correctly decoded in all cases
for i, part in enumerate(msg.iter_parts(), 1):
print(f'MIME PART {i}:')
as_string = part.as_string()
as_bytes = as_string.encode()
print(as_string)
as_string = msg.as_string()
# When source was bytes, the unicode result of as_string is incorrect
as_bytes = as_string.encode()
print(as_string)
msg_from_binary = email.message_from_binary_file(CONTENTS_BYTES, policy=POLICY)
show_message(msg_from_binary)
# UnicodeEncodeError: 'utf-8' codec can't encode characters in position 192-193: surrogates not allowed
# Using the unicode representation is OK
#msg_from_string = email.message_from_string(CONTENTS, EmailMessage)
#show_message(msg_from_string)
I've tried adding a charset and content-transfer-encoding: 8bit in the headers, with the same result (I do not know if this is actually valid)
Looking at the current code it seems that BytesParser always uses the ASCII encoding with errors='surrogateescape'
CPython versions tested on:
3.11, CPython main branch
Operating systems tested on:
Linux
Linked PRs
- gh-134384
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 issue 中最小的二进制 MIME 示例开始,检查 Lib/email/parser.py 第 95-103 行附近的代码,比较二进制文件解析与字符串解析。查看链接的 PR gh-134384 以了解当前的工作。完成的标志是:渲染解析后的消息时,preamble 中的 UTF-8 数据不再变成代理字符。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 缺陷
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 活跃度
- 停滞
- 描述清晰度
- 描述清楚
- 新手友好度
- 20/100