@@ -332,6 +332,16 @@ def testEXPNNotImplemented(self):
332
332
self .assertEqual (smtp .getreply (), expected )
333
333
smtp .quit ()
334
334
335
+ def test_issue43124_putcmd_escapes_newline (self ):
336
+ # see: https://bugs.python.org/issue43124
337
+ smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' ,
338
+ timeout = support .LOOPBACK_TIMEOUT )
339
+ self .addCleanup (smtp .close )
340
+ with self .assertRaises (ValueError ) as exc :
341
+ smtp .putcmd ('helo\n X-INJECTED' )
342
+ self .assertIn ("prohibited newline characters" , str (exc .exception ))
343
+ smtp .quit ()
344
+
335
345
def testVRFY (self ):
336
346
smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' ,
337
347
timeout = support .LOOPBACK_TIMEOUT )
@@ -413,6 +423,51 @@ def testSendNeedingDotQuote(self):
413
423
mexpect = '%s%s\n %s' % (MSG_BEGIN , m , MSG_END )
414
424
self .assertEqual (self .output .getvalue (), mexpect )
415
425
426
+ def test_issue43124_escape_localhostname (self ):
427
+ # see: https://bugs.python.org/issue43124
428
+ # connect and send mail
429
+ m = 'wazzuuup\n linetwo'
430
+ smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'hi\n X-INJECTED' ,
431
+ timeout = support .LOOPBACK_TIMEOUT )
432
+ self .addCleanup (smtp .close )
433
+ with self .assertRaises (ValueError ) as exc :
434
+
435
+ self .assertIn (
436
+ "prohibited newline characters: ehlo hi\\ nX-INJECTED" ,
437
+ str (exc .exception ),
438
+ )
439
+ # XXX (see comment in testSend)
440
+ time .sleep (0.01 )
441
+ smtp .quit ()
442
+
443
+ debugout = smtpd .DEBUGSTREAM .getvalue ()
444
+ self .assertNotIn ("X-INJECTED" , debugout )
445
+
446
+ def test_issue43124_escape_options (self ):
447
+ # see: https://bugs.python.org/issue43124
448
+ # connect and send mail
449
+ m = 'wazzuuup\n linetwo'
450
+ smtp = smtplib .SMTP (
451
+ HOST , self .port , local_hostname = 'localhost' ,
452
+ timeout = support .LOOPBACK_TIMEOUT )
453
+
454
+ self .addCleanup (smtp .close )
455
+
456
+ with self .assertRaises (ValueError ) as exc :
457
+ smtp .
mail (
"[email protected] " , [
"X-OPTION\n X-INJECTED-1" ,
"X-OPTION2\n X-INJECTED-2" ])
458
+ msg = str (exc .exception )
459
+ self .assertIn ("prohibited newline characters" , msg )
460
+ self .assertIn ("X-OPTION\\ nX-INJECTED-1 X-OPTION2\\ nX-INJECTED-2" , msg )
461
+ # XXX (see comment in testSend)
462
+ time .sleep (0.01 )
463
+ smtp .quit ()
464
+
465
+ debugout = smtpd .DEBUGSTREAM .getvalue ()
466
+ self .assertNotIn ("X-OPTION" , debugout )
467
+ self .assertNotIn ("X-OPTION2" , debugout )
468
+ self .assertNotIn ("X-INJECTED-1" , debugout )
469
+ self .assertNotIn ("X-INJECTED-2" , debugout )
470
+
416
471
def testSendNullSender (self ):
417
472
m = 'A test message'
418
473
smtp = smtplib .SMTP (HOST , self .port , local_hostname = 'localhost' ,
0 commit comments