python / python/cpython

email ContentManager mutates messages after failed content validation and accepts unknown byte CTE values

Aperta
#157,254 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

stdlib topic-email type-bug
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Bug report

Summary

EmailMessage.set_content() clears existing content before built-in content validation completes. A failing text-content request therefore destroys the original payload. Separately, the bytes content handler accepts arbitrary cte strings and serializes them as Content-Transfer-Encoding, even though the documented API defines a fixed set of permitted values. Together, these behaviors let invalid content requests corrupt an existing message or produce invalid outgoing MIME metadata.

Reproduction Code
from email.message import EmailMessage

# Failed text setup loses the prior message content.
msg = EmailMessage()
msg.set_content("original")
before = msg.as_bytes()
try:
    msg.set_content("replacement", charset="does-not-exist")
except LookupError as exc:
    print(type(exc).__name__, exc)
print(msg.as_bytes() == before)
print(msg.as_string())

# Unsupported byte CTE is accepted and emitted unchanged.
msg = EmailMessage()
msg.set_content(
    b"abc",
    maintype="application",
    subtype="octet-stream",
    cte="not-a-transfer-encoding",
)
print(msg.as_bytes().decode("ascii"))
Actual Behavior

The first request raises LookupError: unknown encoding: does-not-exist, then prints False. The serialized message no longer contains original; it retains MIME headers added before the failure.

The second request succeeds and produces:

Content-Type: application/octet-stream
Content-Transfer-Encoding: not-a-transfer-encoding
MIME-Version: 1.0

abc

A failing add_attachment("replacement", charset="does-not-exist") also leaves the parent converted to a one-part multipart/mixed message.

Expected Behavior

Invalid content requests should raise before changing the target message. In particular, a failed set_content() or add_attachment() call should preserve the prior serialized message and MIME structure.

The bytes content handler should reject unsupported CTE values with ValueError, consistently with the documented CTE set and the text content handler, rather than emitting an unrecognized Content-Transfer-Encoding header.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-157263

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 i punti di ingresso email.message.EmailMessage.set_content() e add_attachment(), quindi segui i gestori dei contenuti testuali e bytes utilizzati dalle riproduzioni. Esegui gli esempi forniti e i test email correlati; il lavoro è completato quando le richieste di contenuto fallite conservano il messaggio serializzato precedente e i valori CTE di bytes non supportati vengono rifiutati.

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.