Skip to content

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

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

lacatoire wants to merge 3 commits 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.

@Sjord

Sjord commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Looks good to me. Does this need a line in UPGRADING?

@devnexen

devnexen commented Aug 29, 2026 •

Copy link
Copy Markdown
Member

change is good but do not forget to add UPGRADING entry ;)

…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
Presuming ValueError in the catch would let a change of thrown class slip
through; the class is now printed and asserted alongside the message.
@lacatoire
lacatoire force-pushed the fix/ftp-connect-port-range branch from fe3521f to 84f2dcd Compare November 12, 2026 08:02
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.

3 participants