From 9d070fdb4d751cfe038e9f9e42276bf04fdd0fb8 Mon Sep 17 00:00:00 2001 From: k l n murthi Date: Tue, 17 Nov 2026 00:57:24 +0530 Subject: [PATCH 1/2] gh-162128: Raise error_proto on malformed port in ftplib.parse229 --- Lib/ftplib.py | 5 ++++- Lib/test/test_ftplib.py | 12 ++++++++++++ .../2026-11-17-00-57-00.gh-issue-162128.kln123.rst | 1 + 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst diff --git a/Lib/ftplib.py b/Lib/ftplib.py index 2f092d50f31782b..7b78234a39911e0 100644 --- a/Lib/ftplib.py +++ b/Lib/ftplib.py @@ -845,7 +845,10 @@ def parse229(resp, peer): if len(parts) != 5: raise error_proto(resp) host = peer[0] - port = int(parts[3]) + try: + port = int(parts[3]) + except ValueError: + raise error_proto(resp) from None return host, port diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py index 795527382b2abbd..3a7bb5cc5d4321a 100644 --- a/Lib/test/test_ftplib.py +++ b/Lib/test/test_ftplib.py @@ -1190,6 +1190,18 @@ def test__all__(self): 'ftpcp', 'test'} support.check__all__(self, ftplib, not_exported=not_exported) + def test_parse229(self): + peer = ('127.0.0.1', 21) + self.assertEqual( + ftplib.parse229('229 Entering Extended Passive Mode (|||6400|)', peer), + ('127.0.0.1', 6400) + ) + # Non-numeric or empty port must raise error_proto + with self.assertRaises(ftplib.error_proto): + ftplib.parse229('229 Entering Extended Passive Mode (|||invalid|)', peer) + with self.assertRaises(ftplib.error_proto): + ftplib.parse229('229 Entering Extended Passive Mode (||||)', peer) + class CommandLineTest(TestCase): """Tests for the ftplib command-line interface.""" diff --git a/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst new file mode 100644 index 000000000000000..fdb4800dcd785b1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst @@ -0,0 +1 @@ +:func:`ftplib.parse229` now raises :exc:`ftplib.error_proto` instead of :exc:`ValueError` when encountering a malformed or non-numeric port in EPSV responses. From ae3fb8101336287a9e99bc471e0c7a36361bad56 Mon Sep 17 00:00:00 2001 From: k l n murthi Date: Tue, 17 Nov 2026 07:38:21 +0530 Subject: [PATCH 2/2] gh-162128: Format parse229 in NEWS entry as literal instead of func reference --- .../next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst index fdb4800dcd785b1..6121032f33d9e74 100644 --- a/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst +++ b/Misc/NEWS.d/next/Library/2026-11-17-00-57-00.gh-issue-162128.kln123.rst @@ -1 +1 @@ -:func:`ftplib.parse229` now raises :exc:`ftplib.error_proto` instead of :exc:`ValueError` when encountering a malformed or non-numeric port in EPSV responses. +``ftplib.parse229()`` now raises :exc:`ftplib.error_proto` instead of :exc:`ValueError` when encountering a malformed or non-numeric port in EPSV responses.