@@ -234,6 +234,32 @@ def test_list_mailbox(self):
234234 self .assertEqual (m ._list_mailbox ('Entwürfe/%' ),
235235 '"Entwürfe/%"' .encode ())
236236
237+ @unittest .skipUnless (ssl , "SSL not available" )
238+ def test_starttls_rejects_buffered_plaintext (self ):
239+ sock = mock .Mock (spec = socket .socket )
240+ sock .recv .return_value = (
241+ b'* OK [CAPABILITY IMAP4rev1 STARTTLS] ready\r \n ' )
242+ with mock .patch .object (imaplib .IMAP4 , '_create_socket' ,
243+ return_value = sock ):
244+ client = imaplib .IMAP4 ('localhost' )
245+ self .addCleanup (client .shutdown )
246+
247+ tag = client .tagpre + str (client .tagnum ).encode ('ascii' )
248+ next_tag = client .tagpre + str (client .tagnum + 1 ).encode ('ascii' )
249+ # Return the STARTTLS response and injected plaintext in one recv(),
250+ # so the test does not depend on how TCP packets are split.
251+ sock .recv .side_effect = [
252+ tag + b' OK Begin TLS\r \n '
253+ b'* CAPABILITY IMAP4rev1 INJECTED\r \n '
254+ + next_tag + b' OK CAPABILITY completed\r \n '
255+ ]
256+ context = mock .Mock ()
257+ context .wrap_socket .return_value = sock
258+ with self .assertRaisesRegex (imaplib .IMAP4 .abort ,
259+ 'unexpected data after STARTTLS response' ):
260+ client .starttls (context )
261+ context .wrap_socket .assert_not_called ()
262+ self .assertFalse (client ._tls_established )
237263
238264if ssl :
239265 class SecureTCPServer (socketserver .TCPServer ):
@@ -1858,10 +1884,67 @@ def test_file_property_setter_should_not_close_previous_file(self):
18581884 f .close .assert_not_called ()
18591885
18601886
1887+ class StartTLSHandler (SimpleIMAPHandler ):
1888+ capabilities = 'STARTTLS'
1889+
1890+ def cmd_STARTTLS (self , tag , args ):
1891+ context = ssl .SSLContext (ssl .PROTOCOL_TLS_SERVER )
1892+ context .load_cert_chain (CERTFILE )
1893+ self ._send_tagged (tag , 'OK' , 'Begin TLS' )
1894+ self .connection = context .wrap_socket (self .connection ,
1895+ server_side = True )
1896+ self .rfile .close ()
1897+ self .wfile .close ()
1898+ self .rfile = self .connection .makefile ('rb' )
1899+ self .wfile = self .connection .makefile ('wb' , buffering = 0 )
1900+ self .capabilities = 'AUTH=PLAIN'
1901+
1902+ def finish (self ):
1903+ try :
1904+ super ().finish ()
1905+ finally :
1906+ self .connection .close ()
1907+
1908+
18611909class NewIMAPTests (NewIMAPTestsMixin , unittest .TestCase ):
18621910 imap_class = imaplib .IMAP4
18631911 server_class = socketserver .TCPServer
18641912
1913+ @unittest .skipUnless (ssl , "SSL not available" )
1914+ def test_starttls (self ):
1915+ _ , server = self ._setup (StartTLSHandler , connect = False )
1916+ self .client = client = self .imap_class (
1917+ 'localhost' , server .server_address [1 ],
1918+ timeout = support .LOOPBACK_TIMEOUT )
1919+ context = ssl .create_default_context (cafile = CAFILE )
1920+ self .assertEqual (client .capabilities , ('IMAP4REV1' , 'STARTTLS' ))
1921+ self .assertEqual (client .starttls (context ), ('OK' , [None ]))
1922+ self .assertIsInstance (client .sock , ssl .SSLSocket )
1923+ self .assertEqual (client .capabilities , ('IMAP4REV1' , 'AUTH=PLAIN' ))
1924+ client .logout ()
1925+
1926+ @unittest .skipUnless (ssl , "SSL not available" )
1927+ def test_starttls_discards_untagged_responses (self ):
1928+ # Untagged responses received before STARTTLS could have been
1929+ # injected by a MITM, so they must not survive the switch to TLS.
1930+ class InjectingStartTLSHandler (StartTLSHandler ):
1931+ def cmd_STARTTLS (self , tag , args ):
1932+ self ._send_textline ('* 3 EXISTS' )
1933+ self ._send_textline (r'* FLAGS (\Injected)' )
1934+ super ().cmd_STARTTLS (tag , args )
1935+
1936+ _ , server = self ._setup (InjectingStartTLSHandler , connect = False )
1937+ self .client = client = self .imap_class (
1938+ 'localhost' , server .server_address [1 ],
1939+ timeout = support .LOOPBACK_TIMEOUT )
1940+ context = ssl .create_default_context (cafile = CAFILE )
1941+ self .assertEqual (client .starttls (context ), ('OK' , [None ]))
1942+ self .assertIsInstance (client .sock , ssl .SSLSocket )
1943+ self .assertEqual (client .untagged_responses , {})
1944+ self .assertEqual (client .response ('EXISTS' ), ('EXISTS' , [None ]))
1945+ self .assertEqual (client .response ('FLAGS' ), ('FLAGS' , [None ]))
1946+ client .logout ()
1947+
18651948
18661949@unittest .skipUnless (ssl , "SSL not available" )
18671950class NewIMAPSSLTests (NewIMAPTestsMixin , unittest .TestCase ):
@@ -1895,6 +1978,7 @@ def test_ssl_verified(self):
18951978 ssl_context = ssl_context )
18961979 client .shutdown ()
18971980
1981+
18981982class ThreadedNetworkedTests (unittest .TestCase ):
18991983 server_class = socketserver .TCPServer
19001984 imap_class = imaplib .IMAP4
0 commit comments