python / python/cpython

smtplib: missing case in handling 421 response in sendmail

未關閉
#132,796 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

3.13 3.14 stdlib type-bug
主要語言
Python
星號
77.2k
分支
36k
PR 合併指標
PR 指標待擷取

描述

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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

研究方向

檢查 Lib/smtplib.py 中 sendmail 的 data 呼叫附近,以及 issue 中描述的資料回應檢查。執行或調整 smtplib 測試基礎設施中的 test_421_during_data_cmd;完成的標準是 421 情況引發 SMTPDataError、關閉 socket,並使 rcpt_count 保持為零。

由索引模型根據 Issue 內容生成。

評估

技術堆疊
python
領域
networking
Issue 類型
缺陷
難度
2/5
預估耗時
1-3 小時
活躍度
停滯
描述清晰度
描述清楚
新手友好度
30/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。