From: Menno Smits Date: Tue, 15 Dec 2015 10:39:22 +0000 (+1300) Subject: Fix capabilities handling around STARTTLS X-Git-Tag: 1.0.1~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ff24269da1a2627585209bbda182f61d95007e78;p=imapclient.git Fix capabilities handling around STARTTLS Fresh capabilities weren't being retrieved after STARTTLS but before authentication. This has been fixed and now all STARTTLS/capabilities handling is addressed within capabilities(). Fixes #195. --HG-- branch : stable --- diff --git a/doc/src/releases.rst b/doc/src/releases.rst index 19b9d36..1d0cb6b 100644 --- a/doc/src/releases.rst +++ b/doc/src/releases.rst @@ -1,5 +1,21 @@ :tocdepth: 1 +=============== + Version 1.0.1 +=============== + +Changed +------- +- Minimum backports.ssl dependency is now 0.0.9 (an important + performance issue was addressed) +- setuptools 18.8.1 now used due to strange zip file error for 17.1 + +Fixed +----- +- Unit test for version strings were updated to now always include the + patch version. + + =============== Version 1.0.0 =============== diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 649bae9..43f0e6b 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -200,10 +200,6 @@ class IMAPClient(object): self._starttls_done = True - # Clear cached capabilities as per - # https://tools.ietf.org/html/rfc2595#section-3.1 - self._cached_capabilities = None - self._imap.sock = tls.wrap_socket(self._imap.sock, ssl_context, self.host) self._imap.file = self._imap.sock.makefile() return data[0] @@ -286,14 +282,21 @@ class IMAPClient(object): def capabilities(self): """Returns the server capability list. - If the session is authenticated and the server has returned a - CAPABILITY response at authentication time, this response - will be returned. Otherwise, the CAPABILITY command will be - issued to the server, with the results cached for future calls. + If the session is authenticated and the server has returned an + untagged CAPABILITY response at authentication time, this + response will be returned. Otherwise, the CAPABILITY command + will be issued to the server, with the results cached for + future calls. - If the session is not yet authenticated, the cached - capabilities determined at connection time will be returned. + If the session is not yet authenticated, the capabilities + requested at connection time will be returned. """ + # Ensure cached capabilities aren't used post-STARTTLS. As per + # https://tools.ietf.org/html/rfc2595#section-3.1 + if self._starttls_done and self._imap.state == 'NONAUTH': + self._cached_capabilities = None + return self._do_capabilites() + # If a capability response has been cached, use that. if self._cached_capabilities: return self._cached_capabilities @@ -303,21 +306,25 @@ class IMAPClient(object): untagged = _dict_bytes_normaliser(self._imap.untagged_responses) response = untagged.pop(b'CAPABILITY', None) if response: - return self._save_capabilities(response[0]) + self._cached_capabilities = self._normalise_capabilites(response[0]) + return self._cached_capabilities # If authenticated, but don't have a capability reponse, ask for one if self._imap.state in ('SELECTED', 'AUTH'): - response = self._command_and_check('capability', unpack=True) - return self._save_capabilities(response) + self._cached_capabilities = self._do_capabilites() + return self._cached_capabilities - # Just return capabilities that imaplib grabbed at connection + # Return capabilities that imaplib requested at connection # time (pre-auth) return tuple(to_bytes(c) for c in self._imap.capabilities) - def _save_capabilities(self, raw_response): + def _do_capabilites(self): + raw_response = self._command_and_check('capability', unpack=True) + return self._normalise_capabilites(raw_response) + + def _normalise_capabilites(self, raw_response): raw_response = to_bytes(raw_response) - self._cached_capabilities = tuple(raw_response.upper().split()) - return self._cached_capabilities + return tuple(raw_response.upper().split()) def has_capability(self, capability): """Return ``True`` if the IMAP server has the given *capability*. diff --git a/imapclient/test/test_imapclient.py b/imapclient/test/test_imapclient.py index 1bf71e2..70c3dd2 100644 --- a/imapclient/test/test_imapclient.py +++ b/imapclient/test/test_imapclient.py @@ -495,6 +495,23 @@ class TestCapabilities(IMAPClientTest): self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR')) self.assertEqual(self.client._cached_capabilities, (b'FOO', b'BAR')) + def test_with_starttls(self): + # Initial connection + self.client._imap.capabilities = ('FOO',) + self.client._imap.untagged_responses = {} + self.client._imap.state = 'NONAUTH' + self.assertEqual(self.client.capabilities(), (b'FOO',)) + + # Now do STARTTLS; capabilities change and should be reported. + self.client._starttls_done = True + self.client._imap.capability.return_value = ('OK', [b'FOO BAR']) + self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR')) + + # Login done; capabilities change again. + self.client._imap.state = 'AUTH' + self.client._imap.capability.return_value = ('OK', [b'FOO BAR QUX']) + self.assertEqual(self.client.capabilities(), (b'FOO', b'BAR', b'QUX')) + def test_has_capability(self): self.client._cached_capabilities = (b'FOO', b'MORE') diff --git a/imapclient/test/test_starttls.py b/imapclient/test/test_starttls.py index 5cce708..7f5aef4 100644 --- a/imapclient/test/test_starttls.py +++ b/imapclient/test/test_starttls.py @@ -42,19 +42,6 @@ class TestStarttls(IMAPClientTest): self.assertEqual(self.client._imap.file, sentinel.file) self.assertEqual(resp, b'start TLS negotiation') - def test_clears_cached_capabilities(self): - # As per https://tools.ietf.org/html/rfc2595#section-3.1 - - self.client._save_capabilities(b"FOO BAR") - self.assertEqual(self.client.capabilities(), (b"FOO", b"BAR")) - - self.client.starttls(sentinel.ssl_context) - - # Now verify that the cache has been cleared and new - # capabilities are read. - self.client._imap.untagged_responses = {'CAPABILITY': [b'META']} - self.assertEqual(self.client.capabilities(), (b"META",)) - def test_command_fails(self): self.client._imap._simple_command.return_value = "NO", [b'sorry'] diff --git a/imapclient/test/testable_imapclient.py b/imapclient/test/testable_imapclient.py index f6d7161..90dff93 100644 --- a/imapclient/test/testable_imapclient.py +++ b/imapclient/test/testable_imapclient.py @@ -25,6 +25,7 @@ class MockIMAP4(Mock): self.sent = b'' # Accumulates what was given to send() self.tagged_commands = {} self.debug = 0 + self._starttls_done = False def send(self, data): self.sent += data