bpo-27820: Fix AUTH LOGIN logic in smtplib.SMTP (GH-24118) (#24832) · python/cpython@32717b9 (original) (raw)

`@@ -785,7 +785,7 @@ def found_terminator(self):

`

785

785

`except ResponseException as e:

`

786

786

`self.smtp_state = self.COMMAND

`

787

787

`self.push('%s %s' % (e.smtp_code, e.smtp_error))

`

788

``

`-

return

`

``

788

`+

return

`

789

789

`super().found_terminator()

`

790

790

``

791

791

``

`@@ -851,6 +851,11 @@ def _auth_login(self, arg=None):

`

851

851

`self._authenticated(self._auth_login_user, password == sim_auth[1])

`

852

852

`del self._auth_login_user

`

853

853

``

``

854

`+

def _auth_buggy(self, arg=None):

`

``

855

`+

This AUTH mechanism will 'trap' client in a neverending 334

`

``

856

`+

base64 encoded 'BuGgYbUgGy'

`

``

857

`+

self.push('334 QnVHZ1liVWdHeQ==')

`

``

858

+

854

859

`def _auth_cram_md5(self, arg=None):

`

855

860

`if arg is None:

`

856

861

`self.push('334 {}'.format(sim_cram_md5_challenge))

`

`@@ -1069,6 +1074,44 @@ def testAUTH_LOGIN(self):

`

1069

1074

`self.assertEqual(resp, (235, b'Authentication Succeeded'))

`

1070

1075

`smtp.close()

`

1071

1076

``

``

1077

`+

def testAUTH_LOGIN_initial_response_ok(self):

`

``

1078

`+

self.serv.add_feature("AUTH LOGIN")

`

``

1079

`+

with smtplib.SMTP(HOST, self.port, local_hostname='localhost',

`

``

1080

`+

timeout=support.LOOPBACK_TIMEOUT) as smtp:

`

``

1081

`+

smtp.user, smtp.password = sim_auth

`

``

1082

`+

smtp.ehlo("test_auth_login")

`

``

1083

`+

resp = smtp.auth("LOGIN", smtp.auth_login, initial_response_ok=True)

`

``

1084

`+

self.assertEqual(resp, (235, b'Authentication Succeeded'))

`

``

1085

+

``

1086

`+

def testAUTH_LOGIN_initial_response_notok(self):

`

``

1087

`+

self.serv.add_feature("AUTH LOGIN")

`

``

1088

`+

with smtplib.SMTP(HOST, self.port, local_hostname='localhost',

`

``

1089

`+

timeout=support.LOOPBACK_TIMEOUT) as smtp:

`

``

1090

`+

smtp.user, smtp.password = sim_auth

`

``

1091

`+

smtp.ehlo("test_auth_login")

`

``

1092

`+

resp = smtp.auth("LOGIN", smtp.auth_login, initial_response_ok=False)

`

``

1093

`+

self.assertEqual(resp, (235, b'Authentication Succeeded'))

`

``

1094

+

``

1095

`+

def testAUTH_BUGGY(self):

`

``

1096

`+

self.serv.add_feature("AUTH BUGGY")

`

``

1097

+

``

1098

`+

def auth_buggy(challenge=None):

`

``

1099

`+

self.assertEqual(b"BuGgYbUgGy", challenge)

`

``

1100

`+

return "\0"

`

``

1101

+

``

1102

`+

smtp = smtplib.SMTP(

`

``

1103

`+

HOST, self.port, local_hostname='localhost',

`

``

1104

`+

timeout=support.LOOPBACK_TIMEOUT

`

``

1105

`+

)

`

``

1106

`+

try:

`

``

1107

`+

smtp.user, smtp.password = sim_auth

`

``

1108

`+

smtp.ehlo("test_auth_buggy")

`

``

1109

`+

expect = r"^Server AUTH mechanism infinite loop.*"

`

``

1110

`+

with self.assertRaisesRegex(smtplib.SMTPException, expect) as cm:

`

``

1111

`+

smtp.auth("BUGGY", auth_buggy, initial_response_ok=False)

`

``

1112

`+

finally:

`

``

1113

`+

smtp.close()

`

``

1114

+

1072

1115

`@hashlib_helper.requires_hashdigest('md5')

`

1073

1116

`def testAUTH_CRAM_MD5(self):

`

1074

1117

`self.serv.add_feature("AUTH CRAM-MD5")

`