Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Lib/imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,9 @@ def starttls(self, ssl_context=None):
ssl_context = ssl._create_stdlib_context()
typ, dat = self._simple_command(name)
if typ == 'OK':
# Discard received state before TLS was established
# since data could have been injected by a MITM.
self.untagged_responses = {}
self.sock = ssl_context.wrap_socket(self.sock,
server_hostname=self.host)
self.file = self.sock.makefile('rb')
Expand Down
57 changes: 57 additions & 0 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,10 +1720,67 @@ def test_control_characters(self):
self.assertEqual(server.is_selected, [f'"a{c}b"'])


class StartTLSHandler(SimpleIMAPHandler):
capabilities = 'STARTTLS'

def cmd_STARTTLS(self, tag, args):
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
context.load_cert_chain(CERTFILE)
self._send_tagged(tag, 'OK', 'Begin TLS')
self.connection = context.wrap_socket(self.connection,
server_side=True)
self.rfile.close()
self.wfile.close()
self.rfile = self.connection.makefile('rb')
self.wfile = self.connection.makefile('wb', buffering=0)
self.capabilities = 'AUTH=PLAIN'

def finish(self):
try:
super().finish()
finally:
self.connection.close()


class NewIMAPTests(NewIMAPTestsMixin, unittest.TestCase):
imap_class = imaplib.IMAP4
server_class = socketserver.TCPServer

@unittest.skipUnless(ssl, "SSL not available")
def test_starttls(self):
_, server = self._setup(StartTLSHandler, connect=False)
self.client = client = self.imap_class(
'localhost', server.server_address[1],
timeout=support.LOOPBACK_TIMEOUT)
context = ssl.create_default_context(cafile=CAFILE)
self.assertEqual(client.capabilities, ('IMAP4REV1', 'STARTTLS'))
self.assertEqual(client.starttls(context), ('OK', [None]))
self.assertIsInstance(client.sock, ssl.SSLSocket)
self.assertEqual(client.capabilities, ('IMAP4REV1', 'AUTH=PLAIN'))
client.logout()

@unittest.skipUnless(ssl, "SSL not available")
def test_starttls_discards_untagged_responses(self):
# Untagged responses received before STARTTLS could have been
# injected by a MITM, so they must not survive the switch to TLS.
class InjectingStartTLSHandler(StartTLSHandler):
def cmd_STARTTLS(self, tag, args):
self._send_textline('* 3 EXISTS')
self._send_textline(r'* FLAGS (\Injected)')
super().cmd_STARTTLS(tag, args)

_, server = self._setup(InjectingStartTLSHandler, connect=False)
self.client = client = self.imap_class(
'localhost', server.server_address[1],
timeout=support.LOOPBACK_TIMEOUT)
context = ssl.create_default_context(cafile=CAFILE)
self.assertEqual(client.starttls(context), ('OK', [None]))
self.assertIsInstance(client.sock, ssl.SSLSocket)
self.assertEqual(client.untagged_responses, {})
self.assertEqual(client.response('EXISTS'), ('EXISTS', [None]))
self.assertEqual(client.response('FLAGS'), ('FLAGS', [None]))
client.logout()


@unittest.skipUnless(ssl, "SSL not available")
class NewIMAPSSLTests(NewIMAPTestsMixin, unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.. cve: CVE-2026-115559

:meth:`imaplib.IMAP4.starttls` now discards cached untagged responses received
before STARTTLS, preventing them from being treated as TLS-protected data.
Loading