email.parser.Parser dropping headers into payload if space between header name and colon

未关闭
#93,158 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
25/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
停滞
技术栈
python
领域
backend

调研方向

从提供的 email.parser.BytesParser reproducer 开始,并将解析后的 headers 与 get_payload() 进行比较。查看已链接的 PR gh-93176,了解已经在进行的工作;当冒号前带有空白的 headers 仍保持为 headers,且不会被移入 payload 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

3.11 3.12 3.13 topic-email type-bug

In some cases, I have seen the standard library email message parser (invoked via email.parser.BytesParser) losing headers, in cases where a message has a header line where a space appears between the header name and the delimiting colon following.

Sample which reproduces this:

Subject: test-ignore
To: someone@somedomain.com
x-fred :dead
Date: Fri, 20 May 2022 18:13:19 +1200
From: sender@somedomain.com

Hello, this is a test message

When this is parsed in email.parser.BytesParser, the resulting EmailMessage object has these headers, as dumped by calling .items():

[('Subject', 'test-ignore'),
 ('To', ': someone@somedomain.com;')
]

Meanwhile, the dropped headers end up in the message payload, as seen from calling .get_payload():

x-fred :dead
Date: Fri, 20 May 2022 18:13:19 +1200
From: sender@somedomain.com

Hello, this is a test message

This failure to gracefully cope with a non-compliant header puts Python3's standard library email parser in breach of RFC5322, Section 4.5: ...any amount of white space is allowed [in the header] before the ":" at the end of the [header] field name...

To cope with email.parser.Parser's failure to cope with this archaic header format, I've had to implement a crude workaround of detecting if any expected headers are absent in the resulting email.message.EmailMessage object, and if so, manually 'massaging' the headers part of the raw message, then re-submitting it to the Parser.

However, I'd suggest this really does need to be fixed within Python's standard library module.

Linked PRs
  • gh-93176
主要语言
Python
星标
77.2k
派生
36k
平均合并
1 天 9 小时
30 天内合并 PR
558

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python/cpython 的其他 Issue

查看 python/cpython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。