Skip to content

Commit 0bb64a7

Browse files
committed
ext/standard: Throw ValueError for filenames with null bytes
This should never happen in the first place
1 parent 6b3f302 commit 0bb64a7

21 files changed

+155
-108
lines changed

UPGRADING

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,25 @@ PHP 8.5 UPGRADE NOTES
126126
. Using a printf-family function with a formatter that did not specify the
127127
precision previously incorrectly reset the precision instead of treating
128128
it as a precision of 0. See GH-18897.
129+
. Filenames with null bytes now always throw a ValueError for the following functions:
130+
- fileperms()
131+
- fileinode()
132+
- filesize()
133+
- fileowner()
134+
- filegroup()
135+
- fileatime()
136+
- filemtime()
137+
- filectime()
138+
- filetype()
139+
- is_writable()
140+
- is_readable()
141+
- is_executable()
142+
- is_file()
143+
- is_dir()
144+
- is_link()
145+
- file_exists()
146+
- lstat()
147+
- stat()
129148

130149
========================================
131150
2. New Features

ext/standard/filestat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ ZEND_NAMED_FUNCTION(name) { \
10181018
zend_string *filename; \
10191019
\
10201020
ZEND_PARSE_PARAMETERS_START(1, 1) \
1021-
Z_PARAM_STR(filename) \
1021+
Z_PARAM_PATH_STR(filename) \
10221022
ZEND_PARSE_PARAMETERS_END(); \
10231023
\
10241024
php_stat(filename, funcnum, return_value); \

ext/standard/tests/file/bug39863.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ Andrew van der Stock, vanderaj @ owasp.org
66
<?php
77

88
$filename = __FILE__ . chr(0). ".ridiculous";
9-
var_dump(file_exists($filename));
9+
try {
10+
var_dump(file_exists($filename));
11+
} catch (Throwable $e) {
12+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
13+
}
1014
?>
1115
--EXPECT--
12-
bool(false)
16+
ValueError: file_exists(): Argument #1 ($filename) must not contain any null bytes
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
filegroup() with filenames with null bytes
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(filegroup("file_with_null_byte.tmp\0"));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), "\n";
10+
}
11+
12+
?>
13+
--EXPECT--
14+
ValueError: filegroup(): Argument #1 ($filename) must not contain any null bytes

ext/standard/tests/file/filegroup_variation3.phpt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ $files_arr = array(
2626
"//filegroup_variation3//filegroup_variation3.tmp",
2727
"/filegroup_variation3/*.tmp",
2828
"filegroup_variation3/filegroup*.tmp",
29-
30-
/* Testing Binary safe */
31-
"/filegroup_variation3/filegroup_variation3.tmp".chr(0),
32-
"/filegroup_variation3/filegroup_variation3.tmp\0"
3329
);
3430

3531
$count = 1;
@@ -74,13 +70,5 @@ bool(false)
7470

7571
Warning: filegroup(): stat failed for %s/filegroup_variation3/filegroup*.tmp in %s on line %d
7672
bool(false)
77-
- Iteration 7 -
78-
79-
Warning: filegroup(): Filename contains null byte in %s on line %d
80-
bool(false)
81-
- Iteration 8 -
82-
83-
Warning: filegroup(): Filename contains null byte in %s on line %d
84-
bool(false)
8573

8674
*** Done ***
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
fileinode() with filenames with null bytes
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(fileinode("file_with_null_byte.tmp\0"));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), "\n";
10+
}
11+
12+
?>
13+
--EXPECT--
14+
ValueError: fileinode(): Argument #1 ($filename) must not contain any null bytes

ext/standard/tests/file/fileinode_variation3.phpt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ $files_arr = array(
2525
"//fileinode_variation3//fileinode_variation3.tmp",
2626
"/fileinode_variation3/*.tmp",
2727
"fileinode_variation3/fileinode*.tmp",
28-
29-
/* Testing Binary safe */
30-
"/fileinode_variation3/fileinode_variation3.tmp".chr(0),
31-
"/fileinode_variation3/fileinode_variation3.tmp\0"
3228
);
3329

3430
$count = 1;
@@ -73,13 +69,5 @@ bool(false)
7369

7470
Warning: fileinode(): stat failed for %s/fileinode_variation3/fileinode*.tmp in %s on line %d
7571
bool(false)
76-
- Iteration 7 -
77-
78-
Warning: fileinode(): Filename contains null byte in %s on line %d
79-
bool(false)
80-
- Iteration 8 -
81-
82-
Warning: fileinode(): Filename contains null byte in %s on line %d
83-
bool(false)
8472

8573
*** Done ***
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
fileowner() with filenames with null bytes
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(fileowner("file_with_null_byte.tmp\0"));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), "\n";
10+
}
11+
12+
?>
13+
--EXPECT--
14+
ValueError: fileowner(): Argument #1 ($filename) must not contain any null bytes

ext/standard/tests/file/fileowner_variation3.phpt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ $files_arr = array(
2626
"//fileowner_variation3//fileowner_variation3.tmp",
2727
"/fileowner_variation3/*.tmp",
2828
"fileowner_variation3/fileowner*.tmp",
29-
30-
/* Testing Binary safe */
31-
"/fileowner_variation3/fileowner_variation3.tmp".chr(0),
32-
"/fileowner_variation3/fileowner_variation3.tmp\0"
3329
);
3430

3531
$count = 1;
@@ -74,13 +70,5 @@ bool(false)
7470

7571
Warning: fileowner(): stat failed for %s/fileowner_variation3/fileowner*.tmp in %s on line %d
7672
bool(false)
77-
- Iteration 7 -
78-
79-
Warning: fileowner(): Filename contains null byte in %s on line %d
80-
bool(false)
81-
- Iteration 8 -
82-
83-
Warning: fileowner(): Filename contains null byte in %s on line %d
84-
bool(false)
8573

8674
*** Done ***
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
fileperms() with filenames with null bytes
3+
--FILE--
4+
<?php
5+
6+
try {
7+
var_dump(fileperms("file_with_null_byte.tmp\0"));
8+
} catch (Throwable $e) {
9+
echo $e::class, ': ', $e->getMessage(), "\n";
10+
}
11+
12+
?>
13+
--EXPECT--
14+
ValueError: fileperms(): Argument #1 ($filename) must not contain any null bytes

0 commit comments

Comments
 (0)