Skip to content

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

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

…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
@Sjord

Sjord commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

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

@@ -0,0 +1,31 @@
--TEST--

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/ext/ftp/tests/ftp_connect_port_range.phpt b/ext/ftp/tests/ftp_connect_port_range.phpt
index 4225c07cd00..116e1bf94e6 100644
--- a/ext/ftp/tests/ftp_connect_port_range.phpt
+++ b/ext/ftp/tests/ftp_connect_port_range.phpt
@@ -1,31 +1,25 @@
 --TEST--
-ftp_connect(): $port values outside 0-65535 must throw ValueError, not alias onto a valid port
+ftp_connect(): $port outside 0-65535 must throw instead of aliasing onto a valid port
 --EXTENSIONS--
 ftp
 --FILE--
 <?php
 foreach ([-1, 65536, 65536 + 2121, PHP_INT_MIN, PHP_INT_MAX] as $port) {
     try {
-        ftp_connect('127.0.0.1', $port);
+        ftp_connect('totes.invalid', $port, 1);
     } catch (ValueError $e) {
-        echo $e->getMessage(), "\n";
+        echo $e::class, ': ', $e->getMessage(), PHP_EOL;
     }
 }

-// 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
+var_dump(@ftp_connect('totes.invalid', 0, 1));
+var_dump(@ftp_connect('totes.invalid', 65535, 1));
 ?>
 --EXPECT--
-ftp_connect(): Argument #2 ($port) must be between 0 and 65535
-ftp_connect(): Argument #2 ($port) must be between 0 and 65535
-ftp_connect(): Argument #2 ($port) must be between 0 and 65535
-ftp_connect(): Argument #2 ($port) must be between 0 and 65535
-ftp_connect(): Argument #2 ($port) must be between 0 and 65535
-bool(true)
-bool(true)
+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(false)
+bool(false)
diff --git a/ext/ftp/tests/ftp_ssl_connect_port_range.phpt b/ext/ftp/tests/ftp_ssl_connect_port_range.phpt
new file mode 100644
index 00000000000..8b423e2c4a7
--- /dev/null
+++ b/ext/ftp/tests/ftp_ssl_connect_port_range.phpt
@@ -0,0 +1,29 @@
+--TEST--
+ftp_ssl_connect(): $port outside 0-65535 must throw instead of aliasing onto a valid port
+--EXTENSIONS--
+ftp
+--SKIPIF--
+<?php
+if (!function_exists('ftp_ssl_connect')) die('skip ftp_ssl is disabled');
+?>
+--FILE--
+<?php
+foreach ([-1, 65536, 65536 + 2121, PHP_INT_MIN, PHP_INT_MAX] as $port) {
+    try {
+        ftp_ssl_connect('totes.invalid', $port, 1);
+    } catch (ValueError $e) {
+        echo $e::class, ': ', $e->getMessage(), PHP_EOL;
+    }
+}
+
+var_dump(@ftp_ssl_connect('totes.invalid', 0, 1));
+var_dump(@ftp_ssl_connect('totes.invalid', 65535, 1));
+?>
+--EXPECT--
+ValueError: ftp_ssl_connect(): Argument #2 ($port) must be between 0 and 65535
+ValueError: ftp_ssl_connect(): Argument #2 ($port) must be between 0 and 65535
+ValueError: ftp_ssl_connect(): Argument #2 ($port) must be between 0 and 65535
+ValueError: ftp_ssl_connect(): Argument #2 ($port) must be between 0 and 65535
+ValueError: ftp_ssl_connect(): Argument #2 ($port) must be between 0 and 65535
+bool(false)
+bool(false)

@devnexen

devnexen commented Aug 29, 2026

Copy link
Copy Markdown
Member

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

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