email: UnboundLocalError parsing a Message-ID whose domain literal starts with whitespace

Aperta
#161,966 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start in email._header_value_parser, at _get_ptext_to_endchars, and reproduce the issue with parse_message_id('<a@[ x]>'). Done means parsing returns a header with a defect instead of raising UnboundLocalError; the issue mentions linked PR gh-161967, so check its status before starting.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Bug report

email._header_value_parser.parse_message_id and parse_message_ids raise UnboundLocalError when the part after @ is a domain literal that starts with a space or tab, such as <a@[ x]>. Because of this, reading Message-ID, References or In-Reply-To from such a message with policy.default raises instead of returning a header with a defect. gh-134152 fixed the same error for an empty literal (<T@[), but a literal that begins with whitespace still reaches the same code.

import email
from email import policy
from email._header_value_parser import parse_message_id

parse_message_id('<a@[ x]>')

msg = email.message_from_string('Message-ID: <a@[ x]>\n\nbody\n', policy=policy.default)
msg['Message-ID']

Actual:

UnboundLocalError: cannot access local variable 'pos' where it is not associated with a value

Expected: no exception. The header should parse with a defect, the same way <a@[x ]> already does (it gives a MessageID with an ObsoleteHeaderDefect, "obsolete id-right in msg-id").

The cause is in _get_ptext_to_endchars: when value starts with whitespace, the first fragment from _wsp_splitter is empty, the for pos in ... loop never runs, and the else clause reads pos before it is assigned.

CPython versions tested on:

3.15, CPython main branch

Operating systems tested on:

Linux


Found while building a Lean 4 model of CPython (https://broyojo.com/python-formalization/). AI tools (Claude) helped investigate the findings and prepare this report. The examples were rerun on CPython 3.15.2 and main, and the report received technical review. Record: textlibs-email-msgid-no-fold-literal-space

Linked PRs
  • gh-161967
Lingua principale
Python
Stelle
78.2k
Fork
36.6k
Merge medio
19h 57m
PR unite (30g)
630

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.