Skip to content

ext/ftp: validate $port range before narrowing to short in ftp_connect/ftp_ssl_connect - #23481

Open
lacatoire wants to merge 1 commit into
php:masterfrom
lacatoire:fix/ftp-connect-port-range
Open

ext/ftp: validate $port range before narrowing to short in ftp_connect/ftp_ssl_connect#23481
lacatoire wants to merge 1 commit into
php:masterfrom
lacatoire:fix/ftp-connect-port-range

Conversation

@lacatoire

Copy link
Copy Markdown
Member

ftp_connect() and ftp_ssl_connect() accept $port as zend_long but cast it to C short at the ftp_open() call site without range-checking first. Values outside 0–65535 silently wrap: 65536 narrows to 0, which ftp_open() rewrites to 21, connecting to the FTP control port regardless of what the caller requested.

The fix validates $port before the cast and throws a ValueError for out-of-range values, consistent with how other extensions handle integer narrowing.

A test covering the boundary and the silent-wrap case is added.

…ct/ftp_ssl_connect

$port was parsed as zend_long but cast to C short before being passed to
ftp_open(). Values outside 0-65535 silently connected to a different port
(their low 16 bits), e.g. 65536 -> 0 -> 21, and 2121+2**32 -> 2121.

Add a range guard (must be between 0 and 65535) in both ftp_connect() and
ftp_ssl_connect() before the connection attempt, consistent with the
existing timeout guards a few lines below.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant