python / python/cpython

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

Aperta
#93,158 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

3.11 3.12 3.13 topic-email type-bug
Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

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.

Direzione di ricerca

Inizia con il reproducer fornito per email.parser.BytesParser e confronta gli header analizzati con get_payload(). Esamina la PR collegata gh-93176 per il lavoro già in corso; il lavoro è completo quando gli header con spazi bianchi prima dei due punti rimangono header e non vengono spostati nel payload.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.