Skip to content

ftplib: parse229 raises ValueError instead of error_proto on malformed non-numeric port in EPSV response #162128

Open

Description

@klnmurthi12345

Bug report

When parsing a malformed 229 Entering Extended Passive Mode response where the port field is non-numeric or empty, ftplib.parse229 raises an unhandled ValueError: invalid literal for int() with base 10 instead of ftplib.error_proto(resp).

Minimal Reproducible Example:

import ftplib

peer = ('127.0.0.1', 21)

# Case 1: Non-numeric port
try:
    ftplib.parse229('229 Entering Extended Passive Mode (|||invalid|)', peer)
except Exception as e:
    print("Error:", type(e), e)

# Case 2: Empty port field
try:
    ftplib.parse229('229 Entering Extended Passive Mode (||||)', peer)
except Exception as e:
    print("Error:", type(e), e)

Actual Output:

Error: <class 'ValueError'> invalid literal for int() with base 10: 'invalid'
Error: <class 'ValueError'> invalid literal for int() with base 10: ''

Expected Behavior:

As stated in the function's docstring (Raises error_proto if it does not contain '(|||port|)'), malformed server responses should raise ftplib.error_proto(resp). This also aligns with parse227, which matches digits with regex and raises error_proto(resp) on malformed responses.

Root Cause:

In Lib/ftplib.py:parse229:

    parts = resp[left + 1:right].split(resp[left+1])
    if len(parts) != 5:
        raise error_proto(resp)
    host = peer[0]
    port = int(parts[3])
    return host, port

parts[3] is converted directly with int(parts[3]) without verifying that it contains digits or wrapping in try...except ValueError: raise error_proto(resp).

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Linux, macOS, Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions