Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/standard/ftp_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ static php_stream *php_ftp_fopen_connect(php_stream_wrapper *wrapper, const char
/* if the user has configured who they are,
send that as the password */
if (FG(from_address)) {
PHP_FTP_CNTRL_CHK(ZSTR_VAL(FG(from_address)), ZSTR_LEN(FG(from_address)), "Invalid password %s")

php_stream_printf(stream, "PASS %s\r\n", ZSTR_VAL(FG(from_address)));
} else {
php_stream_write_string(stream, "PASS anonymous\r\n");
Expand Down
54 changes: 54 additions & 0 deletions ext/standard/tests/streams/ftp_from_address_cntrl.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
--TEST--
ftp:// wrapper must reject control characters in the from address sent as the anonymous password
--SKIPIF--
<?php
if (array_search('ftp', stream_get_wrappers()) === false) die("skip ftp wrapper not available.");
if (!function_exists('pcntl_fork')) die("skip pcntl_fork() not available.");
?>
--FILE--
<?php

$log = tmpfile();

$socket = stream_socket_server("tcp://127.0.0.1:0", $errno, $errstr);
if (!$socket) {
die("server failed\n");
}
$name = stream_socket_get_name($socket, false);
$port = (int) substr($name, strrpos($name, ':') + 1);

$pid = pcntl_fork();
if ($pid === 0) {
pcntl_alarm(60);
$s = stream_socket_accept($socket, 10);
if (!$s) {
exit(1);
}
fputs($s, "220 ready\r\n");
stream_set_timeout($s, 1);
$seen = '';
while (($buf = fgets($s)) !== false) {
$seen .= $buf;
if (str_starts_with($buf, 'USER')) {
fputs($s, "331 need pass\r\n");
} else {
fputs($s, "500 no\r\n");
}
}
fwrite($log, $seen);
exit(0);
}

ini_set('from', "evil\r\nSITE INJECT");
var_dump(@fopen("ftp://127.0.0.1:{$port}/x", "r"));
pcntl_waitpid($pid, $status);

rewind($log);
$seen = stream_get_contents($log);
var_dump(str_contains($seen, 'SITE INJECT'));
var_dump(str_contains($seen, 'PASS'));
?>
--EXPECT--
bool(false)
bool(false)
bool(false)
Loading