email: non-ASCII whitespace after a space is lost from address display names

オープン
#161,678 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
42/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

The bug is in get_fws() in Lib/email/_header_value_parser.py, which uses value.lstrip() and so strips Unicode whitespace beyond SP and HTAB. The issue body already links PR gh-161679, though the linked-PR list is empty, so check that PR before starting. Done means display names such as '. \xa0b' survive a round trip through the email parser, with a regression test in Lib/test/test_email.

索引モデルが issue の本文から書いたものです。

説明

stdlib topic-email type-bug
Bug description:

The email header parser silently drops Unicode whitespace other than SP and HTAB (for example U+00A0 NO-BREAK SPACE, U+2003 EM SPACE, U+3000 IDEOGRAPHIC SPACE) when it follows a space in an address display name. A message generated by the email package itself doesn't round-trip:

import email
from email import policy
from email.message import EmailMessage
from email.headerregistry import Address

m = EmailMessage()
m['To'] = Address('. \xa0b', 'u', 'example.com')
print(m['To'])          # =?utf-8?q?=2E_=C2=A0?=b <[email protected]>
m2 = email.message_from_string(m.as_string(), policy=policy.default)
print(ascii(m2['To'].addresses[0].display_name))   # '. b', expected '. \xa0b'

Received mail is affected too: To: =?utf-8?q?x_=C2=A0b?= <[email protected]> gives the display name 'x b'.

The cause is get_fws() in Lib/email/_header_value_parser.py:

def get_fws(value):
    """FWS = 1*WSP
    ...
    newvalue = value.lstrip()

str.lstrip() strips all Unicode whitespace, but FWS is only SP and HTAB (RFC 5322 §3.2.2: FWS = ([*WSP CRLF] 1*WSP) / obs-FWS; RFC 5234: WSP = SP / HTAB). The extra characters end up in a WhiteSpaceTerminal, whose value is always a single space, so they are lost.

I couldn't find a test or comment saying stripping all Unicode whitespace was intended.

CPython versions tested on:

3.13, CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-161679
主要言語
Python
スター
78.2k
フォーク
36.6k
平均マージ
19時間 57分
マージ済み PR(30日)
630

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python/cpython のほかの issue

python/cpython の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。