@@ -286,6 +286,16 @@ def testEXPNNotImplemented(self):
286
286
self .assertEqual (smtp .getreply (), expected )
287
287
smtp .quit ()
288
288
289
+ def test_issue43124_putcmd_escapes_newline (self ):
290
+ # see: https://bugs.python.org/issue43124
291
+ smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' ,
292
+ timeout = 10 ) # support.LOOPBACK_TIMEOUT in newer Pythons
293
+ self .addCleanup (smtp .close )
294
+ with self .assertRaises (ValueError ) as exc :
295
+ smtp .putcmd ('helo\n X-INJECTED' )
296
+ self .assertIn ("prohibited newline characters" , str (exc .exception ))
297
+ smtp .quit ()
298
+
289
299
def testVRFY (self ):
290
300
smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' , timeout = 3 )
291
301
expected = (252 , b'Cannot VRFY user, but will accept message ' + \
@@ -355,6 +365,51 @@ def testSendNeedingDotQuote(self):
355
365
mexpect = '%s%s\n %s' % (MSG_BEGIN , m , MSG_END )
356
366
self .assertEqual (self .output .getvalue (), mexpect )
357
367
368
+ def test_issue43124_escape_localhostname (self ):
369
+ # see: https://bugs.python.org/issue43124
370
+ # connect and send mail
371
+ m = 'wazzuuup\n linetwo'
372
+ smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'hi\n X-INJECTED' ,
373
+ timeout = 10 ) # support.LOOPBACK_TIMEOUT in newer Pythons
374
+ self .addCleanup (smtp .close )
375
+ with self .assertRaises (ValueError ) as exc :
376
+
377
+ self .assertIn (
378
+ "prohibited newline characters: ehlo hi\\ nX-INJECTED" ,
379
+ str (exc .exception ),
380
+ )
381
+ # XXX (see comment in testSend)
382
+ time .sleep (0.01 )
383
+ smtp .quit ()
384
+
385
+ debugout = smtpd .DEBUGSTREAM .getvalue ()
386
+ self .assertNotIn ("X-INJECTED" , debugout )
387
+
388
+ def test_issue43124_escape_options (self ):
389
+ # see: https://bugs.python.org/issue43124
390
+ # connect and send mail
391
+ m = 'wazzuuup\n linetwo'
392
+ smtp = smtplib .SMTP (
393
+ HOST , self .port , local_hostname = 'localhost' ,
394
+ timeout = 10 ) # support.LOOPBACK_TIMEOUT in newer Pythons
395
+
396
+ self .addCleanup (smtp .close )
397
+
398
+ with self .assertRaises (ValueError ) as exc :
399
+ smtp .
mail (
"[email protected] " , [
"X-OPTION\n X-INJECTED-1" ,
"X-OPTION2\n X-INJECTED-2" ])
400
+ msg = str (exc .exception )
401
+ self .assertIn ("prohibited newline characters" , msg )
402
+ self .assertIn ("X-OPTION\\ nX-INJECTED-1 X-OPTION2\\ nX-INJECTED-2" , msg )
403
+ # XXX (see comment in testSend)
404
+ time .sleep (0.01 )
405
+ smtp .quit ()
406
+
407
+ debugout = smtpd .DEBUGSTREAM .getvalue ()
408
+ self .assertNotIn ("X-OPTION" , debugout )
409
+ self .assertNotIn ("X-OPTION2" , debugout )
410
+ self .assertNotIn ("X-INJECTED-1" , debugout )
411
+ self .assertNotIn ("X-INJECTED-2" , debugout )
412
+
358
413
def testSendNullSender (self ):
359
414
m = 'A test message'
360
415
smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' , timeout = 3 )
0 commit comments