ext/ftp: validate $port range before narrowing to short in ftp_connect/ftp_ssl_connect - #23481
Open
lacatoire wants to merge 2 commits into
Open
ext/ftp: validate $port range before narrowing to short in ftp_connect/ftp_ssl_connect#23481lacatoire wants to merge 2 commits into
lacatoire wants to merge 2 commits into
Conversation
…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
Contributor
|
Looks good to me. Does this need a line in |
devnexen
reviewed
Aug 29, 2026
| @@ -0,0 +1,31 @@ | |||
| --TEST-- | |||
Member
There was a problem hiding this comment.
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)
Member
|
change is good but do not forget to add UPGRADING entry ;) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
ftp_connect()andftp_ssl_connect()accept$portaszend_longbut cast it to Cshortat theftp_open()call site without range-checking first. Values outside 0–65535 silently wrap:65536narrows to0, whichftp_open()rewrites to21, connecting to the FTP control port regardless of what the caller requested.The fix validates
$portbefore the cast and throws aValueErrorfor out-of-range values, consistent with how other extensions handle integer narrowing.A test covering the boundary and the silent-wrap case is added.