diff --git a/UPGRADING b/UPGRADING index 875e05554566..96dd0779da2b 100644 --- a/UPGRADING +++ b/UPGRADING @@ -32,6 +32,11 @@ PHP 8.7 UPGRADE NOTES curl_setopt() return false, instead of being silently interpreted as a combination of redirect types. +- FTP: + . ftp_connect() and ftp_ssl_connect() now throw a ValueError when $port is + outside the 0-65535 range, instead of silently truncating it to its low + 16 bits (e.g. 65536 previously connected to port 21). + - PDO: . PDOStatement::bindColumn() now throws a ValueError when the column name is not present in the result set. It previously reported the condition diff --git a/ext/ftp/php_ftp.c b/ext/ftp/php_ftp.c index 3f22485c39a9..c5928d39fd75 100644 --- a/ext/ftp/php_ftp.c +++ b/ext/ftp/php_ftp.c @@ -144,6 +144,11 @@ PHP_FUNCTION(ftp_connect) RETURN_THROWS(); } + if (port < 0 || port > 65535) { + zend_argument_value_error(2, "must be between 0 and 65535"); + RETURN_THROWS(); + } + if (timeout_sec <= 0) { zend_argument_value_error(3, "must be greater than 0"); RETURN_THROWS(); @@ -186,6 +191,11 @@ PHP_FUNCTION(ftp_ssl_connect) RETURN_THROWS(); } + if (port < 0 || port > 65535) { + zend_argument_value_error(2, "must be between 0 and 65535"); + RETURN_THROWS(); + } + if (timeout_sec <= 0) { zend_argument_value_error(3, "must be greater than 0"); RETURN_THROWS(); diff --git a/ext/ftp/tests/ftp_connect_port_range.phpt b/ext/ftp/tests/ftp_connect_port_range.phpt new file mode 100644 index 000000000000..7566e9a41999 --- /dev/null +++ b/ext/ftp/tests/ftp_connect_port_range.phpt @@ -0,0 +1,31 @@ +--TEST-- +ftp_connect(): $port values outside 0-65535 must throw ValueError, not alias onto a valid port +--EXTENSIONS-- +ftp +--FILE-- +getMessage(), "\n"; + } +} + +// Port 0 must be accepted (maps to default FTP port 21 in ftp_open) +// We expect false (connection refused), not ValueError. +$result = @ftp_connect('127.0.0.1', 0); +var_dump($result === false || is_object($result)); // true either way + +// Port 65535 must be accepted (last valid port). +$result = @ftp_connect('127.0.0.1', 65535); +var_dump($result === false || is_object($result)); // true either way +?> +--EXPECT-- +ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535 +ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535 +ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535 +ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535 +ValueError: ftp_connect(): Argument #2 ($port) must be between 0 and 65535 +bool(true) +bool(true)