-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
gh-43311: Add regression test for SSLError masking SMTPDataError in rset() #154866
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
zero-context
wants to merge
2
commits into
python:main
from
zero-context:gh-43311-tls-rset-regression-test
+119
−0
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,10 +5,12 @@ | |
| import email.utils | ||
| import hashlib | ||
| import hmac | ||
| import os | ||
| import socket | ||
| import smtplib | ||
| import io | ||
| import re | ||
| import struct | ||
| import sys | ||
| import time | ||
| import select | ||
|
|
@@ -1400,6 +1402,119 @@ def test_lowercase_mail_from_rcpt_to(self): | |
| self.assertIn(['rcpt to:<Sally>'], self.serv._SMTPchannel.all_received_lines) | ||
|
|
||
|
|
||
| SUPPORTS_SMTP_SSL = hasattr(smtplib, 'SMTP_SSL') | ||
| if SUPPORTS_SMTP_SSL: | ||
| import ssl | ||
|
|
||
| CERTFILE = os.path.join( | ||
| os.path.dirname(__file__) or os.curdir, "certdata", "keycert3.pem") | ||
|
|
||
|
|
||
| @unittest.skipUnless(SUPPORTS_SMTP_SSL, 'SSL not supported') | ||
| class SMTPSSLRsetAfterDataErrorTests(unittest.TestCase): | ||
| # gh-43311 (bpo-1481032): a server that rejects DATA and then tears | ||
| # down the TLS connection used to have the automatic rset() inside | ||
| # sendmail()'s except-block raise its own error, masking the original | ||
| # SMTPDataError from the caller. Fixed as a side effect of gh-17498 | ||
| # (all three sendmail() error paths now go through _rset(), and | ||
| # ssl.SSLError has subclassed OSError since then, so send()/getreply() | ||
| # already normalize a dead-connection SSLError into | ||
| # SMTPServerDisconnected instead of leaking a raw ssl error). No | ||
| # regression test previously covered the TLS-specific case -- | ||
| # test__rest_from_mail_cmd above only exercises a plaintext | ||
| # disconnect. | ||
|
|
||
| def setUp(self): | ||
| self.thread_key = threading_helper.threading_setup() | ||
| self.server_ready = threading.Event() | ||
| self.port_holder = [] | ||
| self.thread = threading.Thread(target=self._run_server, daemon=True) | ||
| self.thread.start() | ||
| self.server_ready.wait(support.LOOPBACK_TIMEOUT) | ||
|
|
||
| def tearDown(self): | ||
| threading_helper.join_thread(self.thread) | ||
| threading_helper.threading_cleanup(*self.thread_key) | ||
|
|
||
| def _run_server(self): | ||
| listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| self.addCleanup(listener.close) | ||
| listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | ||
| listener.bind((HOST, 0)) | ||
| listener.listen(1) | ||
| self.port_holder.append(listener.getsockname()[1]) | ||
| self.server_ready.set() | ||
| listener.settimeout(support.LOOPBACK_TIMEOUT) | ||
| try: | ||
| raw_conn, _ = listener.accept() | ||
| except socket.timeout: | ||
| return | ||
| ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | ||
| ctx.load_cert_chain(CERTFILE) | ||
| try: | ||
| conn = ctx.wrap_socket(raw_conn, server_side=True) | ||
| except ssl.SSLError: | ||
| return | ||
| try: | ||
| self._serve_one_rejected_transaction(conn) | ||
| except OSError: | ||
| pass | ||
| finally: | ||
| conn.close() | ||
|
|
||
| def _serve_one_rejected_transaction(self, conn): | ||
| def read_line(): | ||
| data = b"" | ||
| while not data.endswith(b"\r\n"): | ||
| chunk = conn.recv(1) | ||
| if not chunk: | ||
| return data | ||
| data += chunk | ||
| return data | ||
|
|
||
| conn.sendall(b"220 localhost testing TLS DATA rejection\r\n") | ||
| read_line() # EHLO/HELO | ||
| conn.sendall(b"250 localhost\r\n") | ||
| read_line() # MAIL FROM | ||
| conn.sendall(b"250 OK\r\n") | ||
| read_line() # RCPT TO | ||
| conn.sendall(b"250 OK\r\n") | ||
| read_line() # DATA | ||
| # Must accept with 354 here: a non-354 response makes data() | ||
| # raise SMTPDataError *directly*, before sendmail() ever reaches | ||
| # its own self._rset() call. The path this test targets only | ||
| # exists when the message body is accepted for transfer and the | ||
| # *final* response (after the end-of-data terminator) is the | ||
| # rejection -- that's the sendmail() branch that calls | ||
| # self._rset() before raising. | ||
| conn.sendall(b"354 go ahead\r\n") | ||
| received = b"" | ||
| while not received.endswith(b"\r\n.\r\n"): | ||
| chunk = conn.recv(4096) | ||
| if not chunk: | ||
| break | ||
| received += chunk | ||
| conn.sendall(b"554 Transaction failed\r\n") | ||
| # Tear the connection down hard (RST, not a clean TLS | ||
| # close_notify) so the client's automatic rset() -- called from | ||
| # sendmail()'s except-block for the SMTPDataError above -- hits a | ||
| # genuinely dead socket instead of a graceful close. | ||
| conn.setsockopt( | ||
| socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)) | ||
|
|
||
| def test_original_error_not_masked_by_ssl_rset_failure(self): | ||
| port = self.port_holder[0] | ||
| client_ctx = ssl._create_unverified_context() | ||
| smtp = smtplib.SMTP_SSL( | ||
| HOST, port, context=client_ctx, timeout=support.LOOPBACK_TIMEOUT) | ||
| self.addCleanup(smtp.close) | ||
| with self.assertRaises(smtplib.SMTPDataError) as caught: | ||
| smtp.sendmail( | ||
| '[email protected]', ['[email protected]'], | ||
| 'Subject: hi\r\n\r\nbody') | ||
| self.assertEqual(caught.exception.smtp_code, 554) | ||
|
|
||
|
|
||
| class SimSMTPUTF8Server(SimSMTPServer): | ||
|
|
||
| def __init__(self, *args, **kw): | ||
|
|
||
4 changes: 4 additions & 0 deletions
4
Misc/NEWS.d/next/Tests/2026-07-29-10-49-46.gh-issue-43311.tIDaHp.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Added a regression test for :mod:`smtplib` confirming that an error | ||
| raised while resetting the connection after a rejected ``DATA`` command | ||
| no longer masks the original :exc:`~smtplib.SMTPDataError` over a TLS | ||
| connection. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like an un ecessary addition. Can't you just extend existing tests with existing infra? and please do not just ask an agent to do it. You must be able to explain the change yourself without any AI assistance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes i built a tool to help write and test code. I verified it actually catches the regression by temporarily breaking the fix and confirming the test failed, then confirmed it passes against the current code. Happy to answer questions about what it's doing. Just thought it might help. If not please ignore. Cheers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not seem a human answer. I do not want you to build a tool though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also does not answer my request about this useless complication in the tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since SimSMTPServer is plaintext-only, I couldn't use it to test this TLS-specific bug. Adding TLS support to the existing server felt like overkill compared to just making a quick, dedicated class