Skip to content

Commit b3ac843

Browse files
committed
python#16040: fix unlimited read from connection in nntplib.
1 parent f84422d commit b3ac843

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

Lib/nntplib.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@
8585
"decode_header",
8686
]
8787

88+
# maximal line length when calling readline(). This is to prevent
89+
# reading arbitrary lenght lines. RFC 3977 limits NNTP line length to
90+
# 512 characters, including CRLF. We have selected 2048 just to be on
91+
# the safe side.
92+
_MAXLINE = 2048
93+
94+
8895
# Exceptions raised when an error or invalid response is received
8996
class NNTPError(Exception):
9097
"""Base class for all nntplib exceptions"""
@@ -410,7 +417,9 @@ def _getline(self, strip_crlf=True):
410417
"""Internal: return one line from the server, stripping _CRLF.
411418
Raise EOFError if the connection is closed.
412419
Returns a bytes object."""
413-
line = self.file.readline()
420+
line = self.file.readline(_MAXLINE +1)
421+
if len(line) > _MAXLINE:
422+
raise NNTPDataError('line too long')
414423
if self.debugging > 1:
415424
print('*get*', repr(line))
416425
if not line: raise EOFError

Lib/test/test_nntplib.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,11 @@ def handle_NEWNEWS(self, group, date_str, time_str):
563563
<a4929a40-6328-491a-aaaf-cb79ed7309a2@q2g2000vbk.googlegroups.com>
564564
<f30c0419-f549-4218-848f-d7d0131da931@y3g2000vbm.googlegroups.com>
565565
.""")
566+
elif (group == 'comp.lang.python' and
567+
date_str in ('20100101', '100101') and
568+
time_str == '090000'):
569+
self.push_lit('too long line' * 3000 +
570+
'\n.')
566571
else:
567572
self.push_lit("""\
568573
230 An empty list of newsarticles follows
@@ -1158,6 +1163,11 @@ def test_ihave(self):
11581163
self.assertEqual(cm.exception.response,
11591164
"435 Article not wanted")
11601165

1166+
def test_too_long_lines(self):
1167+
dt = datetime.datetime(2010, 1, 1, 9, 0, 0)
1168+
self.assertRaises(nntplib.NNTPDataError,
1169+
self.server.newnews, "comp.lang.python", dt)
1170+
11611171

11621172
class NNTPv1Tests(NNTPv1v2TestsMixin, MockedNNTPTestsMixin, unittest.TestCase):
11631173
"""Tests an NNTP v1 server (no capabilities)."""

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Core and Builtins
1616
Library
1717
-------
1818

19+
- Issue #16040: CVE-2013-1752: nntplib: Limit maximum line lengths to 2048 to
20+
prevent readline() calls from consuming too much memory. Patch by Jyrki
21+
Pulliainen.
22+
1923
- Issue #16039: CVE-2013-1752: Change use of readline in imaplib module to limit
2024
line length. Patch by Emil Lind.
2125

0 commit comments

Comments
 (0)