python / python/cpython

smtplib: missing case in handling 421 response in sendmail

Aperta
#132,796 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Bug report

Bug description:

The sendmail function in smtplib does a good job of handling 421 responses from SMTP servers but is missing one case. This may lead to SMTP client connections not being closed properly.

Background

SMTP servers can send 421 responses whenever they need to shut down the connection (RF 5321 3.8). Clients should take care of closing the connection when receiving a 421.

Details

The sendmail function combines the low-level SMTP commands into a full session for sending some text content via SMTP. The function needs to handle 421 responses, as any single command may return a 421 response. The function already handles almost all cases but is missing one for the call of the data function, which does its own response code checking and may raise an exception.

Test to reproduce

The test was derived from the corresponding rosetta-test SMTP test suite and adapted to the smtplib test infrastructure.

def test_421_during_data_cmd(self):
    smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
                          timeout=support.LOOPBACK_TIMEOUT)
    smtp.noop()
    self.serv._SMTPchannel.data_response = '421 closing'
    with self.assertRaises(smtplib.SMTPDataError):
        smtp.sendmail('John@foo.org', ['Sally@foo.org'], 'test message')
    self.assertIsNone(smtp.sock)
    self.assertEqual(self.serv._SMTPchannel.rcpt_count, 0)
CPython versions tested on:

CPython main branch, 3.10, 3.12

Operating systems tested on:

Linux

Linked PRs
  • gh-132797

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

Esamina Lib/smtplib.py intorno alla chiamata dei dati di sendmail e al controllo della risposta dei dati descritto nell’issue. Esegui o adatta test_421_during_data_cmd nell’infrastruttura di test di smtplib; il lavoro è completato quando il caso 421 solleva SMTPDataError, chiude il socket e lascia rcpt_count a zero.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.