Skip to content

Commit 9c805a6

Browse files
committed
Fix #70264: CLI server directory traversal
On Windows the built-in webserver doesn't prevent directory traversal when backslashes are used as path component separators. Even though that is not a security issue (the CLI webserver is meant for testing only), we fix that by replacing backslashes in the path with slashes on Windows, because backslashes may be valid characters for file names on other systems, but not on Windows.
1 parent 0e51f97 commit 9c805a6

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

sapi/cli/php_cli_server.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,6 +1579,18 @@ static void normalize_vpath(char **retval, size_t *retval_len, const char *vpath
15791579

15801580
decoded_vpath_end = decoded_vpath + php_url_decode(decoded_vpath, vpath_len);
15811581

1582+
#ifdef PHP_WIN32
1583+
{
1584+
char *p = decoded_vpath;
1585+
1586+
do {
1587+
if (*p == '\\') {
1588+
*p = '/';
1589+
}
1590+
} while (*p++);
1591+
}
1592+
#endif
1593+
15821594
p = decoded_vpath;
15831595

15841596
if (p < decoded_vpath_end && *p == '/') {

sapi/cli/tests/bug70264.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #70264 (CLI server directory traversal)
3+
--INI--
4+
allow_url_fopen=1
5+
--SKIPIF--
6+
<?php
7+
include "skipif.inc";
8+
?>
9+
--FILE--
10+
<?php
11+
include "php_cli_server.inc";
12+
php_cli_server_start(null, null);
13+
echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/..\\CREDITS");
14+
echo file_get_contents("http://" . PHP_CLI_SERVER_ADDRESS . "/..%5CCREDITS");
15+
?>
16+
--EXPECTF--
17+
Warning: file_get_contents(http://%s/..\CREDITS): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
18+
in %sbug70264.php on line %d
19+
20+
Warning: file_get_contents(http://%s/..%5CCREDITS): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
21+
in %sbug70264.php on line %d

0 commit comments

Comments
 (0)