Skip to content
9 changes: 8 additions & 1 deletion Lib/ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,14 @@ def parse227(resp):
global _227_re
if _227_re is None:
import re
_227_re = re.compile(r'(\d+),(\d+),(\d+),(\d+),(\d+),(\d+)', re.ASCII)
# Each field is an unsigned 8-bit value (0..255), so at most three
# digits. The leading '\b' anchors the first field so that a longer
# run of digits is rejected instead of being silently truncated, and
# bounding every field to three digits keeps re.search() linear in the
# length of the reply (an unbounded r'\d+' made a crafted reply parse
# in quadratic time, gh-162867).
field = r'(\d{1,3})'
_227_re = re.compile(r'\b' + ','.join([field] * 6), re.ASCII)
m = _227_re.search(resp)
if not m:
raise error_proto(resp)
Expand Down
59 changes: 47 additions & 12 deletions Lib/test/test_ftplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,18 +819,6 @@ def test_source_address_passive_connection(self):
self.skipTest("couldn't bind to port %d" % port)
raise

def test_parse257(self):
self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar')
self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar')
self.assertEqual(ftplib.parse257('257 ""'), '')
self.assertEqual(ftplib.parse257('257 "" created'), '')
self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"')
# The 257 response is supposed to include the directory
# name and in case it contains embedded double-quotes
# they must be doubled (see RFC-959, chapter 7, appendix 2).
self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar')
self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar')

def test_line_too_long(self):
self.assertRaises(ftplib.Error, self.client.sendcmd,
'x' * self.client.maxline * 2)
Expand Down Expand Up @@ -1190,6 +1178,53 @@ def test__all__(self):
'ftpcp', 'test'}
support.check__all__(self, ftplib, not_exported=not_exported)

def test_parse257(self):
self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar')
self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar')
self.assertEqual(ftplib.parse257('257 ""'), '')
self.assertEqual(ftplib.parse257('257 "" created'), '')
self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"')
# The 257 response is supposed to include the directory
# name and in case it contains embedded double-quotes
# they must be doubled (see RFC-959, chapter 7, appendix 2).
self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar')
self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar')

def test_parse227(self):
self.assertEqual(
ftplib.parse227('227 Entering Passive Mode (127,0,0,1,195,80).'),
('127.0.0.1', 195 * 256 + 80))
self.assertEqual(ftplib.parse227('227 =0,0,0,0,0,0'), ('0.0.0.0', 0))
self.assertEqual(
ftplib.parse227('227 (255,255,255,255,255,255)'),
('255.255.255.255', 65535))

# A non-227 reply code is rejected before any parsing.
self.assertRaises(ftplib.error_reply, ftplib.parse227, '250 nope')
# A 227 reply without a valid (h1,h2,h3,h4,p1,p2) tuple fails.
self.assertRaises(ftplib.error_proto, ftplib.parse227,
'227 no numbers here')
# Only 4 fields whereas 6 are expected (missing port number)
self.assertRaises(ftplib.error_proto, ftplib.parse227,
'227 Entering Passive Mode (127,0,0,1).')
# One field is larger than 255
self.assertRaises(ftplib.error_proto, ftplib.parse227,
'227 Entering Passive Mode (1234,0,0,1,195,80).')
self.assertRaises(ftplib.error_proto, ftplib.parse227,
'227 Entering Passive Mode (127,0,0,1,1195,80).')

def test_parse227_linear_time(self):
# gh-162867: a reply made of many digits but no valid
# "(h1,h2,h3,h4,p1,p2)" tuple used to make re.search() backtrack, so
# parse227() ran in time quadratic in len(resp). Parsing such a reply
# must stay fast and still raise error_proto.
resp = '227 ' + '9' * 1_000_000
start_time = time.perf_counter()
self.assertRaises(ftplib.error_proto, ftplib.parse227, resp)
# The function usually takes less than a second
self.assertLessEqual(time.perf_counter() - start_time,
support.SHORT_TIMEOUT)


class CommandLineTest(TestCase):
"""Tests for the ftplib command-line interface."""
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fixed quadratic parsing time in :mod:`ftplib` when parsing the ``227`` reply to
a ``PASV`` command. Parsing is now linear in the length of the reply, while the
result for valid replies is unchanged.
Loading