[3.13] gh-119511: Fix a potential denial of service in imaplib (GH-119514) (GH-129355)

gh-119511: Fix a potential denial of service in imaplib (GH-119514)

The IMAP4 client could consume an arbitrary amount of memory when trying
to connect to a malicious server, because it read a "literal" data with a
single read(size) call, and BufferedReader.read() allocates the bytes
object of the specified size before reading. Now the IMAP4 client reads data
by chunks, therefore the amount of used memory is limited by the
amount of the data actually been sent by the server.
(cherry picked from commit 735f25c5e3)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
Miss Islington (bot) 2025-01-27 23:05:59 +01:00 committed by GitHub
parent 247c3b2644
commit 5829f7bf70
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 1 deletions

View file

@ -901,6 +901,20 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address)
def test_truncated_large_literal(self):
size = 0
class BadHandler(SimpleIMAPHandler):
def handle(self):
self._send_textline('* OK {%d}' % size)
self._send_textline('IMAP4rev1')
for exponent in range(15, 64):
size = 1 << exponent
with self.subTest(f"size=2e{size}"):
with self.reaped_server(BadHandler) as server:
with self.assertRaises(imaplib.IMAP4.abort):
self.imap_class(*server.server_address)
@threading_helper.reap_threads
def test_simple_with_statement(self):
# simplest call