This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: smtplib SASL PLAIN authentication error
Type: behavior Stage: resolved
Components: Extension Modules Versions: Python 2.6, Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: exarkun, surkamp
Priority: normal Keywords:

Created on 2010-01-25 20:52 by surkamp, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg98295 - (view) Author: Sérgio Surkamp (surkamp) Date: 2010-01-25 20:52
There is bug in PLAIN mechanism's of smtplib. The generated base64 string fail when the password start with numbers. As long as I could find, the error occur in method encode_plain. Using the null character (\0) in hexadecimal representation (\x00) seems to fix the problem.

Origin of the problem:

        def encode_plain(user, password):
            return encode_base64("\0%s\0%s" % (user, password), eol="")

Proposed fix:

        def encode_plain(user, password):
            return encode_base64("\x00%s\x00%s" % (user, password), eol="")

Current result:
>>> from email.base64mime import encode as encode_base64
>>> import base64
>>> encode_base64("\0user\0123foo", eol="")
'AHVzZXIKM2Zvbw=='
>>> f = base64.decodestring('AHVzZXIKM2Zvbw==')
>>> f
'\x00user\n3foo'

Expected result:
>>> from email.base64mime import encode as encode_base64
>>> import base64
>>> encode_base64("\x00user\x00123foo", eol="")
'AHVzZXIAMTIzZm9v'
>>> f = base64.decodestring('AHVzZXIAMTIzZm9v')
>>> f
'\x00user\x00123foo'
msg98296 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-01-25 20:55
There's no bug here.  You've misunderstood how the literal \0 syntax works.  Perhaps this will clarify things:

  >>> list('\0123')
  ['\n', '3']
  >>> list('\x00123')
  ['\x00', '1', '2', '3']
  >>>
msg98297 - (view) Author: Sérgio Surkamp (surkamp) Date: 2010-01-25 21:05
The SASL protocol says that the encoded base64 should be formed from:

null + login + null + password

The smtplib is not doing it, instead its "converting" the \012 (\0 + 2 first chars from password) in the char "\n", and it's right in the python way to see the things, it's not the bug reported here. The bug is a patch to change the null character representation from \0 to \x00 (using the hexadecimal representation) in the encode_base64 call to prevent the "conversion" and generate the right base64 encoded string.
msg98298 - (view) Author: Jean-Paul Calderone (exarkun) * (Python committer) Date: 2010-01-25 21:06
It doesn't matter whether \0 or \x00 is used.  They mean the same thing.  Maybe this is the example I should have given:

  >>> list('\0%s' % ('12',))
  ['\x00', '1', '2']
  >>> list('\x00%s' % ('12',))
  ['\x00', '1', '2']
  >>>
msg98300 - (view) Author: Sérgio Surkamp (surkamp) Date: 2010-01-25 21:22
Got your point. Sorry.
History
Date User Action Args
2022-04-11 14:56:56adminsetgithub: 52027
2010-01-25 21:22:21surkampsetmessages: + msg98300
2010-01-25 21:16:19ezio.melottisetstatus: open -> closed
2010-01-25 21:06:19exarkunsetmessages: + msg98298
2010-01-25 21:05:05surkampsetstatus: closed -> open

messages: + msg98297
2010-01-25 20:57:29ezio.melottisettype: behavior
stage: resolved
2010-01-25 20:56:28benjamin.petersonsetstatus: open -> closed
resolution: not a bug
2010-01-25 20:55:08exarkunsetnosy: + exarkun
messages: + msg98296
2010-01-25 20:52:57surkampcreate