diff -r d6a26cd93825 Lib/nntplib.py --- a/Lib/nntplib.py Sat Feb 23 21:51:05 2013 -0500 +++ b/Lib/nntplib.py Thu Oct 24 21:57:44 2013 +0200 @@ -85,6 +85,13 @@ "decode_header", ] +# maximal line length when calling readline(). This is to prevent +# reading arbitrary lenght lines. RFC 3977 limits NNTP line length to +# 512 characters, including CRLF. We have selected 2048 just to be on +# the safe side. +_MAXLINE = 2048 + + # Exceptions raised when an error or invalid response is received class NNTPError(Exception): """Base class for all nntplib exceptions""" @@ -410,7 +417,9 @@ """Internal: return one line from the server, stripping _CRLF. Raise EOFError if the connection is closed. Returns a bytes object.""" - line = self.file.readline() + line = self.file.readline(_MAXLINE +1) + if len(line) > _MAXLINE: + raise NNTPDataError('line too long') if self.debugging > 1: print('*get*', repr(line)) if not line: raise EOFError diff -r d6a26cd93825 Lib/test/test_nntplib.py --- a/Lib/test/test_nntplib.py Sat Feb 23 21:51:05 2013 -0500 +++ b/Lib/test/test_nntplib.py Thu Oct 24 21:57:44 2013 +0200 @@ -563,6 +563,11 @@ .""") + elif (group == 'comp.lang.python' and + date_str in ('20100101', '100101') and + time_str == '090000'): + self.push_lit('too long line' * 3000 + + '\n.') else: self.push_lit("""\ 230 An empty list of newsarticles follows @@ -1158,6 +1163,11 @@ self.assertEqual(cm.exception.response, "435 Article not wanted") + def test_too_long_lines(self): + dt = datetime.datetime(2010, 1, 1, 9, 0, 0) + self.assertRaises(nntplib.NNTPDataError, + self.server.newnews, "comp.lang.python", dt) + class NNTPv1Tests(NNTPv1v2TestsMixin, MockedNNTPTestsMixin, unittest.TestCase): """Tests an NNTP v1 server (no capabilities)."""