jazzband / imaplib2

Fork of Piers Lauder's imaplib2 library for Python.
https://imaplib2.readthedocs.io/
MIT License
34 stars 29 forks source link

Crash with debug in tunneled connection #10

Closed thekix closed 3 years ago

thekix commented 3 years ago

There is a bug in imaplib2 using IMAP4 tunnel. If we set the debug mode, then we get a crash here:

    if __debug__:
      if debug:
        self._mesg('connected to %s on port %s' % (self.host, self.port))

The reason is because self.port is set to None in a tunneled connection, then it raises an error. We need remove the debug mode for this connection type to avoid the crash.

The right code in imaplib2 should be:

    if __debug__:
      if debug & self.port != None:
        self._mesg('connected to %s on port %s' % (self.host, self.port))
nicolas33 commented 3 years ago

I don't get the point. The code "%s %s"% (None, None) works fine on both python3 and python2.

thekix commented 3 years ago

Hi @nicolas33

I was working in this issue. There is a problem in the code because the variables self.host and self.port are not set. Only the variables port and host exists.

If you set the debug argument (I included that in one patch in offlineimap3, file imapserver.py, line 551):

imapobj = imaplibutil.IMAP4_Tunnel(
    self.tunnel,
    timeout=socket.getdefaulttimeout(),
    debug=imap_debug,
    use_socket=self.proxied_socket,
)

Then imaplib2 will crash, the problem is with getattr ():

 *** Finished account 'Test' in 0:00
INFO:OfflineImap:*** Finished account 'Test' in 0:00
 ERROR: Exceptions occurred during the run!
WARNING:OfflineImap:ERROR: Exceptions occurred during the run!
 ERROR: While attempting to sync account 'Test'
  Unknown IMAP4 command: 'port'
WARNING:OfflineImap:ERROR: While attempting to sync account 'Test'
  Unknown IMAP4 command: 'port'

WARNING:OfflineImap:
Traceback:
  File "/home/kix/src/offlineimap3/offlineimap/accounts.py", line 298, in syncrunner
    self.__sync()
  File "/home/kix/src/offlineimap3/offlineimap/accounts.py", line 374, in __sync
    remoterepos.getfolders()
  File "/home/kix/src/offlineimap3/offlineimap/repository/IMAP.py", line 648, in getfolders
    imapobj = self.imapserver.acquireconnection()
  File "/home/kix/src/offlineimap3/offlineimap/imapserver.py", line 551, in acquireconnection
    imapobj = imaplibutil.IMAP4_Tunnel(
  File "/home/kix/src/offlineimap3/offlineimap/imaplibutil.py", line 125, in __init__
    IMAP4.__init__(self, tunnelcmd, **kwargs)
  File "/usr/lib/python3/dist-packages/imaplib2.py", line 340, in __init__
    self._mesg('connected to %s on port %s' % (self.host, self.port))
  File "/usr/lib/python3/dist-packages/imaplib2.py", line 404, in __getattr__
    raise AttributeError("Unknown IMAP4 command: '%s'" % attr)

This problem is only in tunneled connections, because the self.port is not set.

We can change the code in this way:

        if __debug__:
            if debug:
                self._mesg('connected to %s on port %s' % (host, port))

Therefore, the patch is:

index 0aeff4d..be86cea 100755
--- a/imaplib2/imaplib2.py3
+++ b/imaplib2/imaplib2.py3
@@ -337,7 +337,7 @@ class IMAP4(object):

         if __debug__:
             if debug:
-                self._mesg('connected to %s on port %s' % (self.host, self.port))
+                self._mesg('connected to %s on port %s' % (host, port))

         # Threading

I will provide this patch.

Regards, kix