-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible bug in smtplib when initial_response_ok=False #72007
Comments
oo |
I have reasons to believe that smtlib.py does not support AUTH LOGIN well. My guts feeling are that the auth_login method should be changed into: def auth_login(self, challenge=None):
print("auth_login", challenge)
""" Authobject to use with LOGIN authentication. Requires self.user and
self.password to be set."""
if challenge is None:
return self.user
elif challenge == b'Username:':
return self.user
elif challenge == b'Password:':
return self.password While the if at line 634, in the auth method, should actually be a while, # If server responds with a challenge, send the response.
if code == 334:
challenge = base64.decodebytes(resp)
response = encode_base64(
authobject(challenge).encode('ascii'), eol='')
(code, resp) = self.docmd(response) is turned into this: # If server responds with a challenge, send the response.
# Note that there may be multiple, sequential challenges.
while code == 334:
challenge = base64.decodebytes(resp)
response = encode_base64(
authobject(challenge).encode('ascii'), eol='')
(code, resp) = self.docmd(response) First, some background on AUTH LOGIN; based on my understanding of Method A Method B The second method saves two round trips because the client sends In the following I will detail my experience with smtplib. Everything begun from this code fragment: smtpObj = smtplib.SMTP("smtp.example.com", "25")
smtpObj.set_debuglevel(2)
smtpObj.login("noreply@example.com", "chocolaterain")
smtpObj.sendmail(sender, receivers, message) The debug log produced by smtplib looked like this: 01:53:32.420185 send: 'ehlo localhost.localdomain\r\n'
01:53:32.624123 reply: b'250-smtp.example.com\r\n'
01:53:32.862965 reply: b'250-AUTH LOGIN\r\n'
01:53:32.863490 reply: b'250 8BITMIME\r\n'
01:53:32.863844 reply: retcode (250); Msg: b'smtp.example.com\nAUTH LOGIN\n8BITMIME'
01:53:32.868414 send: 'AUTH LOGIN <<<ENCODED_USERNAME>>>\r\n'
01:53:33.069884 reply: b'501 syntax error\r\n'
01:53:33.070479 reply: retcode (501); Msg: b'syntax error'
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/dario/Programming/DigitalOcean/s.py", line 48, in <module>
smtpObj.login("noreply@example.com", "chocolaterain")
File "/usr/lib/python3.5/smtplib.py", line 729, in login
raise last_exception
File "/usr/lib/python3.5/smtplib.py", line 720, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.5/smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (501, b'syntax error') This is most likely not an issue with smtplib, but simply an I figured out that I could force the alternate data flow (method A), in which smtpObj = smtplib.SMTP("smtp.example.com", "25")
smtpObj.set_debuglevel(2)
smtpObj.login("noreply@example.com", "chocolaterain", initial_response_ok=False)
smtpObj.sendmail(sender, receivers, message) This resulted in a slightly more interesting behaviour:
01:53:54.445118 send: 'ehlo localhost.localdomain\r\n'
01:53:54.648136 reply: b'250-smtp.example.com\r\n'
01:53:54.884669 reply: b'250-AUTH LOGIN\r\n'
01:53:54.885197 reply: b'250 8BITMIME\r\n'
01:53:54.885555 reply: retcode (250); Msg: b'smtp.example.com\nAUTH LOGIN\n8BITMIME'
01:53:54.890051 send: 'AUTH LOGIN\r\n'
01:53:55.089540 reply: b'334 VXNlcm5hbWU6\r\n'
01:53:55.090119 reply: retcode (334); Msg: b'VXNlcm5hbWU6'
01:53:55.090955 send: '<<<ENCODED_PASSWORD>>>=\r\n'
01:53:55.296243 reply: b'334 UGFzc3dvcmQ6\r\n'
01:53:55.296717 reply: retcode (334); Msg: b'UGFzc3dvcmQ6'
Traceback (most recent call last):
File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/dario/Programming/DigitalOcean/s.py", line 57, in <module>
smtpObj.login("noreply@example.com", "16226464", initial_response_ok=False)
File "/usr/lib/python3.5/smtplib.py", line 729, in login
raise last_exception
File "/usr/lib/python3.5/smtplib.py", line 720, in login
initial_response_ok=initial_response_ok)
File "/usr/lib/python3.5/smtplib.py", line 641, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (334, b'UGFzc3dvcmQ6') The above log shows two issues:
Due to the way this method is written, the final (code, resp), The fix is in two part:
# If server responds with a challenge, send the response.
# Note that there may be multiple, sequential challenges.
while code == 334:
challenge = base64.decodebytes(resp)
response = encode_base64(
authobject(challenge).encode('ascii'), eol='')
(code, resp) = self.docmd(response) With the two changes discussed above login works also for servers 01:54:42.256276 send: 'ehlo localhost.localdomain\r\n' |
Yes, this (or something similar) totally bit me, when for another unrelated reason 'AUTH PLAIN' authentication failed: https://gist.github.com/macolo/bf2811c14d985d013dda0741bfd339e0 Python then tries auth_login, but doesn't send 'AUTH LOGIN' to the mail server. The second auth method also fails. |
I encountered the same issue , Dario D'Amico's changing works ! please fix the problem ! |
Hi, I'm one of the maintainers of aio-libs/aiosmtpd. This issue also bit me when trying to write unit tests for aio-libs/aiosmtpd AUTH implementation But I partially disagree with Dario D'Amico's changes, specifically the suggested change in the auth_login() method. According to draft-murchison-sasl-login-00.txt [1], the two challenges sent by the server SHOULD be ignored. The example in that document uses b"VXNlciBOYW1lAA==" and b"UGFzc3dvcmQA" (b64 of b"User Name\x00" and b"Password\x00", respectively), and this is what we have implemented in aio-libs/aiosmtpd. Furthermore, the same document never indicated that username may be sent along with "AUTH LOGIN", so we haven't implemented that in aio-libs/aiosmtpd. So rather than hardcoding the challenges to b"Username:" and b"Password:", a compliant SMTP client must instead _count_ the number of challenges it received. I propose the following changes instead: def auth(self, mechanism, authobject, *, initial_response_ok=True):
... snip ...
if initial_response is not None:
response = encode_base64(initial_response.encode('ascii'), eol='')
(code, resp) = self.docmd("AUTH", mechanism + " " + response)
self._challenge_count = 1
else:
(code, resp) = self.docmd("AUTH", mechanism)
self._challenge_count = 0
# If server responds with a challenge, send the response.
while code == 334:
self._challenge_count += 1
challenge = base64.decodebytes(resp)
... snip ...
def auth_login(self, challenge=None):
""" Authobject to use with LOGIN authentication. Requires self.user and
self.password to be set."""
if challenge is None or self._challenge_count < 2:
return self.user
else:
return self.password [1] https://www.ietf.org/archive/id/draft-murchison-sasl-login-00.txt |
This issue is still a bug for Python 3.6 and Python 3.8 I haven't checked on Python 3.7 and Python 3.9 |
I tried creating a PR, but for the life of me I couldn't wrap my head around how testAUTH_LOGIN is being performed (it's in Lib/test/test_smtplib.py) All I know is, the test doesn't AT ALL test for situations where initial_response_ok=False. ALL tests are done with initial_response_ok=True. There needs to be a whole set of additions to test_smtplib.py |
I tried adding the code below to test_smtplib.py: def testAUTH_LOGIN_initial_response_notok(self):
self.serv.add_feature("AUTH LOGIN")
smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost',
timeout=support.LOOPBACK_TIMEOUT)
resp = smtp.login(sim_auth[0], sim_auth[1], initial_response_ok=False)
self.assertEqual(resp, (235, b'Authentication Succeeded'))
smtp.close() and I ended up with: ====================================================================== Traceback (most recent call last):
File "/home/pepoluan/projects/cpython/Lib/test/test_smtplib.py", line 1065, in testAUTH_LOGIN_initial_response_notok
resp = smtp.login(sim_auth[0], sim_auth[1], initial_response_ok=False)
File "/home/pepoluan/projects/cpython/Lib/smtplib.py", line 738, in login
raise last_exception
File "/home/pepoluan/projects/cpython/Lib/smtplib.py", line 727, in login
(code, resp) = self.auth(
File "/home/pepoluan/projects/cpython/Lib/smtplib.py", line 650, in auth
raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (451, b'Internal confusion') |
Okay, I finally figured out what's wrong. This piece of code in if self.smtp_state == self.AUTH:
line = self._emptystring.join(self.received_lines)
print('Data:', repr(line), file=smtpd.DEBUGSTREAM)
self.received_lines = []
try:
self.auth_object(line)
except ResponseException as e:
self.smtp_state = self.COMMAND
self.push('%s %s' % (e.smtp_code, e.smtp_error))
return The last "return" is over-indented. |
PR available on GitHub and it's already more than one month since the PR was submitted, so I'm pinging this issue. |
Hello Pandu, Thank you for this patch and the explanation. Does client blocking on repeated challenge from the server (using of while loop) look okay here? Thanks, |
Hi Senthil, You're right, it does need a guard. According to my knowledge there is no AUTH mechanism that will send more than 3 challenges; they should fail afterwards with 535 or similar. Servers that don't do that should be considered buggy/broken. So I've pushed a commit to the GH PR that limits the challenge to 5 times, after which it will raise SMTPException. This will protect users of smtplib.SMTP from being trapped by a buggy/broken server. Rgds, |
Hi Traceback (most recent call last):
File "/usr/lib/python3.6/smtplib.py", line 644, in auth
challenge = base64.decodebytes(resp)
File "/usr/lib/python3.6/base64.py", line 553, in decodebytes
return binascii.a2b_base64(s)
binascii.Error: Incorrect padding I think this fit the title "a bug in smtplib when initial_response_ok=False", should I just comment on this issue or open a new issue? |
Please open a new issue. It has better chances of being fixed quickly. On Mon, Apr 26, 2021 at 10:02 PM junpengruan <report@bugs.python.org> wrote:
> and it's not base64 encoding, while in the auth() it will decode the
> resp(here is "ok, go on") which will cause a binascii.Error:
>
> Traceback (most recent call last):
> File "/usr/lib/python3.6/smtplib.py", line 644, in auth
> challenge = base64.decodebytes(resp)
> File "/usr/lib/python3.6/base64.py", line 553, in decodebytes
> return binascii.a2b_base64(s)
> binascii.Error: Incorrect padding
>
> I think this fit the title "a bug in smtplib when
> initial_response_ok=False", should I just comment on this issue or open a
> new issue?
> Thanks!
>
>
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: