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: imaplib Cyrillic password
Type: behavior Stage: resolved
Components: Library (Lib), Unicode Versions: Python 2.7
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Nikita Velykanov, christian.heimes, ezio.melotti, vstinner
Priority: normal Keywords:

Created on 2018-09-04 14:11 by Nikita Velykanov, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg324586 - (view) Author: Nikita Velykanov (Nikita Velykanov) Date: 2018-09-04 14:11
Let's consider there is an email box with password which contains Cyrillic symbols. When loging in

  imaplib.IMAP4_SSL(host, port).login(login, password)

password field is passed as "кириллица" (type is str, not unicode).
Then, I get this traceback:

  File "/usr/lib64/python2.7/imaplib.py", line 518, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib64/python2.7/imaplib.py", line 1083, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python2.7/imaplib.py", line 870, in _command
    self.send('%s%s' % (data, CRLF))
  File "/usr/lib64/python2.7/imaplib.py", line 1191, in send
    sent = self.sslobj.write(data)
  File "/usr/lib64/python2.7/ssl.py", line 669, in write
    return self._sslobj.write(data)

This is because self._sslobj.write method requires unicode string.

In other case, if password field is passed as u"кириллица" (type unicode, not str), I get another traceback:

  File "/usr/lib64/python2.7/imaplib.py", line 518, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib64/python2.7/imaplib.py", line 1083, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python2.7/imaplib.py", line 852, in _command
    data = '%s %s' % (data, self._checkquote(arg))

It's because '%s' % u"some unicode string" doesn't work.

I guess the problem was described in details.
The problem is that two different libraries are using different string types but they must be agreed.
msg324587 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-09-04 14:27
You have cut off the exception message. Please provide a full traceback including the exception name and message.

Password algorithms typically handle only bytes. The encoding depends on your target system and is usually utf-8.
msg324592 - (view) Author: Nikita Velykanov (Nikita Velykanov) Date: 2018-09-04 15:29
Thank you for fast reply.
Here's full traceback for first case:

Traceback (most recent call last):
  File "some_my_file.py", line 10, in some_function
    self.mail.login(login, password)
  File "/usr/lib64/python2.7/imaplib.py", line 518, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib64/python2.7/imaplib.py", line 1083, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python2.7/imaplib.py", line 870, in _command
    self.send('%s%s' % (data, CRLF))
  File "/usr/lib64/python2.7/imaplib.py", line 1191, in send
    sent = self.sslobj.write(data)
  File "/usr/lib64/python2.7/ssl.py", line 669, in write
    return self._sslobj.write(data)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 30-39: ordinal not in range(128)

Full traceback for second case:

Traceback (most recent call last):
  File "some_my_file.py", line 10, in some_function
    self.mail.login(login, password)
  File "/usr/lib64/python2.7/imaplib.py", line 518, in login
    typ, dat = self._simple_command('LOGIN', user, self._quote(password))
  File "/usr/lib64/python2.7/imaplib.py", line 1083, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib64/python2.7/imaplib.py", line 852, in _command
    data = '%s %s' % (data, self._checkquote(arg))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 1: ordinal not in range(128)


But okay, even if encoding depends on target system and it's usually utf-8 so why string operations in imaplib are not in usual format but in some other?
msg324598 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-09-04 19:05
This is not a bug in Python's imaplib but a limitation of the LDAP protocol. It's not possible to enable UTF-8 mode before the session is authenticatd. LOGIN supports only ASCII user name and password. You have to use SASL PLAIN handshake with AUTHENTICATE, see https://tools.ietf.org/html/rfc6855.html#page-5
msg324599 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2018-09-04 19:05
This is not a bug in Python's imaplib but a limitation of the LDAP protocol. It's not possible to enable UTF-8 mode before the session is authenticatd. LOGIN supports only ASCII user name and password. You have to use SASL PLAIN handshake with AUTHENTICATE, see https://tools.ietf.org/html/rfc6855.html#page-5
msg324631 - (view) Author: Nikita Velykanov (Nikita Velykanov) Date: 2018-09-05 12:48
It works. Thanks a lot!
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78758
2018-09-05 12:48:23Nikita Velykanovsetstatus: open -> closed

messages: + msg324631
stage: resolved
2018-09-04 19:05:55christian.heimessetstatus: open

messages: + msg324599
2018-09-04 19:05:47christian.heimessetstatus: open -> (no value)

messages: + msg324598
2018-09-04 15:29:30Nikita Velykanovsetmessages: + msg324592
2018-09-04 14:27:14christian.heimessetnosy: + christian.heimes
messages: + msg324587
2018-09-04 14:11:45Nikita Velykanovcreate