Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Lib/imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1408,8 +1408,11 @@ def _check_bye(self):

def _command(self, name, *args):

if self.state not in Commands[name]:
literal = self.literal
if literal is not None:
self.literal = None

if self.state not in Commands[name]:
raise self.error("command %s illegal in state %s, "
"only allowed in states %s" %
(name, self.state,
Expand All @@ -1434,9 +1437,7 @@ def _command(self, name, *args):
raise ValueError("NUL, CR and LF not allowed in commands")
data = data + b' ' + arg

literal = self.literal
if literal is not None:
self.literal = None
if type(literal) is type(self._command):
literator = literal
else:
Expand Down
22 changes: 22 additions & 0 deletions Lib/test/test_imaplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2326,6 +2326,28 @@ def test_control_characters(self):
self.assertEqual(name.encode('ascii').decode('utf-7-imap'),
f'a{c}b')

def test_control_characters_append_literal(self):
client, server = self._setup(SimpleIMAPHandler)
client.login('user', 'pass')
for bad_mailbox in (b'a\nb', b'a\rb', b'a\0b'):
with self.subTest(mailbox=bad_mailbox):
with self.assertRaises(ValueError):
client.append(bad_mailbox, None, None, b'message')
self.assertIsNone(client.literal)
typ, _ = client.select('INBOX')
self.assertEqual(typ, 'OK')
with self.subTest(case='flags'):
with self.assertRaises(ValueError):
client.append('INBOX', '(\\Flag\n)', None, b'message')
self.assertIsNone(client.literal)
typ, _ = client.select('INBOX')
self.assertEqual(typ, 'OK')
client.logout()
with self.subTest(case='illegal_state'):
with self.assertRaises(imaplib.IMAP4.error):
client.append('INBOX', None, None, b'message')
self.assertIsNone(client.literal)

def test_mutf7_mailbox_name(self):
# Without UTF8=ACCEPT a non-ASCII mailbox name is sent as modified
# UTF-7 (RFC 3501, 5.1.3). A name that is already modified UTF-7 (e.g.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix an issue in :mod:`imaplib` where a rejected :meth:`~imaplib.IMAP4.append`
command left its message literal pending, causing it to be sent with the
subsequent command.
Loading