Skip to content

Commit c099aa1

Browse files
[3.13] gh-63121: Refresh imaplib capabilities on state changes (GH-152752) (GH-152865)
imaplib fetched the server capabilities only once, at connection time. They are now also refreshed after a successful LOGIN or AUTHENTICATE, from the CAPABILITY response the server sent or, if it sent none, by querying it. This lets methods such as enable() see capabilities added after login, for example ENABLE on Gmail (gh-103451). Capabilities advertised in the server greeting are now used too, saving a redundant CAPABILITY command. (cherry picked from commit c89b72a) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7933f4b commit c099aa1

3 files changed

Lines changed: 87 additions & 2 deletions

File tree

Lib/imaplib.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def _connect(self):
259259
self._encoding, 'replace')
260260
raise self.error('invalid greeting: ' + greeting)
261261

262-
self._get_capabilities()
262+
self._refresh_capabilities()
263263
if __debug__:
264264
if self.debug >= 3:
265265
self._mesg('CAPABILITIES: %r' % (self.capabilities,))
@@ -454,6 +454,7 @@ def authenticate(self, mechanism, authobject):
454454
if typ != 'OK':
455455
raise self.error(dat[-1].decode('utf-8', 'replace'))
456456
self.state = 'AUTH'
457+
self._refresh_capabilities()
457458
return typ, dat
458459

459460

@@ -622,6 +623,7 @@ def login(self, user, password):
622623
if typ != 'OK':
623624
raise self.error(dat[-1].decode('UTF-8', 'replace'))
624625
self.state = 'AUTH'
626+
self._refresh_capabilities()
625627
return typ, dat
626628

627629

@@ -1088,6 +1090,15 @@ def _get_capabilities(self):
10881090
self.capabilities = tuple(dat.split())
10891091

10901092

1093+
def _refresh_capabilities(self):
1094+
# Use a CAPABILITY response sent by the server, or ask for it.
1095+
if 'CAPABILITY' in self.untagged_responses:
1096+
dat = self.untagged_responses.pop('CAPABILITY')[-1]
1097+
self.capabilities = tuple(str(dat, self._encoding).upper().split())
1098+
else:
1099+
self._get_capabilities()
1100+
1101+
10911102
def _get_response(self):
10921103

10931104
# Read response and store.

Lib/test/test_imaplib.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ def _send_textline(self, message):
221221
def _send_tagged(self, tag, code, message):
222222
self._send_textline(' '.join((tag, code, message)))
223223

224+
welcome = '* OK IMAP4rev1'
225+
224226
def handle(self):
225227
# Send a welcome message.
226-
self._send_textline('* OK IMAP4rev1')
228+
self._send_textline(self.welcome)
227229
while 1:
228230
# Gather up input until we receive a line terminator or we timeout.
229231
# Accumulate read(1) because it's simpler to handle the differences
@@ -659,6 +661,71 @@ def cmd_APPEND(self, tag, args):
659661
client.append('INBOX', None, None, message)
660662
self.assertEqual(server.response, b'a\r\nb\r\nc\r\nd')
661663

664+
def test_login_capabilities(self):
665+
# A server may advertise new capabilities after login (as an
666+
# untagged CAPABILITY response); imaplib must refresh its cached
667+
# capability list (gh-63121, gh-103451).
668+
class CapabilityLoginHandler(SimpleIMAPHandler):
669+
def cmd_LOGIN(self, tag, args):
670+
self.server.logged = args[0]
671+
self._send_textline('* CAPABILITY IMAP4rev1 ENABLE UTF8=ACCEPT')
672+
self._send_tagged(tag, 'OK', 'LOGIN completed')
673+
def cmd_ENABLE(self, tag, args):
674+
self._send_tagged(tag, 'OK', 'ENABLE completed')
675+
676+
client, _ = self._setup(CapabilityLoginHandler)
677+
self.assertNotIn('ENABLE', client.capabilities)
678+
client.login('user', 'pass')
679+
self.assertIn('ENABLE', client.capabilities)
680+
self.assertIn('UTF8=ACCEPT', client.capabilities)
681+
typ, _ = client.enable('UTF8=ACCEPT')
682+
self.assertEqual(typ, 'OK')
683+
684+
def test_authenticate_capabilities(self):
685+
# Capabilities are also refreshed after AUTHENTICATE, here from a
686+
# CAPABILITY response code in the tagged OK response.
687+
class CapabilityAuthHandler(SimpleIMAPHandler):
688+
def cmd_AUTHENTICATE(self, tag, args):
689+
self._send_textline('+')
690+
self.server.response = yield
691+
self._send_tagged(
692+
tag, 'OK',
693+
'[CAPABILITY IMAP4rev1 ENABLE] AUTHENTICATE completed')
694+
695+
client, _ = self._setup(CapabilityAuthHandler)
696+
self.assertNotIn('ENABLE', client.capabilities)
697+
client.authenticate('MYAUTH', lambda x: b'fake')
698+
self.assertIn('ENABLE', client.capabilities)
699+
700+
def test_greeting_capabilities(self):
701+
# Capabilities advertised in the greeting are used directly,
702+
# without sending a separate CAPABILITY command.
703+
class GreetingHandler(SimpleIMAPHandler):
704+
welcome = '* OK [CAPABILITY IMAP4rev1 ENABLE] Server ready'
705+
def cmd_CAPABILITY(self, tag, args):
706+
self.server.capability_queried = True
707+
super().cmd_CAPABILITY(tag, args)
708+
709+
client, server = self._setup(GreetingHandler)
710+
self.assertEqual(client.capabilities, ('IMAP4REV1', 'ENABLE'))
711+
self.assertFalse(getattr(server, 'capability_queried', False))
712+
713+
def test_login_requery_capabilities(self):
714+
# If the server does not advertise capabilities after login,
715+
# imaplib re-queries them (as it does after STARTTLS), so a
716+
# capability that becomes available only after authentication is
717+
# still recognized (gh-63121).
718+
class RequeryHandler(SimpleIMAPHandler):
719+
def cmd_CAPABILITY(self, tag, args):
720+
caps = 'IMAP4rev1 ENABLE' if self.server.logged else 'IMAP4rev1'
721+
self._send_textline('* CAPABILITY ' + caps)
722+
self._send_tagged(tag, 'OK', 'CAPABILITY completed')
723+
724+
client, _ = self._setup(RequeryHandler)
725+
self.assertNotIn('ENABLE', client.capabilities)
726+
client.login('user', 'pass')
727+
self.assertIn('ENABLE', client.capabilities)
728+
662729
def test_logout(self):
663730
client, _ = self._setup(SimpleIMAPHandler)
664731
typ, data = client.login('user', 'pass')
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:mod:`imaplib` now refreshes the cached capability list after a successful
2+
:meth:`~imaplib.IMAP4.login` or :meth:`~imaplib.IMAP4.authenticate`, using
3+
the ``CAPABILITY`` response sent by the server or, if none was sent, by
4+
querying it, so that capabilities that become available only after
5+
authentication (such as ``ENABLE`` on Gmail) are recognized. Capabilities
6+
advertised in the server greeting are now also used, avoiding a redundant
7+
``CAPABILITY`` command.

0 commit comments

Comments
 (0)