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